I'm finally learning how to use sed to edit configs while installing Ubuntu(Server/Desktop). After much gnashing of teeth and searching about for the proper syntax to not remove the "(comment double-quotes with exactly one blank space afterward(real comments)), as opposed to the lines that have potential code to toggle(zero blank spaces or two blank spaces for indented lines of code) in the /etc/vim/vimrc file, I figured out how to get things working with the following command:

$ sudo sed -i.orig '/^\" [a-zA-Z]\|^"set compatible\|^\" let g:skip_defaults_vim = 1b/! s/^\"//' /etc/vim/vimrc

Will the Guru's or apprentices, alike, please let me know if I could have done this better(tighter/cleaner/etc.)?

Example using awk for the same result?

Thank you.

closed as off-topic by Gerald Schneider, womble Sep 11 '18 at 10:33

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions on Server Fault must be about managing information technology systems in a business environment. Home and end-user computing questions may be asked on Super User, and questions about development, testing and development tools may be asked on Stack Overflow." – womble
If this question can be reworded to fit the rules in the help center, please edit the question.

  • Uncommenting lines or removing lines that are comments? – user31170 Sep 11 '18 at 7:13
  • Thanks Barry for asking for clarification, I meant just removing the "(double-quote) on lines that contain code while leaving the true comments, commented. – Chris Rainey Sep 11 '18 at 14:13
  • Np. Keep on truckin' – user31170 Sep 12 '18 at 1:55

Use the \1 numbered capture variables in the change destination like so s/^([[:space:]]*)"[[:space:]]*(let .*)/\1\2/

Also use sed 'your change;another change' file > file.tmp$$ && mv file.tmp$$ file

It's the "safer" and portable way to change files in place than sed -i ...

For dotfiles like ~/.vimrc under ~ and conf files under /etc, use git to save them locally. etckeeper is great for /etc management with git.

Some people use augeas to edit conf files, or even chef/puppet to manage them automatically.

Not the answer you're looking for? Browse other questions tagged or ask your own question.