Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Comment more than one line in vi 2

Status
Not open for further replies.

buxtonicer1

Programmer
Sep 20, 2006
82
GB
I'm using vi to write some UNIX shell scripts.

Is there a way to comment out more than one command quickly
instread of me going to each line using esc hjkl to move the cursor around and then x to delete a letter.
Surely there is a quicker way !!!

 
You can use commands like the following in vi editor:
:%s/^/#/g

or

:%s/^#//g

You can replace % with the range of numbers like
:1,5s/^/#/g

Or can use ., $ for current position and end of file respectively.
 
If you use vim you can use block visual mode. Go to the beginning of the first line you want to comment, press ctrl-v to enter block visual mode, press j until you reach the last line you want to comment, press I (capital) to enter insert mode, then press # and esc and all the lines you selected will be commented.
 
map V 0i# ^[j

This mapping makes commenting (# + 2 space chars) superfast when you type Shift-V. It works on the line to be commented as long the cursor is somewhere on the intended line.


Put it in your .exrc file. Note that '^[' can be typed in most consoles as ctrl-v + ctrl+[


Cheers,
ND [smile]
 
You can "comment" several lines in a script like this...
[tt]
command1
: << COMMENT!
command2
command3
COMMENT!
command4
[/tt]
This prevents command2 and command3 from running.
 
Hello Ygor,

I tried your command on AIX using sh and ksh.

The first time I had the 2nd and 5th lines starting in the middle of the line. That time only command1 executed.

I quickly fixed that and made the 2nd line ": << COMMENT!" and 5th line "COMMENT!" to start at the beginning of the line.

This time it worked as you said it would. What a difference the starting position of these 2 lines made!!

Can you or someone explain why it failed to work the first time but worked the second time?
 
It is only the end-sequence that needs to be at the beginning of the line.

This trick uses an inline input document or here-document and you can use any word you prefer:

cmd run
: <<EndOfComment
cmd not run
cmd not run
cmd not run
EndOfComment
cmd run

The : is the null command which doesn't do anything, but the shell now provides an input stream for it containing the three lines between the : line and the terminating marker and so the shell is "hiding" these lines for itself. The only thing is that the terminating marker needs to ba at the first position.

see also


HTH,

p5wizard
 
Another way:
cmd run
if [ 0 = 1 ]; then
cmd not run
cmd not run
cmd not run
fi
cmd run

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 

p5wizard, have a star as thanks for the nice explanation of the here-document in that script.
 
I forgot to mention that my suggestion doesn't create a temporary file (the here-doc does).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top