Closed ranelpadon closed 2 years ago
Haven't tested your function, but you should be able to skip the gv
and call '<,'>Commentary
directly. Those marks continue to work after you leave visual mode.
@tpope You're right, I could simplify the script with that. However, copy '>
still seems to have quirks when called programmatically (i.e. it duplicated the lines but in reverse order). But your answer gave me hints, and came up with a simple mapping and avoids the need for custom script. This works as intended:
vnoremap <Leader>D :copy '><CR> :'<,'>Commentary<CR>
Thanks a lot. :)
@tpope You're right, I could simplify the script with that. However,
copy '>
still seems to have quirks when called programmatically (i.e. it duplicated the lines but in reverse order).
There are no quirks. Hint: there's a difference between calling '<,'> copy '>
once and calling copy '>
over and over again for each line.
vnoremap <Leader>D :copy '><CR> :'<,'>Commentary<CR>
That space is part of the map, and you don't want that.
Cleaned up version:
xnoremap <Leader>D :copy '><Bar>'<,'>Commentary<CR>
Thanks a lot. :)
;)
Right, forgot about the <Bar>
, and good point on the extraneous space. Yeah, I noticed this also when I tried using the
'<,'> copy '>
inside the script: the N lines are duplicated as intended, but repeated N times also.
There are no quirks. Hint: there's a difference between calling '<,'> copy '> once and calling copy '> over and over again for each line.
Is there a simple way though to execute the '<,'> copy '>
correctly inside the script?
Is there a simple way though to execute the
'<,'> copy '>
correctly inside the script?
Yes. Call the function once rather than N times. You might want <C-U>
to remove the usual '<,'>
.
Right, I found a related answer:
So, to execute the function once, there are 2 options:
range
at the function definition<C-U>
to clear the selectionI chose the first approach, and it works now as intended!
function AutoCommentDuplicatedLines() range
" Duplicate the selected lines.
'<,'>copy '>
" Comment the previously selected lines.
'<,'>Commentary
endfunction
This is bothering me for some time already, so I could live peacefully now. Answered also my post in Vim StackExchange. Thanks again for your pointers, appreciate it. :)
One of my common workflows while experimenting on various code fixes/implementations is:
So, I do this in Vim by doing these:
S-v
/Visual Lines)vnoremap <Leader>d :copy '><CR>
gv
selected linesgc
(:Commentary
)So, if I have these initial lines:
After executing those 4 steps above I'll have this, which is what I want:
However, the 4 steps above are repetitive and annoying. Ideally, I should only do Step 1 and the rest should be automated. So, I try to implement the Steps 2-4 via script, but it doesn't work as expected. Step 1 (i.e. lines selection) should be done manually by the user:
Looks like Step 3 and 4 should be in 1 line, but this doesn't work as well
Results Expected
Actual
Am I missing something? Thanks