Vim Indentation and Code Formatting — >>, <<, =, gq

Format and indent code in Vim using >>, <<, = for auto-indent, gq for text wrapping, and Visual mode indentation. Configure tabstop, shiftwidth, and expandtab settings.

Editing

Detailed Explanation

Indentation Commands

Manual Indentation

Command Action
>> Indent current line
<< Unindent current line
>{motion} Indent lines covered by motion
<{motion} Unindent lines covered by motion
>ip Indent inner paragraph
3>> Indent 3 lines

Visual Mode Indentation

  1. Select lines with V + movement
  2. > to indent, < to unindent
  3. . to repeat the last indent

Auto-Indent

Command Action
= Auto-indent (with motion)
== Auto-indent current line
=ip Auto-indent inner paragraph
gg=G Auto-indent entire file
=i{ Auto-indent inside curly braces

Text Formatting

Command Action
gq{motion} Format text to textwidth
gqip Format current paragraph
gqq Format current line
gw{motion} Format without moving cursor

Indentation Settings

:set tabstop=4       " Display width of tab character
:set shiftwidth=4    " Width of auto-indent
:set expandtab       " Use spaces instead of tabs
:set autoindent      " Copy indent from current line on new line
:set smartindent     " Smart auto-indentation for C-like languages

The Retab Command

:retab converts between tabs and spaces based on your current settings. :retab! forces the conversion even for existing indentation.

Use Case

You need to fix indentation in code, auto-format blocks of text, or configure Vim's indentation settings to match your project's coding style.

Try It — Vim Cheat Sheet

Open full tool