styles, however; so if you want to, you can customize the
indentation style with the 'cinoptions' option.
By default 'cinoptions' is empty and Vim uses the default style. You can
add various items where you want something different. For example, to make
curly braces be placed like this:
if (flag) ~
{ ~
i = 8; ~
j = 0; ~
} ~
Use this command: >
:set cinoptions+={2
There are many of these items. See |cinoptions-values|.
==============================================================================
*30.3* Automatic indenting
You don't want to switch on the 'cindent' option manually every time you edit
a C file. This is how you make it work automatically: >
:filetype indent on
Actually, this does a lot more than switching on 'cindent' for C files. First
of all, it enables detecting the type of a file. That's the same as what is
used for syntax highlighting.
When the filetype is known, Vim will search for an indent file for this
type of file. The Vim distribution includes a number of these for various
programming languages. This indent file will then prepare for automatic
indenting specifically for this file.
If you don't like the automatic indenting, you can switch it off again: >
:filetype indent off
If you don't like the indenting for one specific type of file, this is how you
avoid it. Create a file with just this one line: >
:let b:did_indent = 1
Now you need to write this in a file with a specific name:
{directory}/indent/{filetype}.vim
The {filetype} is the name of the file type, such as "cpp" or "java". You can
see the exact name that Vim detected with this command: >
:set filetype
In this file the output is:
filetype=help ~
Thus you would use "help" for {filetype}.
For the {directory} part you need to use your runtime directory. Look at
the output of this command: >
set runtimepath
Now use the first item, the name before the first comma. Thus if the output
looks like this:
runtimepath=~/.config/nvim,/usr/local/share/vim/vim60/runtime,~/.config/nvim/after ~
You use "~/.config/nvim" for {directory}. Then the resulting file name is:
~/.config/nvim/indent/help.vim ~
Instead of switching the indenting off, you could write your own indent file.
How to do that is explained here: |indent-expression|.
==============================================================================
*30.4* Other indenting
The simplest form of automatic indenting is with the 'autoindent' option.
It uses the indent from the previous line. A bit smarter is the 'smartindent'
option. This is useful for languages where no indent file is available.
'smartindent' is not as smart as 'cindent', but smarter than 'autoindent'.
With 'smartindent' set, an extra level of indentation is added for each {
and removed for each }. An extra level of indentation will also be added for
any of the words in the 'cinwords' option. Lines that begin with # are
treated specially: all indentation is removed. This is done so that
preprocessor directives will all start in column 1. The indentation is
restored for the next line.
CORRECTING INDENTS
When you are using 'autoindent' or 'smartindent' to get the indent of the
previous line, there will be many times when you need to add or remove one
'shiftwidth' worth of indent. A quick way to do this is using the CTRL-D and
CTRL-T commands in Insert mode.
For example, you are typing a shell script that is supposed to look like
this:
if test -n a; then ~
echo a ~
echo "-------" ~
fi ~
Start off by setting these options: >
:set autoindent shiftwidth=3
You start by typing the first line, <Enter> and the start of the second line:
if test -n a; then ~
echo ~
Now you see that you need an extra indent. Type CTRL-T. The result:
if test -n a; then ~
echo ~
The CTRL-T command, in Insert mode, adds one 'shiftwidth' to the indent, no
matter where in the line you are.
You continue typing the second line, <Enter> and the third line.