Auto and Smart Indenting
Vim offers four increasingly complex and powerful methods
to automatically indent text. In its simplest form, Vim behaves almost
identically to vi’s autoindent option, and indeed it uses the
same name to describe the behavior.
You can choose the indentation method simply by specifying it in
a :set command, such as:
:set cindent
Vim offers the following methods, listed in order of increasing sophistication:
autoindentAuto indentation closely mimics
vi’sautoindent. It differs subtly as to where the cursor is placed after indentation is deleted.smartindentSlightly more powerful than
, but it recognizes some basic C syntax primitives for defining indentation levels.autoindentcindentAs its name implies,
cindentembodies a much richer awareness of C syntax and introduces sophisticated customization beyond simple indentation levels. For example,cindentcan be configured to match your (or your boss’s) favorite coding style rules, including but not limited to how braces ({}) indent, where braces are placed, whether either or both braces are indented, and even how indentation matches included text.indentexprLets you define your own expression, which Vim evaluates in the context of each new line you begin. With this feature, you write your own rules. We refer you to the discussions of scripting and functions in this book and to the Vim documentation for details. If the other three options don’t give you enough flexibility for automatic indentation,
indentexprcertainly ...