vim and indenting

Auto-indenting in vim may or may not be helpful depending on the file your editing and the conventions you like or have to follow. This will breifly show a few methods of turning this feature on/off and changing the way vim implements it.

Disabling auto indent for the current file

To see the current indenting settings, and where they were set, enter: :verbose set ai? cin? cink? cino? si? inde? indk? If you are editing a particular file and you want to prevent auto indenting within that file, enter: :setlocal noautoindent
:setlocal nocindent
:setlocal nosmartindent
:setlocal indentexpr=
The following is equivalent (it uses the abbreviated names in a single command): :setl noai nocin nosi inde= Here is a mapping so you can press F8 to disable auto indenting: :nnoremap <F8> :setl noai nocin nosi inde=<CR> This example toggles auto indenting on or off and displays the current status: :setl ai! ai?

Disable auto indent for all files

Edit your or the system vimrc file. Typically located in ~/.vimrc and /etc/vim/vimrc respectively - execute ":version" within vi to know for sure. Then edit any lines, among others, that look like this by commenting them out or swapping "on" to "off": filetype indent plugin on
filetype indent on
set ai
set si

Convert tabs to double spaces

You can replace all tab characters with two spaces in a file with the following:

:%s/\t/ /g OR :set expandtab tabstop=2 shiftwidth=2 softtabstop=2
:retab!
Where
:set expandatab Enables using space characters instead of tab characters when the tab key is pressed.
Note: with this option set you will have to press Ctrl-V<Tab> to enter a tab character
:set tabstop=N Sets number of space characters (N) that will be inserted when the tab key is pressed
:set shiftwidth=NSets number of space characters (N) that will be used for identation
:retab Replaces tab characters in file with new definition
Also see the retab command.

Links