April 11, 2025

Copy between files using split screens

Lately I have been doing a lot of work rearranging a large piece of software. I often find myself wanting to move a range of lines from one file to another. What I do currently is to save the lines to a temporary file. I typically use "z" or "zz" for a filename. Then I exit vim, open the target file, pull the "zz" file in, then I exit vim again and delete the "zz" file (or just leave it laying around).

Can I do all of this from within vim?

The first way

This way just involves starting with the first file and yanking the lines. Then use ":e" to open the second file and put the lines where you want them. Save that file, then use :e to get back to the original file. No split window business, but you do have to remember and type filenames. Most important, nothing new for me to learn.

See "the fifth way" below for a way to make this nicer.

The second way

This way eases you into split windows. Start with this command:

vi -o aaa bbb
This fires up vim with a split window. The file "aaa" is up top and "bbb" is below. By default it starts you out up top in file "aaa", so yank or delete some lines there. Then use Ctrl-W "down" to switch the the lower window. On my machine "down" is the down arrow key on my huge keyboard. To get back to the upper window, I use Ctrl-W "up".

Even easier, type Ctrl-W Ctrl-W (yes, type it twice) and that takes you to the "other" split.

And perhaps easier type Ctrl-W hjkl (selecting hjkl as appropriate to move up/left/...)

The third way

Suppose I am in vim and I see the lines I want to move. I didn't have the foresight to lauch vim in the way described above (and I doubt if I ever will or would).

Type ":sp file" to bring "file" in in a new split. The new guy will go up top. Then you are all set up to do things as per the "second way" above.

In lieu of using the ":sp" command you can type Ctrl-W s to split the current window. This gives you the current file in both splits. Then use ":e" to pull in the file you want into one of the splits.

The fourth way

I find I like this better than the above. Use ":vsp" rather than ":sp". This gives you a vertical split (the splits are side by side rather than above one another). Especially with a big monitor and vim running in a wide window this really is better than one above the other.

To really make this the nicest solution, use "visual mode" and delete the lines from the source file. Use "V" to lauch visual mode, the "j" key (or whatever) to go to the end of the region you want to move.

The fifth way

No split windows for this one. Suppose you are working on file "aaa" and want to copy some lines to "bbb". First yank the lines. Then do ":e bbb" and deposit the lines into "bbb". Save the changes. Then use ":b 1" to return to aaa!! You now have both aaa and bbb in different buffers. Did you know that the :e command did that? You can use :b 1 and :b 2 to move back and forth (watch the filename in the status line in the bottom of the screen).

And tabs for a future time

Vim also supports multiple tabs containing files. This is worth looking into and could be useful for a project with multiple files. You could load all the files into vim and jump between them using tabs. Perhaps.

If you are curious, type :tabnew bbb and you will see vim create tab names at the top of the screen. Moving around is best done by adding some key mappings to your .vimrc and all of that is getting beyond what I intended to deal with in this page, but it is another way to accomplish this.


Have any comments? Questions? Drop me a line!

Tom's vim pages / tom@mmto.org