How do I reindent a block of code using a visual selection?
= (in visual mode)
After making a visual selection, pressing = applies Vim's auto-indent to every selected line at once.
= (in visual mode)
After making a visual selection, pressing = applies Vim's auto-indent to every selected line at once.
[p
When you copy code from one indentation level and paste it at another, p preserves the original indentation, leaving your code misaligned.
=i{
When editing code with messy indentation — after a paste, a merge conflict, or a refactor — you often need to fix just one block rather than the entire file
<C-r><C-o>{reg}
When you use a in insert mode to paste register a, Vim inserts the text as if you typed it character by character.
<C-t> and <C-d> in insert mode
When typing in insert mode, you can adjust the current line's indentation without leaving to normal mode.
<C-t> and <C-d>
When you're typing in insert mode and realize the current line needs more or less indentation, you don't have to leave insert mode to fix it.
>gv
Normally, pressing > in visual mode indents the selection but exits visual mode, requiring you to press gv to reselect.
:'<,'>retab!
The :retab! command converts between tabs and spaces based on your expandtab setting.
^ vs 0
Vim has two distinct motions for moving to the start of a line: 0 goes to column 1 (the absolute start), while ^ goes to the first non-blank character.
navigation #navigation #motions #line-navigation #indentation
:set virtualedit=block
By default, Vim's visual block mode () is limited by line length — if some lines are shorter than others, the block selection gets ragged.
:set breakindent
When wrap is enabled, long lines wrap to the next screen row starting at column 1 by default, which makes indented code look messy.
:set linebreak
By default, when wrap is enabled, Vim wraps long lines at the window edge — which can split words in the middle.
autocmd FileType python setlocal expandtab shiftwidth=4
Vim's autocmd FileType lets you apply settings automatically whenever a specific file type is detected.
<C-t> / <C-d>
While in Insert mode, you can adjust indentation without switching back to Normal mode.
qa:s/^ / /\njq
Record a macro that substitutes leading 4-space indentation with 2 spaces on each line.
]p
The ]p command pastes text and adjusts its indentation to match the current line.
:set autoindent
The autoindent option copies the indentation from the current line when starting a new line.
>
In visual mode, pressing > indents all selected lines by one shiftwidth.
==
The == command auto-indents the current line based on the surrounding context.
> and <
How it works In visual mode, you can shift selected lines to the right or left using the > and to indent them or or shifts the selected lines one shiftwidth to