Vim logo vim online Vim Book Ad

 Tip #271: easy (un)commenting out of source code

 tip karma   Rating 52/22, Viewed by 1918 

created:   June 30, 2002 22:57      complexity:   intermediate
author:   [email protected]      as of Vim:   5.7

Something that I do quite alot is comment out blocks of text, only to uncomment that same block later. The following mappings have proven useful to me. They can be applied using visually selected blocks, or with motion keys.

" lhs comments
map ,# :s/^/#/<CR>
map ,/ :s/^/\/\//<CR>
map ,> :s/^/> /<CR>
map ," :s/^/\"/<CR>
map ,% :s/^/%/<CR>
map ,! :s/^/!/<CR>
map ,; :s/^/;/<CR>
map ,- :s/^/--/<CR>
map ,c :s/^\/\/\\|^--\\|^> \\|^[#"%!;]//<CR>

" wrapping comments
map ,* :s/^\(.*\)$/\/\* \1 \*\//<CR>
map ,( :s/^\(.*\)$/\(\* \1 \*\)/<CR>
map ,< :s/^\(.*\)$/<!-- \1 -->/<CR>
map ,d :s/^\([/(]\*\\|<!--\) \(.*\) \(\*[/)]\\|-->\)$/\2/<CR>

The commands to comment a selection of text are as follows, begining with begining-of-line comments:

    ,#    shell, perl, etc
    ,/     c++
    ,>    email quote
    ,"     vim
    ,%    latex, prolog
    ,!      assembly?... add single !
    ,;      scheme
    ,-      don't remember this one... add --
    ,c     clears any of the previous comments

Here are the wrapping comments, each line wrapped individually:

    ,*      c
    ,(       Standard ML
    ,<      html
    ,d      clears any of the wrapping comments

 rate this tip  Life Changing Helpful Unfulfilling 

<<Insert a single character | automaticaly formating pasted text (p=`]) >>

Additional Notes

Anonymous, July 1, 2002 6:28
After executing this tip, the content of "/ is highlighted in my gvim; which is rather annoying.
In e.g. MapBasic which I'm trying to figure out right now, I use:
map ,' :s/^/'/<CR> :let @/=""<CR>
Leo
alinets<at>yahoo.com, July 1, 2002 13:26
I am having the same problem,
I have hlsearch option on (highlight search) and when I use one of the mappings to add comment it leaves me with a lot of hihjlighting that is not required/intended. Is there a way to turn off highlight search for these command only ?
alinets<at>yahoo.com, July 1, 2002 14:40
OK, figured it out.
Just add :nohlsearch <CR> at the end of the mapping and it would remove the highlight.
Highlighting would be reenabled when you do the next search. I checked it out it works

" lhs comments
map ,# :s/^/#/<CR> <Esc>:nohlsearch <CR>
map ,/ :s/^/\/\//<CR> <Esc>:nohlsearch <CR>
map ,> :s/^/> /<CR> <Esc>:nohlsearch<CR>
map ," :s/^/\"/<CR> <Esc>:nohlsearch<CR>
map ,% :s/^/%/<CR> <Esc>:nohlsearch<CR>
map ,! :s/^/!/<CR> <Esc>:nohlsearch<CR>
map ,; :s/^/;/<CR> <Esc>:nohlsearch<CR>
map ,- :s/^/--/<CR> <Esc>:nohlsearch<CR>
map ,c :s/^\/\/\\|^--\\|^> \\|^[#"%!;]//<CR> <Esc>:nohlsearch<CR>

" wrapping comments
map ,* :s/^\(.*\)$/\/\* \1 \*\//<CR> <Esc>:nohlsearch<CR>
map ,( :s/^\(.*\)$/\(\* \1 \*\)/<CR><Esc>:nohlsearch <CR>
map ,< :s/^\(.*\)$/<!-- \1 -->/<CR> <Esc>:nohlsearch<CR>
map ,d :s/^\([/(]\*\\|<!--\) \(.*\) \(\*[/)]\\|-->\)$/\2/<CR> <Esc>:nohlsearch<CR>
[email protected], July 1, 2002 14:50
Adding :nohlsearch<cr> to the end of each pattern clears up the highlighting. Note that this doesn't turn off search highlighting, it just removes the highlighting that's there. Here are the ammended regexs:

" lhs comments
map ,# :s/^/#/<CR>:nohlsearch<CR>
map ,/ :s/^/\/\//<CR>:nohlsearch<CR>
map ,> :s/^/> /<CR>:nohlsearch<CR>
map ," :s/^/\"/<CR>:nohlsearch<CR>
map ,% :s/^/%/<CR>:nohlsearch<CR>
map ,! :s/^/!/<CR>:nohlsearch<CR>
map ,; :s/^/;/<CR>:nohlsearch<CR>
map ,- :s/^/--/<CR>:nohlsearch<CR>
map ,c :s/^\/\/\\|^--\\|^> \\|^[#"%!;]//<CR>:nohlsearch<CR>

" wrapping comments
map ,* :s/^\(.*\)$/\/\* \1 \*\//<CR>:nohlsearch<CR>
map ,( :s/^\(.*\)$/\(\* \1 \*\)/<CR>:nohlsearch<CR>
map ,< :s/^\(.*\)$/<!-- \1 -->/<CR>:nohlsearch<CR>
map ,d :s/^\([/(]\*\\|<!--\) \(.*\) \(\*[/)]\\|-->\)$/\2/<CR>:nohlsearch<CR>
[email protected], July 1, 2002 14:52
Jinx!
[email protected], July 3, 2002 2:53
A more intelligent way is to save the search pattern highlighted.
let hls=@/|s ... |let @/=hls
[email protected], July 3, 2002 5:52
The problem with that is that it thwarts motion keys. The way it stands you can do something like '3,#' and it will comment 3 lines. Ranges don't work with 'let ...', unfortunately.
[email protected], July 3, 2002 12:16
This function is based on the various comments here, and seems to work.
fun CppstyleQuote()
   let hls=@/
   s/^/\/\//
   let @/=hls
endfun
map ,/ :call CppstyleQuote()<CR>

Leo
[email protected], July 6, 2002 17:23
" I use a single mapping, which can comment/uncomment line:
function! C_CommentLine()
if getline(".") =~ '/\*.*\*/'
normal ^2x$xx
else
normal I/*A*/
endif
endfunction
nmap <buffer> <Esc>d :call C_CommentLine()<LF>

" <Esc>d on my term means <M-D> or Alt-D

" This mapping comments the visual selection, You have to perform a selection from left to
" right or from top to bottom. If you do a linewise selection, the cursor
" position, not the end of selection matters.
vmap <buffer> <Esc>d a*/gvoi/*

[email protected], September 23, 2002 19:22
i use the mouse the select the lines i want to comment/uncomment and have this on the right click menu
to insert/delete // at the start of the line:

:vmenu PopUp.Comments.Add  :s/^/\/\//<CR>
:vmenu PopUp.Comments.Remove  :s/^..//<CR>

seems to work ok
[email protected], September 28, 2002 16:02
The comment whose language you forgot (in your notes), ,-, is for SQL comments....
[email protected], October 12, 2002 11:30
hi all!!
those are great, but i don't like the highlighted search and also i like my //  allways at the very begining of the line, so i've wrote this function (very newbie and unoptimized, but it works XD )

it comments and uncomments the current line:

"""""""""""""""""""""""""""""""""""""""""""""""""
function! MyComm()
    let linenum= line('.')
    let line = getline('.')

    let commpos= match(line, "//")
    let n = 0
    while n< commpos
        if line[n]!= " " && line[n]!= "\t"
            break
        endif
        let n= n+1
    endwhile
    if n== commpos && commpos!= -1
        let line= strpart(line, 0, commpos).strpart(line, commpos+2)
    else
        let line= "//".line
    endif

    let err= setline(linenum, line)
endfunction
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"and use this keybindings:

map <M-c> :call MyComm()<CR>
imap <M-c> <esc>:call MyComm()<CR>i

" for the /* */ pair, i use visual mode, an then alt-v:

vmap <M-v> v`<I<CR><esc>k0i/*<ESC>`>I<CR><esc>k0i*/<ESC>

hope u find useful :)
[email protected], October 25, 2002 3:04
Using visual block selection at the start of the lines you want to comment, along with 'I' (CAPS-EYE), and the
comment character is also very handy. Decommenting would be (ofcourse!) to select the comments in a visual
block and delete them.
If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to [email protected] after searching the archive. Help Bram help Uganda.
SourceForge Logo