Vim Tips Wiki
(standard pre blocks)
Line 36: Line 36:
   
 
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:
 
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:
  +
<pre>
 
filetype indent plugin on
+
filetype indent plugin on
  +
</pre>
   
 
or
 
or
  +
<pre>
 
filetype indent on
+
filetype indent on
  +
</pre>
   
 
If you remove the <tt>indent</tt> keyword, none of your files will have file type based indentation rules applied.
 
If you remove the <tt>indent</tt> keyword, none of your files will have file type based indentation rules applied.
   
 
However, the <tt>filetype indent plugin on</tt> command may be included in a system-wide vimrc ({{help|system-vimrc}}), or in the file <tt>vimrc_example.vim</tt> sourced from your vimrc. For either of these cases, put the following near the bottom of your vimrc to disable filetype based indentation:
 
However, the <tt>filetype indent plugin on</tt> command may be included in a system-wide vimrc ({{help|system-vimrc}}), or in the file <tt>vimrc_example.vim</tt> sourced from your vimrc. For either of these cases, put the following near the bottom of your vimrc to disable filetype based indentation:
  +
<pre>
 
filetype indent off
+
filetype indent off
  +
</pre>
   
 
==Disabling auto indent for the current file==
 
==Disabling auto indent for the current file==

Revision as of 02:52, 3 August 2009

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

If you are editing a particular file and you want to prevent auto indenting within that file, enter these commands:

: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

To see the current indenting settings, and where they were set, enter command:

:verbose set ai? cin? cink? cino? si? inde? indk?
Good comment. I suggest moving it into the tip itself. (Spiiph 23:10, 2 August 2009 (UTC))

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))

I made some edits for correctness, since even if file type based indentation is disabled, it doesn't necessarily mean that all automatic indentation is disabled. The last section of the tip correctly describes how to disable automatic indentation altogether. I hope I made things clearer. :) (Spiiph 23:21, 2 August 2009 (UTC))