Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing Vim indentation behavior by file type

Could someone explain to me in simple terms the easiest way to change the indentation behavior of Vim based on the file type? For instance, if I open a Python file it should indent with 2 spaces, but if I open a Powershell script it should use 4 spaces.

like image 599
EBGreen Avatar asked Oct 01 '08 18:10

EBGreen


People also ask

What is Tabstop in Vim?

Tabstop applies to pressing tab. You can have tabs at 8 and shiftwidth at 4. vim will use both freely when indenting. The vim source code is an example of this convention. 8.

How do I indent to 4 in Vim?

The shiftwidth parameter controls your indentation size; if you want four space indents, use :set shiftwidth=4 , or the abbreviation :set sw=4 . If only this is done, then indentation will be created using a mixture of spaces and tabs, because noexpandtab is the default. Use :set expandtab .

What is Vim Ftplugin?

A file type plugin (ftplugin) is a script that is run automatically when Vim detects the type of file when the file is created or opened. The type can be detected from the file name (for example, file sample. c has file type c ), or from the file contents.


1 Answers

You can add .vim files to be executed whenever vim switches to a particular filetype.

For example, I have a file ~/.vim/after/ftplugin/html.vim with this contents:

setlocal shiftwidth=2 setlocal tabstop=2 

Which causes vim to use tabs with a width of 2 characters for indenting (the noexpandtab option is set globally elsewhere in my configuration).

This is described here: http://vimdoc.sourceforge.net/htmldoc/usr_05.html#05.4, scroll down to the section on filetype plugins.

like image 142
SpoonMeiser Avatar answered Oct 11 '22 10:10

SpoonMeiser