Learn Practice Daily golf Reference

How to split the screen in Vim

Type :vsplit and press ↵ to split the screen side by side, or :split to stack two windows. Add a file name to open that file in the new window. Ctrl-w then h, j, k or l moves between them.

The ninja sliding a paper screen down the middle of a glowing window, the menu board on one side and the price list on the other, a Ctrl-W keycap on the sill.

The Friday specials go up at noon, and menu.txt still has last week's prices. The new ones are in prices.txt. Switching from one file to the other means holding a price in your head long enough to type it. Put the two side by side and you can read one while you fix the other.

Open a second file beside the first

With menu.txt open in Normal mode, type :vs prices.txt and press ↵. The screen splits down the middle. The prices open on the left and the cursor goes with them, and the menu is still there on the right. Each half is a window, Vim's name for a view onto a file. :vs is short for :vsplit, and the short one is all you need.

:sp prices.txt (short for :split) stacks the two instead, with the new window on top. Leave the file name off and you get the file you're in twice. That helps in a long file, where you can keep the top in view while you work further down. It's still one file, so whatever you type shows up in both windows.

Practise the keys

Split the screen

Type each command the short way, as you would in Vim. Nothing here splits anything.

Which keys do it?
You're editing menu.txt. Open prices.txt beside it, side by side.
scroll here, then press it
1 of 3
You're editing menu.txt. Open prices.txt beside it, side by side.

Move from one window to another

The window keys all start with Ctrl-w. Hold Ctrl, press W, let go, then press one more key. The cursor is in the prices on the left, so Ctrl-wl takes it right, into the menu, and Ctrl-wh brings it back. When the windows are stacked, Ctrl-wj goes down and Ctrl-wk goes up. They're the same four keys that move the cursor.

Ctrl-ww goes to the next window whichever way it is, and from the last window it wraps round to the first. With two windows open it just hops back and forth. Each window keeps its own cursor, and :w saves the file in the window you're in.

Splitting has keys too. Ctrl-wv splits side by side and Ctrl-ws splits top and bottom, both showing the file you're in. To open a different file in the new window, use :vs or :sp with its name.

Close a window

:q closes the window the cursor is in, and the others grow to fill the space. It only quits Vim when it's the last window left. Ctrl-wq does the same as :q.

When the prices are all fixed and you want the menu back on its own, go to the menu's window and type :only, or press Ctrl-wo. Every other window closes and this one fills the screen. :close (or Ctrl-wc) is a :q that never quits Vim. In the last window it refuses with E444: Cannot close last window.

Practise the keys

Close windows

Type the commands. The cards leave out the Ctrl-w keys, because on Windows and Linux a browser takes Ctrl-W to close the tab.

Which keys do it?
You've copied the last price over. Close the window you're in.
scroll here, then press it
1 of 3
You've copied the last price over. Close the window you're in.

Make a window bigger or smaller

Ctrl-w= makes every window the same size again, which is also the fix when one ends up squashed. Ctrl-w_ makes the window you're in as tall as it can go, and Ctrl-w| as wide, leaving the others a line or a column thin. Put a number first to pick the size, so 10Ctrl-w_ makes it 10 lines tall.

The commands take numbers too. :resize 10 sets the height to 10 lines, and :resize +5 or :resize -5 changes it by five. Widths go through :vertical resize, as in :vertical resize 40. With the mouse on, as it is in Neovim, you can also drag the line between two windows.

Practise the keys

Resize a window

Spell the commands out in full, :resize rather than :res.

Which keys do it?
Make the window you're in 10 lines tall.
scroll here, then press it
1 of 3
Make the window you're in 10 lines tall.

Start Vim with the files already split

vim -O menu.txt prices.txt opens the two files side by side from the start, and vim -o menu.txt prices.txt stacks them. The cursor starts in the first file. nvim takes the same flags.

New windows open on the left and on top. If you'd rather they opened on the right and below, as in most other apps, put set splitright splitbelow in your config. Your Vim config, line by line shows where it goes.

Keys or commandWhat it does
:vs file or :vsplit fileSplit side by side and open the file in the new window, on the left. Without a name, show this file twice.
:sp file or :split fileSplit top and bottom, with the new window on top.
Ctrl-wv / Ctrl-wsSplit side by side / top and bottom, showing the file you're in.
:vnew / :newSplit with a new empty file in the new window.
Ctrl-wh Ctrl-wj Ctrl-wk Ctrl-wlMove to the window on the left, below, above or on the right.
Ctrl-ww or Ctrl-wCtrl-wMove to the next window, wrapping round to the first.
Ctrl-wpGo back to the window you were in before.
:q or Ctrl-wqClose this window. In the last one, quit Vim.
:close or Ctrl-wcClose this window, but never the last one.
:only or Ctrl-woClose every window except this one.
Ctrl-w=Make all the windows the same size.
Ctrl-w_ / Ctrl-w|Make this window as tall / as wide as it goes. With a count first, that many lines or columns.
Ctrl-w+ / Ctrl-w-One line taller / shorter. A count first for more.
Ctrl-w> / Ctrl-w<One column wider / narrower. A count first for more.
:resize 10 / :resize +5Set the height to 10 lines / make it five lines taller.
:vertical resize 40Set the width to 40 columns.
vim -O a b / vim -o a bStart Vim with the files side by side / stacked.