You can also use Visual Mode if you don't want to count lines :
Press [Esc] key
Use arrows to move cursor to the beginning of your paragraph
Press [v] to enter Visual Mode
Use arrows to select all the lines you want to comment
Then type the following command :
:'<,'>s/^/#/g
Note : when you hit ':' in Visual Mode, ":'<,'>" is added automatically.
Comments
I have a file containing 50 lines.
I want to comment line number 4 to 8 and line number 12 to 20 in same time or in one query.
how can i do that kindly update..
REgards
Linux Lover
'|' can be used to separate commands, so you can give multiple commands in one line. If you want to use '|' in an argument, precede it with '\'.
:4,8 s/^/#/ | 12,20 s/^/#/
Instead of counting lines ( using: .,+N ) one could also use a search ( .,/WORDONTHELASTLINE/ ),
so
this is line 1
this is line 2
this is line three
this is line 4
could be commented by putting the cursor on line 1 and using:
.,/three/ s/^/#/g
Using
#if 0
Lines to be commented
#endif
is much easier....
To comment out multiple lines while writing scripts under vi where "#" is the comment marker.
Where "N" is number of lines to be commented after the current cursor location inclusive of the current line.
You can also use Visual Mode if you don't want to count lines :
Press [Esc] key
Use arrows to move cursor to the beginning of your paragraph
Press [v] to enter Visual Mode
Use arrows to select all the lines you want to comment
Then type the following command :
:'<,'>s/^/#/g
Note : when you hit ':' in Visual Mode, ":'<,'>" is added automatically.
To remove comment, same procedure but :
:'<,'>s/^#//g
Thank you!