Vim splits and buffers: two files side by side
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
| Command | Does |
|---|---|
vim -O a b | open two files side by side (-o stacks them) |
:vsplit file | split the window side by side, with another file in the new half |
:split file | split it top and bottom |
:wincmd w | go to the next window |
:wincmd h / l / j / k | go to the window on the left, right, below or above |
:q | close this window (the others stay) |
:only | close all the others |
:wqa | save 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.
| Command | Does |
|---|---|
:ls | list the buffers, with numbers |
:b 2 or :b notes | show buffer 2 (or the one whose name matches) |
:bn / :bp | the 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.
Your task
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.
- Two windows side by side (or stacked) (not done yet)
- The two missing settings are in .env, which keeps its own values, saved (not done yet)
The checklist ticks itself off as you work in the terminal: any way that gets the result counts.
Hints come one at a time, then one way to do it. Try each before the next.
What this lesson uses, in one place.
:vsplit- Vim: split the window side by side (:split stacks them), optionally with another file in the new one
:wincmd- Vim: the window commands of Ctrl-w, typed out: :wincmd w goes to the next window, h j k l to a neighbour
:ls- Vim: list the open buffers (files); :b 2 or :bn switches to another
:wqa- Vim: save every changed file and quit
Every everyday command on one page: the Linux commands cheat sheet.