I have two files, say a.txt and b.txt, in the same session of vim and I split the screen so I have file a.txt in the upper window and b.txt in the lower window.

I want to move lines here and there from a.txt to b.txt: I select a line with Shift+v, then I move to b.txt in the lower window with Ctrl+w , paste with p, get back to a.txt with Ctrl+w and I can repeat the operation when I get to another line I want to move.

My question: is there a quicker way to say vim "send the line I am on (or the test I selected) to the other window"?

  • Use q macro? q[some_letter] [whatever operations] q, then call the macro with [times to be called]@q – Chong Nov 24 '15 at 12:33
  • I presume that you're deleting the line that you've selected in a.txt. If not, you'd be pasting something else into b.txt. If so, there's no need to select the line first. – Anthony Geoghegan Nov 24 '15 at 13:00
up vote 5 down vote accepted

This sounds like a good use case for a macro. Macros are commands that can be recorded and stored in a Vim register. Each register is identified by a letter from a to z.

Recording

From Recording keys for repeated jobs - Vim Tips

To start recording, press q in Normal mode followed by a letter (a to z). That starts recording keystrokes to the specified register. Vim displays “recording” in the status line. Type any Normal mode commands, or enter Insert mode and type text. To stop recording, again press q while in Normal mode.

For this particular macro, I chose the m (for move) register to store it.

I pressed qm to record the following commands:

  • dd to delete the current line (and save it to the default register)
  • CtrlWj to move to the window below
  • p to paste the contents of the default register
  • and CtrlWk to return to the window above.

When I typed q to finish recording the macro, the contents of the m register were:

dd^Wjp^Wk

Usage

  • To move the current line, simply type @m in Normal mode.
  • To repeat the macro on a different line, @@ can be used to execute the most recently used macro.
  • To execute the macro 5 times (i.e., move the current line with the following four lines below it), use 5@m or 5@@.
  • I asked to see if there is a command unknown to me that does the job: it seems there is none. In absence of such a command, this can be a good solution. – brad Nov 24 '15 at 14:26
  • @brad, you can find all the commands available to you in the documentation. If it's not there it doesn't exist… no need to ask random strangers. – romainl Nov 26 '15 at 9:54
  • @romainl, yes, I know this but vim documentation is really huge and, although it doesn't scare me, there is always the possibility to miss something. Moreover, it could also be that you can obtain the effect using the combination of 2 commands and in this case it would be hardly documented – brad Nov 26 '15 at 10:17

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.