Lets say I have a block of text:

def blarg_func:
    blarg = 1
    return blarg

and I want to comment it all out in vi

#def blarg_func:
#    blarg = 1
#    return blarg

What I usually do is go to the first line and hit "^i" and then enter "#". next I arrow down to the next line and hit "." and then repeat until all the lines are commented.

If I hit "2" then "." it will give me.

#def blarg_func:
##    blarg = 1
    return blarg

I know I can do this with numbered lines, like

:1,3s/^/#/

but I want to be able to do this without having to enter line numbers.

Keep in mind I am using vi not vim, so "^v" will not work.

asked 06 May '10, 18:47

Joehillen's gravatar image

Joehillen
1461211
accept rate: 40%

Is there a reason you're not using vim?

(06 May '10, 19:32) aardvark

Sometimes you get older Unix boxes that have vi, and you can't compile and install Vim on them because of the age. I have three like that in my environment. It's horrible to go back to old SCO boxes.

(07 May '10, 03:49) codebunny



An alternative for plain vi, and if you have a small number of lines to change, is to do this

  1. put your cursor on the first line
  2. make a note of how many lines you want to change (example 4)
  3. :.,.+3s/^/#/

What you are doing here is telling it to do a search and replace on the block marked out by the current line (. - dot) to the current line plus 3 (dot plus 3). If you have four lines to comment out, it will be current line plus 3 = .,.+3. If you have 10 lines it will be current line plus 9 = .,.+9.

When you do a search and replace, before the s you can specify the region that the search and replace is to apply to. One method of specifying the region is to use line numbers, like m,n = from line m to line n. Example 45,55.

There are a number of abbreviations that fit in there. $ = last line in the file. So if you want to apply it to the whole file, you use 1,$ = from line 1 to last line. Another abbreviation is . meaning the current line. Example .,$ meaning from the current line to the last line in the file.

You can also do relative numbers, like $ - 5 meaning up to the fifth last line. Or .+4 meaning current line plus 4. Or .-2 meaning current line minus 2.

link

answered 06 May '10, 21:40

codebunny's gravatar image

codebunny
4086
accept rate: 38%

edited 06 May '10, 21:46

You can use macro recording to do this:

  1. Put cursor on the 'd' in def blarg_func:
  2. Press Esc to go into COMMAND MODE
  3. Type qa to start a recording macro and store it in register 'a'
  4. Press i to go into INSERT MODE
  5. Press # to enter a literal pound symbol
  6. Press j or the down arrow to go to the next line
  7. Press q to stop recording
  8. Press <#>@a to repeat the macro stored in register a. Example 10@a to comment out the next 10 lines.

So all together the key presses would be: qai#<ESC>jq10@a

link

answered 06 May '10, 19:51

SiegeX's gravatar image

SiegeX
1213
accept rate: 50%

edited 06 May '10, 20:05

upvote for a hilariously long key squence.

(06 May '10, 20:29) Joehillen

Well, if you think about it the only key sequence added to do what you want is the qa, q and 10@a parts. The i#<ESC>j sequence is something you were going to do anyway. Also, once you have the macro recorded you simply need to run <#>@a at any other location you want to comment out. Finally, you can record some very complex operations making the overhead of recording trivial.

(06 May '10, 20:42) SiegeX

Please note that you will want to use I or ^i instead of i as the # will always be a the start of the line. This will save a headache later

(07 May '10, 06:04) kainosnous

@kainosnous: Can you please elaborate on what you are trying to say, where and why would I use l to presumably move one char right or ^i which I'm guessing means <CTRL>+i? – SiegeX 0 secs ago

(07 May '10, 15:49) SiegeX

Use repeat last substitute maybe. (unless that's vim specific)

:s/^/#/ <return> j3&

will comment out 4 lines

link

answered 06 May '10, 21:41

olejorgenb's gravatar image

olejorgenb
614
accept rate: 33%

Assuming this isn't vim specific, this is my favorite answer.

(07 May '10, 03:28) SiegeX

map something to it:

:map ^K I#^[jI#^[

that works for me on AIX, so should work in any vi.

To get the esc working, use control-v, so the key sequence is:

colon map control-k space shift-i shift-3 control-v esc j shift-i shift-3 control-v esc

HTH.

link

answered 06 May '10, 19:50

Jason's gravatar image

Jason
113
accept rate: 0%

For plain standard vi, do this:

  1. Go to the starting line of the block
  2. ma (to mark the start of the block)
  3. Move to the end of the block.
  4. :'a,.s/^/#/

Basically, you go to to the first line of the block and mark it and you may as well use register a. So that's ma.

Then you move to the end of the block and you do a search and replace.

: puts you into ex mode

'a is the start of the search block and it refers to the mark you made earlier

. is the current line

so the whole block is described by 'a,. (dash a comma dot)

then you replace the start of the line with a #, but only for that specified block (s/^/#/)

This is standard vi, so it will work for you.

link

answered 06 May '10, 20:54

codebunny's gravatar image

codebunny
4086
accept rate: 38%

edited 06 May '10, 21:36

RE: you comment on the other question's visual block mode, you're right, it doesn't work that way. After you highlight the block in visual mode, hit : to go into command mode and you'll notice it will be auto filled with :'<,'> which basically means the highlighted portion. To that you add s/^/#/ just like your answer here. So basically its just like marking.

(07 May '10, 15:53) SiegeX

I looked at that method again and realised where I made my mistake - I thought the colon was part of the instructions, not part of the command to type. But still, it's Vim specific.

(07 May '10, 18:21) codebunny

Try the Enhanced Commentify plugin. Alternatively you can add some code to your vimrc for each filetype - see here.

link

answered 06 May '10, 19:20

peter's gravatar image

peter
1
accept rate: 0%

why dont you use Emacs..its very simple in emacs which you have asked

link

answered 06 May '10, 19:34

srinivasmiriyalu's gravatar image

srinivasmiri...
1
accept rate: 0%

I'm using a bare-bones FreeBSD server that only has vi and nano. No vim. No emacs. =(

(06 May '10, 19:49) Joehillen

Quicker, no custom macros needed:

  1. Press shift-V to go into block selection mode
  2. Select the block you want to comment out
  3. Press: shift-i # ESC
  4. Profit

Shift - I will take you to the beginning of the line, then add your # character. Escape, and it's applied to the entire highlighted block.

This may be vim specific.

link

answered 06 May '10, 20:42

Mike%201's gravatar image

Mike 1
1
accept rate: 0%

I couldn't make it add the # to the whole block. It only did it to the first line. I would love to have this work. Did you miss a step, or did I do something wrong?

(06 May '10, 20:59) codebunny

Yes, I was doing something wrong, in step 3 I thought the colon was part of the word Press:, but instead I now realise I have to type the colon first.

(07 May '10, 18:17) codebunny

It is Vim specific and won't work in vi.

(07 May '10, 18:19) codebunny

I usually do... ctrl-v, shift-i, #, esc

(18 May '10, 19:59) inty
Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "Title")
  • image?![alt text](/path/img.jpg "Title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×3

Asked: 06 May '10, 18:47

Seen: 17,820 times

Last updated: 06 May '10, 21:46

powered by OSQA