Skip to content

Instantly share code, notes, and snippets.

@h1mesuke
Created June 23, 2010 06:16
Show Gist options
  • Save h1mesuke/449571 to your computer and use it in GitHub Desktop.
Save h1mesuke/449571 to your computer and use it in GitHub Desktop.
Vim - My keymaps for commenting out source codes
"-----------------------------------------------------------------------------
" Commenting-out
" derived from:
" http://nanasi.jp/articles/vim/commentout_source.html
" <prefix><x> - encomment
" <prefix><prefix><x> - encomment with copy
" <prefix>c - decomment
"---------------------------------------
" Encomment
"" lhs comments
vnoremap <silent> ,! :s/^ \?/!/<CR>:noh<CR>
vnoremap <silent> ," :s/^ \?/\"/<CR>:noh<CR>
vnoremap <silent> ,# :s/^ \?/#/<CR>:noh<CR>
vnoremap <silent> ,% :s/^ \?/%/<CR>:noh<CR>
vnoremap <silent> ,; :s/^ \?/;/<CR>:noh<CR>
vnoremap <silent> ,/ :s/^\( \)\?/\/\//<CR>:noh<CR>
vnoremap <silent> ,- :s/^\( \)\?/--/<CR>:noh<CR>
vnoremap <silent> ,> :s/^\( \)\?/> /<CR>:noh<CR>
" shortcuts
vmap # ,#
vmap / ,/
"" wrapping comments
vnoremap <silent> ,* :s/^ \{,4\}\(\s*\)\(.*\)\s*$/\/\* \1\2 \*\//<CR>:'<,'>Align! p1P0 */<CR>:noh<CR>u<C-r>zz
vnoremap <silent> ,< :s/^ \{,5\}\(\s*\)\(.*\)\s*$/<!-- \1\2 -->/<CR>:'<,'>Align! p1P0 --><CR>:noh<CR>u<C-r>zz
"" block comments
vmap <silent> <Leader>* v`<I<CR><Esc>k0i/*<Esc>`>j0i*/<CR><Esc><Esc>
vmap <silent> <Leader>< v`<I<CR><Esc>k0i<!--<Esc>`>j0i--><CR><Esc><Esc>
"---------------------------------------
" Encomment with copy
"" lhs comments
vmap <silent> ,,! "vygv,!'>"vp
vmap <silent> ,," "vygv,"'>"vp
vmap <silent> ,,# "vygv,#'>"vp
vmap <silent> ,,% "vygv,%'>"vp
vmap <silent> ,,; "vygv,;'>"vp
vmap <silent> ,,/ "vygv,/'>"vp
vmap <silent> ,,- "vygv,-'>"vp
vmap <silent> ,,> "vygv,>'>"vp
"" wrapping comments
vmap <silent> ,,* "vygv,*'>"vp
vmap <silent> ,,< "vygv,<'>"vp
"" block comments
vmap <silent> <Leader><Leader>* "vygv<Leader>*"vP
vmap <silent> <Leader><Leader>< "vygv<Leader>h"vP
"---------------------------------------
" Decomment
"" lhs and wrapping comments
vnoremap ,c :s/^\s*\([!"#%;]\\|\/\/\\|--\\|> \\|\/\*\\|<!--\)//<CR>:'<,'>s/\s*\(\*\/\\|-->\\|\)\s*$//<CR>:noh<CR>`<=`>
vnoremap <Leader>c <Esc><Esc>'<dd'>dd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment