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 >.
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.
This editor needs a keyboard. Read on, and play it at your computer.
| Keys | What it does |
|---|---|
| >> / << | indent / outdent the current line by one level |
| 3>> / 3<< | indent / outdent this line and the two below |
| >ip | indent the paragraph (the lines between blank lines) |
| >} / >G | indent 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 |
| == / =ip | re-indent the line / paragraph to match the code around it |
| gg=G | re-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.