Skip to content
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

Add duplicate (yank, comment, and put) operation. #42

Open
wants to merge 5 commits 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
20 changes: 20 additions & 0 deletions doc/commentary.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,26 @@ gcu
*:Commentary*
:[range]Commentary Comment or uncomment [range] lines

In addition, you can yank a section of text before commenting it out:

gcy{motion} Yank and then comment or uncomment the lines that
{motion} moves over.

gcy Yank and then comment or uncomment [count] lines.

{Visual}gcy Yank and then comment or uncomment the highlighted
lines.

You can also duplicate a section of text before commenting it out:

gcd{motion} Duplicate and then comment or uncomment the lines that
{motion} moves over.

gcd Duplicate and then comment or uncomment [count] lines.

{Visual}gcd Duplicate and then comment or uncomment the highlighted
lines.

The |User| CommentaryPost autocommand fires after a successful operation and
can be used for advanced customization.

Expand Down
50 changes: 50 additions & 0 deletions plugin/commentary.vim
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,54 @@ if !hasmapto('<Plug>Commentary') || maparg('gc','n') ==# ''
nmap gcu <Plug>Commentary<Plug>Commentary
endif

if !get(g:, "commentary_disable_dupe_and_comment_mappings", 0)

function! s:setcommentaryreg(reg)
let s:targetreg = a:reg
endfunction

function! s:yankandcomment(type,...)
" only linewise operations make sense (to me, at least)
" so I am ignoring `type`
if a:0
let [mark1, mark2] = [a:type, a:1]
let reg = a:2
else
let [mark1, mark2] = ["'[", "']"]
let reg = get(s:, "targetreg", '"')
endif
execute 'normal! ' . mark1 . '"' . reg . 'y' . mark2 . ']'
call <SID>go(line(mark1),line(mark2))
execute 'normal! ' . mark1
endfunction

function! s:yankcommentpaste(type,...)
if a:0
let [mark1, mark2] = [a:type, a:1]
else
let [mark1, mark2] = ["'[", "']"]
endif
let savereg = @"
execute "normal " . mark1 ."gcy" . mark2 . "]"
execute "normal! " . mark2 . "p" . mark1
let @" = savereg
endfunction

xnoremap <silent> <Plug>CommentaryYank :<C-U>call<SID>yankandcomment("'<", "'>", v:register)<CR>
nnoremap <silent> <Plug>CommentaryYank :<C-U>call <SID>setcommentaryreg(v:register)<CR>:set opfunc=<SID>yankandcomment<CR>g@
nnoremap <silent> <Plug>CommentaryYankLine :<C-U>call <SID>setcommentaryreg(v:register)<CR>:set opfunc=<SID>yankandcomment<Bar>exe 'norm! 'v:count1.'g@_'<CR>
xnoremap <silent> <Plug>CommentaryDupe :<C-U>call<SID>yankcommentpaste("'<", "'>", v:register)<CR>:normal! '>j<CR>
nnoremap <silent> <Plug>CommentaryDupe :<C-U>call <SID>setcommentaryreg(v:register)<CR>:set opfunc=<SID>yankcommentpaste<CR>g@
nnoremap <silent> <Plug>CommentaryDupeLine :<C-U>call <SID>setcommentaryreg(v:register)<CR>:set opfunc=<SID>yankcommentpaste<Bar>exe 'norm! 'v:count1.'g@_'<CR>
xnoremap <silent> <Plug>Commentary :<C-U>call <SID>go(line("'<"),line("'>"))<CR>

xmap gcy <Plug>CommentaryYank
nmap gcy <Plug>CommentaryYank
nmap gcyy <Plug>CommentaryYankLine
xmap gcd <Plug>CommentaryDupe
nmap gcd <Plug>CommentaryDupe
nmap gcdd <Plug>CommentaryDupeLine

endif

" vim:set et sw=2: