简体   繁体   中英

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.

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 @
:set nostartofline

请参阅:help startofline

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM