Learn Practice Daily golf Reference

How to indent lines in Vim

In Normal mode, press >> to indent the current line and << to outdent it. A count shifts more lines: 3>> indents this line and the two below. Or select lines with V and press >.

Try it here

Indent two blocks

The bodies of both if blocks lost their indent. The cursor is on the first body line: 3>> indents all three. Then go down to stop(); and press . to repeat the same indent there.

1 of 2Indent three lines with 3>>
1if (ready) {
2start();
1log('started');
2notify('ready');
3}
4if (done) {
5stop();
6log('stopped');
7notify('done');
8}
NORMALLoading the editor…1/20.0s2:1

This editor needs a keyboard. Read on, and play it at your computer.

KeysWhat it does
>> / <<indent / outdent the current line by one level
3>> / 3<<indent / outdent this line and the two below
>ipindent the paragraph (the lines between blank lines)
>} / >Gindent to the next blank line / to the end of the file
V, select, >indent the selected lines; 3> instead indents them three levels
.repeat the last indent, on as many lines as before
== / =ipre-indent the line / paragraph to match the code around it
gg=Gre-indent the whole file
Ctrl-t / Ctrl-d (Insert mode)indent / outdent the current line while typing
:5,12> ↵indent lines 5 to 12 (:5,12< outdents)

> shifts, = re-indents

> and < are operators: they shift lines by a fixed step, whatever is in them. >> is the line form, and any motion works after a single >: >ip, >j, >% on a brace for the whole block. In Visual mode, > shifts the selection and then ends it; press . to shift the same lines again, or gv to select them again.

= is different: it works out the right indent for each line from the code around it (using the indent rules for the file type) and applies it. So =ip fixes a pasted paragraph and gg=G re-indents the whole file. In Vim, that needs filetype plugin indent on in your vimrc (the default vimrc, defaults.vim, turns it on; Neovim has it on by default).

Choosing the width

One indent level is shiftwidth columns. Most code uses 2 or 4 spaces, so add set shiftwidth=4 expandtab to ~/.vimrc (or vim.opt.shiftwidth = 4 and vim.opt.expandtab = true in Neovim's init.lua). Language plugins often set their own: Python files, for example, get 4. If a file already mixes tabs and spaces, :retab converts the tabs to spaces once expandtab is on.