Reputation: 9632
I've added these shortcuts to duplicate a line based on the :t
command, where :t.
duplicates the current line onto the next line (keeps cursor on the second of the pair of lines), and :t-1
duplicates the current line onto the previous line (keeps cursor on the first of the pair of lines).
nmap <leader>D :t-1<cr>
nmap <leader>d :t.<cr>
In both cases the command moves the cursor position to the beginning of the line that has been created. How can I keep the cursor in the same position it was on (e.g. 20 characters in from the start of the line) on the new line?
Upvotes: 0
Views: 314
Reputation: 446
Easiest solution is to use
:set nostartofline
Makes ex commands retain cursor column when possible, see h:nosol
Upvotes: 2
Reputation: 19849
Here is a solution:
nmap <leader>D mayyp`a
nmap <leader>d mayyP`a
It starts by placing a mark called a
(ma
) on the current cursor position, then copy the current line (yy
) and paste it below (p
). After that, it goes back to the original cursor position with `a
. The second mapping uses P
instead of p
to paste the line above the current one.
This should do what you’re expecting.
Upvotes: 1