How do I fix the indentation of a code block without affecting surrounding code?
=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
=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
:s/^/\=line('.') - line("'<") + 1 . '. '/
When you need to quickly number a set of lines — such as TODO items, steps, or bullet points — you can use a visual selection combined with a substitution e
visual-mode #visual-mode #editing #ex-commands #formatting #substitute
80i-<Esc>
Vim's insert commands accept a count prefix that repeats everything you type.
/\(\<\w\+\>\)\_s\+\1\>
When writing or editing text, repeated words like "the the" or "is is" are a common typo that spell checkers often miss.
:'<,'>!column -t
When working with data that has uneven spacing — such as variable assignments, CSV-like data, or configuration entries — you can select the lines and pipe t
visual-mode #visual-mode #editing #formatting #external-command #alignment
<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.
autocmd FileType python setlocal ts=4 sw=4 et
Using autocmd FileType, you can configure Vim to automatically apply buffer-local settings whenever a file of a particular type is opened.
gq
The gq operator reformats text to fit within your configured textwidth.
<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.
gqap
The gq operator reformats text by wrapping lines to fit within the textwidth setting.
:g/pattern/m $
The :g (global) command combined with :m (move) lets you collect all lines matching a pattern and relocate them to a specific position in the file.
command-line #ex-commands #editing #global #search #formatting
:'<,'>!awk '{printf "%-20s %s\n", $1, $2}'
By piping a visual selection through awk with printf formatting, you can align columns to fixed widths.
visual-mode #visual-mode #formatting #alignment #external-command
:lua require('conform').format()
conform.
:center 80
Vim has built-in text alignment commands that adjust lines relative to a specified width.
:set formatoptions+=cro
The formatoptions setting controls how Vim automatically formats text as you type — including comment continuation, auto-wrapping, and paragraph formatting.
gl{motion}{char}
vim-lion (by Tom McDonald) adds gl and gL as alignment operators.
: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.
:ALEFix
ALE (Asynchronous Lint Engine) is a popular Vim plugin that runs linters and formatters asynchronously in the background, showing errors and warnings in the gut
r<CR>
You can split a line at the cursor without entering Insert mode by using r.