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.
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 .
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With