1

The way the code needs to be indented depends on the filetype.

How can I configure Vim to use the correct value of tabstop, shiftwidth and expandtab depending on the file?

1 Answer 1

3

By file type

If the value of these option only depend on the filetype you can override the default for a specific file type (e.g.: python) by adding a ./vim/after/ftplugin/filetype.vim with the desired content.

e.g.: to ask Vim to indent cpp files using tab of 6 characters size instead of 4 spaces you could add a:

~/.vim/after/ftplugin/cpp.vim with the following content:

setlocal noexpandtab
setlocal shiftwidth=6
setlocal tabstop=6

e.g.: to ask Vim to indent python file using tab of 2 characters size instead of 4 spaces you could add a:

~/.vim/after/ftplugin/python.vim with the following content:

setlocal noexpandtab
setlocal shiftwidth=2
setlocal tabstop=2

Remark: this will work if the following command has been executed (typically in your .vimrc file)

filetype plugin on

Remark: if you have no .vimrc a default .vimrc will be executed by Vim that contains that command.

Remark:

  • On Windows you have to replace ~/.vim by ~/vimfiles
  • For Neovim you have to replace ~/.vim by ~/.config/nvim

By file

If your the indentation depends not only on the filetype but on the file itself you can use a modeline at the start or at the end of the file. A modeline is a comment with a specific syntax that Vim uses to set some buffer specific options.

e.g.: To have a specific cpp file that use tabs of 6 character size instead of 4 spaces for indentation you could add the following line in your cpp file:

// vim:ts=6:sw=6:noet:

e.g.: To have a specific python file that use tabs of 2 character size instead of 4 spaces for indentation you could add the following line in your python script:

# vim:ts=2:sw=2:noet:

More information about modeline with :help modeline

By project

If your indentation settings depend on the project (the child file from a root directory) you can use the editorconfig plugin.

To have:

  • The cpp file of your project indented with tabs of 6 characters size
  • The python file of your project indented with tabs of 2 characters size you can have a .editorconfig file at the root of your project with the following content:
[*.cpp]
indent_style = tab
indent_size = 6
[*.py]
indent_style = tab
indent_size = 2

Remark (thanks to @romainl for that): Starting with Vim 9.1 the editorconfig plugin is now distributed together with Vim. You can activate it by adding the following line to your .vimrc file:

packadd! editorconfig

More information with: :help editorconfig-install

More information about the format in the documentation page.

Ask a Vim plugin to guess

You can also use the vim-sleuth to guess these settings based on the content of the file.

There are various alternatives:

8
  • 2
    Note that the editorconfig plugin is now distributed with Vim: :help editorconfig.
    – romainl
    Jan 11 at 8:26
  • 1
    Nice write up (although I would have hoped to find something like this in Vim's :help somewhere). I have a minor objection to using Python with non-PEP-8 compliant indentation as an example. Other languages are far more liberal when it comes to whitespace.
    – Friedrich
    Jan 11 at 8:52
  • 1
    @Friedrich, I understand but to demonstrate the example I should use value that differs from the Vim standard. Maybe should I use cpp where the norm is less shared? Jan 11 at 8:59
  • 1
    My concern is that people tend to copy stuff verbatim, without ever thinking. I would hate to see tab indented Python code crop up as a result of your answer. With C or C++, anything from 2 (to make McCabe unhappy) to 8 (to make Linus happy) goes. [Edit: you already edited while I was typing...]
    – Friedrich
    Jan 11 at 9:14
  • 1
    It's "modeline", not "modline". And Vim doesn't guess anything, it is those plugins that do.
    – romainl
    Jan 11 at 10:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.