Tuesday, June 07, 2005

Commenting Lines using vim for bash,conf file etc

It was needed that I comment a number of lines in a file. The comments of a configuration file are usally like a # in the begining of the line.
So
Line 1
Line 2
Line 3
Should become

#Line 1
#Line 2
#Line 3

HOWTO:

1) Go to the First Column of the First Line.
2) Select the Block using cntl-v (for Linux) and cntl-q(for Windows, because cntl-v is mapped to paste function in gvim under windows)
3) With the Selected test, give the command :s/^/#/
4) That should do!

But vim users always look for yet another way, so I am.

3 comments:

Kannappan said...

Wont this work? sed s/^/\#/g filename

Kannappan said...

awk -F '{print"#"$n}' filename

Senthil Kumaran said...

Yeah,the sed script worked and awk gave soem pbms, but both are supposed to work throughout the file and I am sure when prefixed with numbering like 3,10 it should able to comment out particular section as well.
In vim, :3,6s/^/#/
Should also be possible.
When I searched for this at GG (comp.editors), I came across a key to change the visually highlightes text. Something like .. after CNTRL+Q step, i#l ESC
Tried it, did not work. :( so just putup a working thing.