Lets say I have a block of text:
and I want to comment it all out in vi
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.
I know I can do this with numbered lines, like
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 |
An alternative for plain vi, and if you have a small number of lines to change, is to do this
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. answered 06 May '10, 21:40 codebunny |
You can use macro recording to do this:
So all together the key presses would be: answered 06 May '10, 19:51 SiegeX 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
(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
(07 May '10, 15:49)
SiegeX
|
Use repeat last substitute maybe. (unless that's vim specific)
will comment out 4 lines answered 06 May '10, 21:41 olejorgenb Assuming this isn't
(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. answered 06 May '10, 19:50 Jason |
For plain standard vi, do this:
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. answered 06 May '10, 20:54 codebunny 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
(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. answered 06 May '10, 19:20 peter |
why dont you use Emacs..its very simple in emacs which you have asked answered 06 May '10, 19:34 srinivasmiri... 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:
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. answered 06 May '10, 20:42 Mike 1 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
|
Remember VI uses sed and awk commands. Research how to do what you want to do with sek and awk commands and you will be way ahead of a VI expert. Visit ora.com to get the best technical books for linux users. O'Reilly permits you purchase directly from them and will sell you the next revision of the book at a discount. answered 16 Jul '14, 11:44 Mike Reno |
Is there a reason you're not using vim?
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.