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

[Feature request] NERDCommenterDuplicate command #435

Open
nwaywood opened this issue Sep 11, 2020 · 7 comments
Open

[Feature request] NERDCommenterDuplicate command #435

nwaywood opened this issue Sep 11, 2020 · 7 comments
Labels

Comments

@nwaywood
Copy link

nwaywood commented Sep 11, 2020

A command that in addition to yanking (like the NERDCommenterYank) command also pastes the yanked code below the commented out version.

For example:

Before running NERDCommenterDuplicate command

some 
code
block

After running NERDCommenterDuplicate command with the three lines selected in visual mode

// some
// code
// block
some
code
block

Obviously is you are in normal mode and not visual mode then it would just yank, comment and paste the current line.

I hope you like the idea!

@nwaywood nwaywood changed the title [Feature request] NERDCommenterYankAndPaste command [Feature request] NERDCommenterDuplicate command Sep 11, 2020
@alerque
Copy link
Member

alerque commented Sep 11, 2020

This sounds pretty workflow specific, and all the pieces are there already. Why not just add yourself a map that first calls the Yank function, then pastes it? This is what VIM is really good at: composability. VIM and plugins provide building blocks of specific actions, you compose them into whatever sequences you want to run.

@nwaywood
Copy link
Author

nwaywood commented Sep 11, 2020

@alerque Thanks for the suggestion :)

I tried that already but without much success. Please note that I am not a Vimscript expert so I apologize in advance if I'm doing something stupid.

I wrote the following code in my init.vim to try and do what I want:

nnoremap gC :call Test()<cr>
function! Test()
   :call NERDComment('n', 'yank')
   exec 'normal! p'
endfunc

xnoremap gC :call Test2()<cr>
function! Test2()
   :call NERDComment('x', 'yank')
   exec 'normal! p'
endfunc

The first function/binding for normal mode works correctly but the function/binding for visual mode doesn't work at all. Any idea what I'm doing wrong?

Also, even if the second function/binding did do what I was expecting, I believe it still wouldn't be the desired result. Since after executing the NERDCommenterYank command the cursor is at the top of the commented section, pasting would result in the following:

// some
some
code
block
// code
// block

where the uncommented version gets pasted below the first line instead of below the last line of the commented section.

@alerque
Copy link
Member

alerque commented Sep 11, 2020

For the visual mode example, before running the normal mode command I think you need to jump the cursor to the end of the visual selection and switch to normal mode.

@nwaywood
Copy link
Author

nwaywood commented Sep 11, 2020

@alerque Yes I think that would work if it wasn't for the other issue that I mentioned. If I modify the function so that it jumps to the end of the visual selection like this:

xnoremap gC :call Test2()<cr>
function! Test2()
   :call NERDComment('x', 'yank')
   exec "normal! '>"
   exec 'normal! p'
endfunc

It results in the following code. It seems like the NERDComment('x', 'yank') is doing something funky.

"""some
"""code
"""block
""some
""code
""block
"some
"code
"block
some
code
block

Any idea why this would be happening?

@nwaywood
Copy link
Author

nwaywood commented Sep 12, 2020

@alerque I did some more testing and this code snippet

xnoremap gC :call Test2()<cr>
function! Test2()
   :call NERDComment('x', 'yank')
endfunc

does not even do the same thing as <leader>cy. Am I calling the function NERDComment incorrectly?

@PlasmaHH
Copy link

PlasmaHH commented Apr 7, 2022

I am currently trying to move from feraltogglecommentify to nerdcommenter and after struggling to recreate its functionality with nerdcommenter I came here to open an issue and found this already existing one.

While its correct that vim can easily compose simple things, sometimes it gets a bit more complicated for the average vim users, which are often not well versed in vim script.

The problems I was facing (and could not solve) in replicating the functionality of C-c of feraltogglecommentify are:

  • for the visual case I ran into the same duplication problem as mentioned by nwaywood
  • This pollutes the yank registers and subsequently pollutes YankRing plugin history

Additionally I was hoping that with this I could find a solution that would not even clobber the "" register.

Therefore it would really be nice if this could be a done as a plugin feature.

@alerque alerque added the feature label Apr 7, 2022
@alerque
Copy link
Member

alerque commented Apr 7, 2022

I would consider a PR if somebody submits a function that copies to a non-default register, pastes a copy, comments one of them, then leaves the cursor at a reasonable location.

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

No branches or pull requests

3 participants