4

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"?

brad
  • 236

2 Answers2

6

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@@.
0

you can just go :set clipboard+=unnamed to use the system clipboard and that spans all vim splits.

I found this out over at this SO question Sharing Vim Yank Register