[Feature request] NERDCommenterDuplicate command
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!
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.
@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.
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.
@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?
@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?
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.
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.