Skip to content

Add insert mode command to comment around cursor #146

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -102,12 +102,18 @@ function! s:textobject(inner) abort
endif
endfunction

function s:insert()
let [l, r] = s:surroundings()
return l . r . repeat("\<C-G>U\<Left>", strchars(r))
endfunction

command! -range -bar -bang Commentary call s:go(<line1>,<line2>,<bang>0)
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 <expr> <Plug>CommentaryInsert <SID>insert()
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 @@ -116,6 +122,8 @@ if !hasmapto('<Plug>Commentary') || maparg('gc','n') ==# ''
omap gc <Plug>Commentary
nmap gcc <Plug>CommentaryLine
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: