Rahul Savani
Rahul Savani

Reputation: 872

Vim: yank and paste line and maintain column postition

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.

Upvotes: 4

Views: 179

Answers (3)

benjifisher
benjifisher

Reputation: 5112

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 @

Upvotes: 5

romainl
romainl

Reputation: 196566

:set nostartofline

See :help startofline.

Upvotes: 2

FlyingFoX
FlyingFoX

Reputation: 3509

You could record a simple macro like

qamayyP`aq

This writes your command to the register a and lets you replay it with the command @a.

Upvotes: 0

Related Questions