Vi commenting out
Jorge Almeida
jalmeida
Tue Oct 17 05:05:38 PDT 2006
On Tue, 17 Oct 2006, Roger Oberholtzer wrote:
> On Tue, 2006-10-17 at 08:22 +0100, Jorge Almeida wrote:
>> On Mon, 16 Oct 2006, Michael Hipp wrote:
>>
>>> Is there any simple trick for having vi comment out a whole range of lines by
>>> placing a '#' in front of them? The manual method of i # ESC j is rather
>>> tedious if there is a big section to be done.
>>>
>> Are you talking about the real vintage vi, as opposed to vim?
>>
>> With vim, I select the range in visual mode and just do :s/^/#/
>> Actually, I use a visual mode macro to do it (Ctrl-X Ctrl-D to comment
>> and Ctrl-X Ctrl-U to uncomment). It's as easy as I could wish.
>> (Don't know a thing about vi...)
>
> My vim (6.3.84) does not do this. How are you selecting the text to
> comment?
I have vim-7.0.17, but I think it worked before.
To select text, I press Shift-V in normal mode and then press the up or
down arrow (or k/j key) until the required region is selected. (Pressing
4k after the initial Shift-V will select four lines above the initial
line, etc.)
> Is this macro (Ctrl-X Ctrl-D) something you have defined?
Yes.
:vmap <C-X><C-D> :s/^/\#/<CR>
:vmap <C-X><C-U> :s/^#//<CR>
I also have:
:imap <C-X><C-D> <Esc>m`0i#<Esc>``la
:nmap <C-X><C-D> V:s/^/\#/<CR>
:imap <C-X><C-U> <Esc>V:s/^#//<CR>
:nmap <C-X><C-U> V:s/^#//<CR>
(These are usefull to comment/uncomment the current line only.)
The keys probably conflict with some vim default, but I remember it was
something I had no use for.
For tex files, for example, put the above in
~/.vim/ftplugin/tex/general.vim
after substituting # by % (etc)
For html:
:vmap <C-X><C-D> :s/\(.*\)/<!--\1-->/<CR>
:vmap <C-X><C-U> :s/\s*<!--\(.*\)-->/\1/<CR>
--
Jorge Almeida
More information about the Linux-users
mailing list