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

Conversation

jeetsukumaran
Copy link

A common operation is for a section of text to be duplicated and
commented out, so that it can be edited/refactored/reworked, while the
original is still available for reference until the new version is
finalized or otherwise ready for commit. This duplicate + comment
operation requires the target text to be specified twice: once for the
yanking, and then again for the commenting-out. This patch makes this
operation much easier by combining the yanking, commenting, and pasting
actions to act on the same target.

Notes:

(1) I am somewhat equivocal on the choice of the key-mapping (gy). Please feel free to change if something better occurs to you, assuming you accept the patch.

(2) I cannot see a use-case where this operation would not be linewise. It should be easy to enough to fix if there is a need/demand for characterwise or blockwise operations.

@tpope
Copy link
Owner

tpope commented Mar 15, 2015

Can you ditch the duplicate-and-comment documentation (at least temporarily) and squash everything else down to a single commit?

Support for yanking a section of text before commenting them out. By
default, mapped to '`gcy`'.
@jeetsukumaran
Copy link
Author

Ok, done.

@tpope
Copy link
Owner

tpope commented Mar 15, 2015

Actually if it's not too much trouble, could you add a second commit restoring the duplicate functionality on gcd? I will play with both for a few days and see what I find most useful.

    Support for duplicating a section of text before commenting them out. By
    default, mapped to '`gcd`'.
@jeetsukumaran
Copy link
Author

And ... done!

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Won't this cause a pause since <Plug>CommentaryDupeLine overlaps <Plug>CommentaryDupe? (solution would be to wrap in parentheses, e.g., <Plug>(CommentaryDupeLine) and <Plug>(CommentaryDupe))

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. In practice, however, it may not be an issue? In normal
mode, where the conflict arises, <Plug>CommentaryDupe takes a motion,
and so there is no pause (except if you count Vim waiting for you to
complete the operator motion). So, you will always be typing either
gcdd or gcd{motion}`. Still, it makes sense to code defensively here
and wrap the names with parentheses, but that will break the convention
with the rest of Plug's, so I am going to wait and see if (a) @tpope
accepts this patch in the first place, and (b) if he amenable to
modifying all the Plug names.

On 3/19/15 6:03 PM, Justin M. Keyes wrote:

In plugin/commentary.vim
#42 (comment):

@@ -87,6 +126,12 @@ if !hasmapto('Commentary') || maparg('gc','n') ==# ''
nmap gcc CommentaryLine
nmap cgc ChangeCommentary
nmap gcu CommentaryCommentary

  • xmap gcy CommentaryYank
  • nmap gcy CommentaryYank
  • nmap gcyy CommentaryYankLine
  • xmap gcd CommentaryDupe
  • nmap gcd CommentaryDupe

Won't this cause a pause since |CommentaryDupeLine| overlaps
|CommentaryDupe|? (solution would be to wrap in parentheses, e.g.,
|(CommentaryDupeLine)| and |(CommentaryDupe)|)


Reply to this email directly or view it on GitHub
https://github.com/tpope/vim-commentary/pull/42/files#r26802462.


Jeet Sukumaran

jeetsukumaran@gmail.com

Blog/Personal Pages:
http://jeetworks.org/
GitHub Repositories:
http://github.com/jeetsukumaran
Photographs (as stream):
http://www.flickr.com/photos/jeetsukumaran/
Photographs (by galleries):

http://www.flickr.com/photos/jeetsukumaran/sets/

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am indeed adopting the parentheses convention for new plugins, but I will not be updating old plugins unless there's a concrete issue.

@airblade
Copy link

I know this is 4 years old but I would love to see this (or similar) merged.

@justinmk
Copy link

justinmk commented Aug 30, 2019

@airblade are you aware of '] and '[? One can already yank and then comment using those marks. E.g. to yank 2 lines then comment those same lines:

yj
gc']

@airblade
Copy link

@justinmk Thanks for the info – yes, I know about '[ and '] but I feel gcd{motion} et al is neater than yanking, pasting, and commenting as three separate activities. I could cobble it together in my vimrc but it would be nice were it built in.

@nwaywood
Copy link

nwaywood commented Sep 9, 2020

@tpope can this please be merged?

@evanthegrayt
Copy link

evanthegrayt commented Oct 19, 2020

I would also still like this feature. I'd also prefer an option that didn't automatically put it -- just yank. Sometimes I want the duplicated code to be in a specific place, so having it auto-put might be more of a hassle than having it yank, and then manually putting it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants