vimtricks.wiki Concise Vim tricks, one at a time.

How do I enable automatic indentation?

Answer

:set autoindent

Explanation

The autoindent option copies the indentation from the current line when starting a new line. This maintains consistent indentation while typing.

How it works

  • :set autoindent copies indent from current line to new line
  • Pressing o, O, or <CR> in insert mode inherits indentation
  • Works independently of language-specific indentation

Example

With autoindent enabled and the cursor on an indented line:

    current indented line
    |  <- new line starts here after pressing Enter

Tips

  • smartindent adds one level after { and removes after }
  • cindent provides C-language-aware indentation
  • filetype indent on enables filetype-specific indentation (recommended)
  • indentexpr is the most flexible indentation method
  • Most users want :filetype indent on rather than manual settings

Next

How do you yank a single word into a named register?