Skip to content

Commit

Permalink
Add insert mode command to comment around cursor
Browse files Browse the repository at this point in the history
  • Loading branch information
tomtomjhj committed Jun 2, 2021
1 parent 349340d commit 00d8cb4
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
4 changes: 4 additions & 0 deletions doc/commentary.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ gc Text object for a comment (operator pending mode
gcgc Uncomment the current and adjacent commented lines.
gcu

*i_CTRL-/* *i_CTRL-_*
CTRL-/ or CTRL-_ Insert comment delimiters around the cursor in insert
mode.

*:Commentary*
:[range]Commentary Comment or uncomment [range] lines

Expand Down
8 changes: 8 additions & 0 deletions plugin/commentary.vim
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,18 @@ function! s:textobject(inner) abort
endif
endfunction

function s:insert()
let [l, r] = s:surroundings()
call feedkeys(l . r . repeat("\<Left>", strchars(r)), 'ni')
endfunction

command! -range -bar Commentary call s:go(<line1>,<line2>)
xnoremap <expr> <Plug>Commentary <SID>go()
nnoremap <expr> <Plug>Commentary <SID>go()
nnoremap <expr> <Plug>CommentaryLine <SID>go() . '_'
onoremap <silent> <Plug>Commentary :<C-U>call <SID>textobject(get(v:, 'operator', '') ==# 'c')<CR>
nnoremap <silent> <Plug>ChangeCommentary c:<C-U>call <SID>textobject(1)<CR>
inoremap <silent> <Plug>CommentaryInsert <C-\><C-O>:call <SID>insert()<CR>
nmap <silent> <Plug>CommentaryUndo :echoerr "Change your <Plug>CommentaryUndo map to <Plug>Commentary<Plug>Commentary"<CR>
if !hasmapto('<Plug>Commentary') || maparg('gc','n') ==# ''
Expand All @@ -114,6 +120,8 @@ if !hasmapto('<Plug>Commentary') || maparg('gc','n') ==# ''
nmap cgc <Plug>ChangeCommentary
endif
nmap gcu <Plug>Commentary<Plug>Commentary
imap <C-/> <C-G>u<Plug>CommentaryInsert
imap <C-_> <C-G>u<Plug>CommentaryInsert
endif

" vim:set et sw=2:

0 comments on commit 00d8cb4

Please sign in to comment.