Vim:Commenting Blocks of Code

From Zoelife4U Wiki
Jump to: navigation, search

One of the things that irritates me about IDEs is having to command blocks of code one line at a time. Now, Vim offers relief from this. So vim ~/.vimrc and slap the following in there towards the bottom.

" 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> 

To use just use the comma and the left character that begins the comment:

,#

Here's the list of comments to languages:

    ,#    shell, perl, etc
    ,/     c++
    ,>    email quote
    ,"     vim
    ,%    latex, prolog
    ,!      assembly?... add single !
    ,;      scheme
    ,-      SQL add --
    ,c     clears any of the previous comments 
Personal tools
Online Users
Zoelife4U: