A Vim plugin for automatically detecting indent settings
This script provides a command which will attempt to guess the correct indent settings for an open file, for use when there is no modeline available. Specifically, the command sets the following buffer-local options for you:
'expandtab'
(tabs vs. spaces)'shiftwidth'
(width of indentation)'tabstop'
(width of a tab character)'softtabstop'
(number of spaces that Tab inserts)Note that this is a pure Vim implementation, and doesn’t require any external applications or interpreters.
:DetectIndent
May also be used in an autocommand. For example, you might want to put this in your vimrc
:
augroup DetectIndent
autocmd!
autocmd BufReadPost * DetectIndent
augroup END
The plugin provides some options you can set to configure what settings are chosen in edge cases or to constrain the range of indent widths allowed. Read about these options in the docs on GitHub or with :help detectindent
in Vim.
With pathogen.vim:
cd ~/.vim/bundle
git clone https://github.com/roryokane/detectindent.git
With Vundle:
Bundle 'roryokane/detectindent'
The original plugin (the one in the official Vim plugin repository) seems to be abandoned. So this is a fork of Raymond Ko’s version, which was the most polished and complete of the forks at that time, and included some merged commits that gave this plugin much more robust detection of indentation style than the original. His README said this:
I merged all the forks that I found were semi-reasonable in the GitHub network graph. This was done in less than 15 minutes, so expect bugs!
My fork’s additions to Raymond’s fork:
softtabstop
when Vim supports it, to make it easier for users to manually change indentationg:detectindent_preferred_expandtab
, which is redundant with the global setting &g:expandtab
Feel free to submit pull requests or file issues, such as if DetectIndent wrongly guesses the indentation of a file you tried to edit in Vim.