Skip to content

Vim splits and buffers: two files side by side

NinjaLesson 56 of 668 min

Open two files side by side in Vim with :vsplit or vim -O, move between windows with :wincmd, copy between them, and juggle open files as buffers.

The app won't start, and the error says a setting is missing. The settings it expects are listed in .env.example; yours are in .env. Comparing them in one window means scrolling up and down and remembering things. Put them side by side.

Windows

CommandDoes
vim -O a bopen two files side by side (-o stacks them)
:vsplit filesplit the window side by side, with another file in the new half
:split filesplit it top and bottom
:wincmd wgo to the next window
:wincmd h / l / j / kgo to the window on the left, right, below or above
:qclose this window (the others stay)
:onlyclose all the others
:wqasave every changed file and quit

In Vim itself, every :wincmd has a shortcut: Ctrl-w and then the same letter (Ctrl-w w, Ctrl-w l). But on Windows and Linux, the browser keeps Ctrl+W for itself and closes the tab, so in this practice Vim type :wincmd instead (on a Mac Ctrl+W reaches Vim; on a phone use the Ctrl key above the keyboard). In your own Vim you can give it a key that browsers leave alone, such as nnoremap <Tab> <C-w>w in your .vimrc.

Yanks and puts work across windows: yank in one, move, put in the other.

Buffers

Every file Vim has open is a buffer, shown in a window or not. :e notes.txt opens another file in this window, and the first one stays open in the background.

CommandDoes
:lslist the buffers, with numbers
:b 2 or :b notesshow buffer 2 (or the one whose name matches)
:bn / :bpthe next or the previous buffer

Vim won't hide a buffer with unsaved changes unless you save first or add !. Neovim does it by default.

Your turn

Open app/.env and app/.env.example side by side, copy the two settings that .env is missing from the example into .env (keep .env’s own values), and save.

Next