Skip to content

Commit 976201c

Browse files
committed
Add 'yank-and-comment' operation.
Support for yanking a section of text before commenting them out. By default, mapped to '`gcy`'.
1 parent 9c68513 commit 976201c

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

doc/commentary.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,16 @@ gcu
2929
*:Commentary*
3030
:[range]Commentary Comment or uncomment [range] lines
3131

32+
In addition, you can yank a section of text before commenting it out:
33+
34+
gcy{motion} Yank and then comment or uncomment the lines that
35+
{motion} moves over.
36+
37+
gcy Yank and then comment or uncomment [count] lines.
38+
39+
{Visual}gcy Yank and then comment or uncomment the highlighted
40+
lines.
41+
3242
The |User| CommentaryPost autocommand fires after a successful operation and
3343
can be used for advanced customization.
3444

plugin/commentary.vim

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,24 @@ function! s:textobject(inner) abort
7272
endif
7373
endfunction
7474

75+
function! s:setcommentaryreg(reg)
76+
let s:targetreg = a:reg
77+
endfunction
78+
function! s:yankandcomment(type,...)
79+
" only linewise operations make sense (to me, at least)
80+
" so I am ignoring `type`
81+
if a:0
82+
let [mark1, mark2] = [a:type, a:1]
83+
let reg = a:2
84+
else
85+
let [mark1, mark2] = ["'[", "']"]
86+
let reg = get(s:, "targetreg", '"')
87+
endif
88+
execute 'normal! ' . mark1 . '"' . reg . 'y' . mark2 . ']'
89+
call <SID>go(line(mark1),line(mark2))
90+
execute 'normal! ' . mark1
91+
endfunction
92+
7593
xnoremap <silent> <Plug>Commentary :<C-U>call <SID>go(line("'<"),line("'>"))<CR>
7694
nnoremap <silent> <Plug>Commentary :<C-U>set opfunc=<SID>go<CR>g@
7795
nnoremap <silent> <Plug>CommentaryLine :<C-U>set opfunc=<SID>go<Bar>exe 'norm! 'v:count1.'g@_'<CR>
@@ -80,13 +98,21 @@ nnoremap <silent> <Plug>ChangeCommentary c:<C-U>call <SID>textobject(1)<CR>
8098
nmap <silent> <Plug>CommentaryUndo <Plug>Commentary<Plug>Commentary
8199
command! -range -bar Commentary call s:go(<line1>,<line2>)
82100

101+
xnoremap <silent> <Plug>CommentaryYank :<C-U>call<SID>yankandcomment("'<", "'>", v:register)<CR>
102+
nnoremap <silent> <Plug>CommentaryYank :<C-U>call <SID>setcommentaryreg(v:register)<CR>:set opfunc=<SID>yankandcomment<CR>g@
103+
nnoremap <silent> <Plug>CommentaryYankLine :<C-U>call <SID>setcommentaryreg(v:register)<CR>:set opfunc=<SID>yankandcomment<Bar>exe 'norm! 'v:count1.'g@_'<CR>
104+
105+
xnoremap <silent> <Plug>Commentary :<C-U>call <SID>go(line("'<"),line("'>"))<CR>
83106
if !hasmapto('<Plug>Commentary') || maparg('gc','n') ==# ''
84107
xmap gc <Plug>Commentary
85108
nmap gc <Plug>Commentary
86109
omap gc <Plug>Commentary
87110
nmap gcc <Plug>CommentaryLine
88111
nmap cgc <Plug>ChangeCommentary
89112
nmap gcu <Plug>Commentary<Plug>Commentary
113+
xmap gcy <Plug>CommentaryYank
114+
nmap gcy <Plug>CommentaryYank
115+
nmap gcyy <Plug>CommentaryYankLine
90116
endif
91117

92118
if maparg('\\','n') ==# '' && maparg('\','n') ==# '' && get(g:, 'commentary_map_backslash', 1)

0 commit comments

Comments
 (0)