Skip to content

Commit 00d8cb4

Browse files
committed
Add insert mode command to comment around cursor
1 parent 349340d commit 00d8cb4

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

doc/commentary.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ gc Text object for a comment (operator pending mode
2626
gcgc Uncomment the current and adjacent commented lines.
2727
gcu
2828

29+
*i_CTRL-/* *i_CTRL-_*
30+
CTRL-/ or CTRL-_ Insert comment delimiters around the cursor in insert
31+
mode.
32+
2933
*:Commentary*
3034
:[range]Commentary Comment or uncomment [range] lines
3135

plugin/commentary.vim

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,18 @@ function! s:textobject(inner) abort
9797
endif
9898
endfunction
9999

100+
function s:insert()
101+
let [l, r] = s:surroundings()
102+
call feedkeys(l . r . repeat("\<Left>", strchars(r)), 'ni')
103+
endfunction
104+
100105
command! -range -bar Commentary call s:go(<line1>,<line2>)
101106
xnoremap <expr> <Plug>Commentary <SID>go()
102107
nnoremap <expr> <Plug>Commentary <SID>go()
103108
nnoremap <expr> <Plug>CommentaryLine <SID>go() . '_'
104109
onoremap <silent> <Plug>Commentary :<C-U>call <SID>textobject(get(v:, 'operator', '') ==# 'c')<CR>
105110
nnoremap <silent> <Plug>ChangeCommentary c:<C-U>call <SID>textobject(1)<CR>
111+
inoremap <silent> <Plug>CommentaryInsert <C-\><C-O>:call <SID>insert()<CR>
106112
nmap <silent> <Plug>CommentaryUndo :echoerr "Change your <Plug>CommentaryUndo map to <Plug>Commentary<Plug>Commentary"<CR>
107113
108114
if !hasmapto('<Plug>Commentary') || maparg('gc','n') ==# ''
@@ -114,6 +120,8 @@ if !hasmapto('<Plug>Commentary') || maparg('gc','n') ==# ''
114120
nmap cgc <Plug>ChangeCommentary
115121
endif
116122
nmap gcu <Plug>Commentary<Plug>Commentary
123+
imap <C-/> <C-G>u<Plug>CommentaryInsert
124+
imap <C-_> <C-G>u<Plug>CommentaryInsert
117125
endif
118126

119127
" vim:set et sw=2:

0 commit comments

Comments
 (0)