Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set up different tab settings for different languages in Vim?

Tags:

vim

In my .vimrc I've got a generic tab setting of two spaces, and I'd like to override that on a per language basic (that is, four for Python, etc, otherwise use the default), but I'm having trouble finding any good example of this.

like image 255
Magnus Österlind Avatar asked May 21 '09 07:05

Magnus Österlind


People also ask

How do I set tabs in Vim?

As for tabs, there are two settings. Within Vim, type a colon and then "set tabstop=4" which will set the tabs to display as four spaces. Hit colon again and type "set expandtab" which will insert spaces for tabs. You can put these settings in a .

Does Vim insert tabs or spaces?

There are four main ways to use tabs in Vim: Always keep 'tabstop' at 8, set 'softtabstop' and 'shiftwidth' to 4 (or 3 or whatever you prefer) and use 'noexpandtab'. Then Vim will use a mix of tabs and spaces, but typing and will behave like a tab appears every 4 (or 3) characters.

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 set tab to two spaces in Vim?

To convert tabs to spaces in the currently opened file in Vim, enter the Normal mode by pressing Esc key. Now use the retab command by pressing the ':' (colon) character and Vim will convert the existing tabs to spaces.


1 Answers

These other answers seem way too complex. Instead of messing around with yet more directories and files in your ~/.vim tree, just add the following to your ~/.vimrc.

autocmd Filetype python setlocal expandtab tabstop=4 shiftwidth=4 softtabstop=4 

(you can be l33t and abbreviate parameters to et ts=4 sw=4 sts=4). I found this in Setting Vim whitespace preferences by filetype

like image 117
skierpage Avatar answered Sep 19 '22 17:09

skierpage