Vim Tips Wiki
Register
Advertisement
Tip 330 Printable Monobook Previous Next

created 2002 · complexity basic · version 6.0


This tip discusses automatic indenting of text that may occur while you are typing, and explains how to disable such automatic indentation.

For a general discussion of indenting, see Indenting source code.

If you use Vim in a terminal, you may find that pasting from another application changes the indents in the pasted text. See Toggle auto-indenting for code paste to deal with that problem.

Disabling file type based indentation for a specific file type[]

You may like auto indenting in C programs, but dislike it when editing html files. You can disable auto indentation for particular files types; the following example shows how to do this for html files.

Create the file ~/.vim/indent/html.vim on Unix-based systems, or $HOME/vimfiles/indent/html.vim on Windows systems, containing the single line:

let b:did_indent = 1

This creates a user-specific indent script which will be loaded before the file type indent script. Auto indenting for the particular file type is disabled because well-behaved indent scripts do nothing if the buffer-local variable b:did_indent is defined (that variable indicates that the current buffer already has script-based indenting enabled).

You need to use the correct name for the file type (html in the above). If you are not sure what that name is, edit a file where you want to remove auto indentation (for example, my.htm), then enter the following command to display the value of the ft (filetype) option for the current buffer:

:set ft?

Disabling file type based indentation for all file types[]

The auto indentation provided for most languages is very helpful, and you should consider trying it. However, it's easy to disable all auto indents if wanted.

Actually, auto indentation is off by default, and if it is enabled on your system, that is because something has enabled it. For example, your vimrc may contain:

filetype indent plugin on

or

filetype indent on

If you remove the indent keyword, none of your files will have file type based indentation rules applied.

However, the filetype indent plugin on command may be included in a system-wide vimrc (:help system-vimrc), or in the file vimrc_example.vim sourced from your vimrc. For either of these cases, put the following near the bottom of your vimrc to disable filetype based indentation:

filetype indent off

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>

Comments[]

You may have disabled auto indenting in HTML files, but you would like auto indenting of your embedded PHP code. When wanted, enter the following to toggle the smartindent option, and show its current value:

:setl si! si?
Note that while 'smartindent' works decently for PHP, filetype indentation usually works better, even in this case. I think that -- and correct me if I'm wrong -- setting ft=php should offer indentation for PHP while leaving the HTML alone. See :help 'filetype', and also VimTip1213. (Spiiph 23:10, 2 August 2009 (UTC))

(update - add this to your vimrc to block the autoindenting of comments - no other solution on this page works):-

autocmd FileType * setlocal formatoptions-=c formatoptions-=r formatoptions-=o
Presumably this refers to the comment leaders inserted automatically by Vim. This is not indentation. Indentation refers to whitespace only. See the following for your problem with comment leaders, or do a search yourself for "comment leader" on the wiki or elsewhere:
--Fritzophrenic (talk) 18:23, January 17, 2013 (UTC)

Disabling auto indent temporarily to paste[]

Sometimes you only need to paste some snippet of code, that already has indentation, so you can disable it only during the paste operation:

:set paste

then

:set nopaste
The introduction at the top mentions "See Toggle auto-indenting for code paste" and that tip has the correct way to do this. You don't need the above set commands. JohnBeckett (talk) 11:30, February 8, 2013 (UTC)

Globally Disable Auto-Indentation by Disabling the File[]

cd /usr/share/vim/vim80

cp indent.vim indent.vim.bak

indent.vim in the Vim directory (vim80 for the latest version of Vim)

is THE file which sources indent/ directory (located in /usr/share/vim80 and being indent.vim's sibling node)

which contains *filetype*.vim files (eg. sh.vim) who set indentexpr option (eg to GetShIndent()).

to trigger based on filetypes of opened files.

Why do it this way, when you can just turn off filetype indentation rules in your .vimrc? this way you affect all users on the system and there is no easy way to turn back on filetype indentation, even temporarily, if desired. --Fritzophrenic (talk)
Advertisement