" ============================================================================= " commentop.vim - commands and operators to comment/uncomment lines "============================================================================= " " Author: Takahiro SUZUKI " Version: 1.0.2 (Vim 7.1) " Licence: MIT Licence " URL: http://www.vim.org/scripts/script.php?script_id=2708 " "============================================================================= " Document: {{{1 " "----------------------------------------------------------------------------- " Description: " This plugin provides a set of commands and operators to comment or " uncomment lines. Linewise comment token (such as double quote in vim " script) is detected automatically by looking up filetype of the file. " Here are the preset filetypes: " vim, cpp, php, python, ruby, perl, sh, haskell, tex, matlab " " You can also easily define your own comment token for filetype. Add below " in your .vimrc: " CommentopSetCommentType FILETYPE REMOVEPATTERN INSERTSTRING " " plugin keymaps: " CommentopNormaltoggle " (n) toggle comment [count] lines " CommentopNormalappend " (n) comment out [count] lines " CommentopNormalremove " (n) uncomment [count] lines " " CommentopVisualtoggle " (v) toggle comment selected lines " CommentopVisualappend " (v) comment out selected lines " CommentopVisualremove " (v) uncomment selected lines " " CommentopOperatortoggle " (n op) toggle comment {motion} " CommentopOperatorappend " (n op) comment out {motion} " CommentopOperatorremove " (n op) uncomment {motion} " " default mapping: " co CommentopNormaltoggle " cO CommentopNormalappend " c CommentopNormalremove " " co CommentopVisualtoggle " cO CommentopVisualappend " c CommentopVisualremove " " go CommentopOperatortoggle " gO CommentopOperatorappend " g CommentopOperatorremove " "----------------------------------------------------------------------------- " Installation: " Place this file in /usr/share/vim/vim*/plugin or ~/.vim/plugin/ " "----------------------------------------------------------------------------- " Examples: " in normal mode: " co " toggle comment for this line " 3cO " comment out 3 lines " " in normal mode (operator): " goip " toggle comment for this paragraph " gOi{ " comment out this {} block " " in visual mode: " c " remove comments in visual selection " "----------------------------------------------------------------------------- " ChangeLog: " 1.0.2: " - bug fix (wrong comment string with ft=vim) " 1.0.1: " - bug fix (gO was mapped to comment out operator) " 1.0: " - Initial release " " }}}1 "============================================================================= function! s:CountHeadSpace() let p = getpos('.') if p[2]==1 | return 0 | endif let line = getline('.')[0:p[2]-2] let len = 0 while len < strlen(line) if line[len]=~"[ \]" | let len += 1 | else | break | endif endwhile return len endfunction " visual mode function! s:VisualLinewiseComment(mode) let b = getpos("'<") let e = getpos("'>") call s:Comment(a:mode, e[1]-b[1]+1) endfunction " normal mode with seved count function! s:LinewiseComment(mode) call s:Comment(a:mode, s:count1) endfunction "mode 0:off 1:on 2:toggle function! s:Comment(mode, count) " determine the comment type if has_key(s:comment_types, &ft) let commentmatch = s:comment_types[&ft]['match'] let commentinsertstr = s:comment_types[&ft]['insert'] else return endif let c = a:count " count head spaces normal! ^ let p = getpos('.') let mh = -1 while c>0 let hs = s:CountHeadSpace() if hs0 let iscomment = getline('.')[mh :] =~ commentmatch let prevstr = (mh>0) ? getline('.')[0:mh-1] : '' if a:mode==0 || (a:mode==2 && iscomment) " remove call setline('.', prevstr . substitute(getline('.')[mh :], commentmatch, '', '')) else " insert call setline('.', prevstr . commentinsertstr . getline('.')[mh :]) endif normal! j let c -= 1 endwhile call setpos('.', p) endfunction " comment in/out operator function! s:LinewiseCommentInOperator(type) call s:LinewiseCommentOperator(0, a:type) endfunction function! s:LinewiseCommentOutOperator(type) call s:LinewiseCommentOperator(1, a:type) endfunction function! s:LinewiseCommentToggleOperator(type) call s:LinewiseCommentOperator(2, a:type) endfunction function! s:LinewiseCommentOperator(mode, type) exe 'normal! `[' let b = getpos('.') exe 'normal! `]' let e = getpos('.') exe 'normal! `[' call s:Comment(a:mode, e[1]-b[1]+1) endfunction function! s:SaveCnt() let s:count1 = v:count1 endfunction " function and command to set the comment type from file type let s:comment_types = {} function! s:SetCommentType(...) " 1:filetype, 2:match, 3:insert let s:comment_types[a:1] = {'match': a:2, 'insert': a:3} endfunction command! -nargs=* CommentopSetCommentType :call s:SetCommentType() " preset comment types CommentopSetCommentType vim ^\"[\ ]\\{,1} "\ CommentopSetCommentType sh ^#[\ ]\\{,1} #\ CommentopSetCommentType perl ^#[\ ]\\{,1} #\ CommentopSetCommentType python ^#[\ ]\\{,1} #\ CommentopSetCommentType ruby ^#[\ ]\\{,1} #\ CommentopSetCommentType haskell ^--[\ ]\\{,1} --\ CommentopSetCommentType cpp ^//[\ ]\\{,1} //\ CommentopSetCommentType php ^//[\ ]\\{,1} //\ CommentopSetCommentType tex ^%[\ ]\\{,1} %\ CommentopSetCommentType matlab ^%[\ ]\\{,1} %\ " default keymaps nmap co CommentopNormaltoggle nmap cO CommentopNormalappend nmap c CommentopNormalremove vmap co CommentopVisualtoggle vmap cO CommentopVisualappend vmap c CommentopVisualremove nmap go CommentopOperatortoggle nmap gO CommentopOperatorappend nmap g CommentopOperatorremove " plugin keymaps nnoremap CommentopNormaltoggle :call SaveCnt():call LinewiseComment(2) nnoremap CommentopNormalappend :call SaveCnt():call LinewiseComment(1) nnoremap CommentopNormalremove :call SaveCnt():call LinewiseComment(0) vnoremap CommentopVisualtoggle :call SaveCnt():call VisualLinewiseComment(2) vnoremap CommentopVisualappend :call SaveCnt():call VisualLinewiseComment(1) vnoremap CommentopVisualremove :call SaveCnt():call VisualLinewiseComment(0) nnoremap CommentopOperatortoggle :call SaveCnt():set opfunc=LinewiseCommentToggleOperatorg@ nnoremap CommentopOperatorappend :call SaveCnt():set opfunc=LinewiseCommentOutOperatorg@ nnoremap CommentopOperatorremove :call SaveCnt():set opfunc=LinewiseCommentInOperatorg@