0

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.

3
  • Uncommenting lines or removing lines that are comments?
    – user31170
    Sep 11, 2018 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. Sep 11, 2018 at 14:13
  • Np. Keep on truckin'
    – user31170
    Sep 12, 2018 at 1:55

1 Answer 1

1

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 .