Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim: yank and paste line and maintain column postition

Tags:

vim

Background: I am editing a reStructuredText table in vim. I would like to yank a line and paste it. The line only contains cell vertical delimiters (|) so this operation corresponds to giving an existing row one more line of space in the source, but doesn't alone affect the output. A simple yyP or yyp, puts the cursor to column 1 after the operation.

Q: Is there an easy way to "yank and paste a line" and keep the cursor in the same column after the operation as before it?

After I wrote the question, it dawned on me to use a mark, and indeed that works: I can do mayyP and then `a to jump back to the desired column. That's a bit long though. So the question is, can I do this with less keystrokes?

Edit: As Shahbaz rightly points out, I can just write an alias, now I know how to do what I want. I am still interested in any shorter way that uses standard commands, in case I am missing some functionality that I should know about.

like image 976
Rahul Savani Avatar asked Mar 22 '23 00:03

Rahul Savani


1 Answers

As @romainl says, you should :set nostartofline (or :set nosol for short). Then, instead of yyp, use the :copy command:

:copy .
:copy -

If :copy is too long, you can use :co or :t. If you do not use any ex commands in between, then you can repeat the command with @: and then with @@.

:help :copy
:help @:
:help @
like image 89
benjifisher Avatar answered Apr 01 '23 15:04

benjifisher