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
<<Insert a single character |
automaticaly formating pasted text (p=`]) >>
Additional Notes
|