Turning off automatic indentation in Vim

It always annoys me when my text editor types things for me. Vim does this when it thinks I want to indent code with a certain amount of whitespace, which is usually the case, but I want to type the whitespace myself so that I know what's going on. Even worse is when it realises I'm typing a comment and starts inserting # or * characters for me.

The obvious settings to put in your .vimrc file to turn automatic indentation off would be something like this:

" Turn off simple indentation (if the current line is indented,
" autoindent will indent the next line by the same amount).
set noautoindent

" Turn off C-style indentation (which adds indentation after lines
" which end with a { character, for example).
set nocindent

" Turn off the two 'formatoptions' ('fo' for short) which
" automatically insert comment characters for the current filetype
" when Vim thinks you want to add a line to a comment.
set fo-=r fo-=o

This isn't enough though, because these values can get overridden by filetype plugins, which set options they think are appropriate for particular types of file. So files which Vim thinks are C, C++ or Java programs get the cindent option turned on when you start editing them.

According to the documentation, you can avoid this as follows:

" Should turn off the automatic indentation options set
" by filetype-specific 'indent' plugins, but doesn't.
filetype indent off

This prevents Vim from loading files like indent/c.vim, which set indentation options for specific languages, but it unfortunately it doesn't have the desired affect (at least for Vim 6.1 on Debian). The indentation script for C has setlocal cindent in it, as you'd expect, but the generic filetype plugin ftplugin/c.vim also sets that, which I think is a mistake. The generic script also sets the options which continue comments across lines, so that solution probably wouldn't be adaquate anyway.

The solution I'm using for now, which does seem to work, is to set certain options after the filetype-specific commands have been run, and doing so for every file type. That can be done with an autocommand for the FileType event:

" This works, and overrides the options for any filetype.
autocmd FileType * set fo-=r fo-=o nocindent noautoindent

By the way: my distaste for automatic indentation is one of the main reasons I don't use Emacs, because both Emacsen make it almost impossible to turn this stuff off.

< The Dreaded Command Line | Linux Babble, Apache training, RSS feeds >

Miniblog

(nuggets of inanity)

Tuesday Apr 24th 2007, 16:54 »
Just took the annual web design survey that AListApart do. I don't realy consider myself to be a web designer, but I have been doing a lot of HTML and CSS lately.
Monday Apr 23rd 2007, 18:23 »
Strange, there appears to be a bare-knuckle boxing match going on in the field outside my flat. Wish they wouldn't make so much noise about it.
Thursday Mar 1st 2007, 18:47 »
“In its written form, Hebrew has no vowels, making it the ideal language for texting.”
—Said in jest on some Radio 4 programme just now.

Archive: 2007 · 2006 · 2005 · 2004
Feed