10

Sometimes I edit others source code where the prevailing style is to use tabs. In this case, I want to keep the existing convention of using literal tabs.

For files I create myself, and files that use spaces as the prevailing indent style, I wish to use that instead.

How can I do this in vim?

3 Answers 3

12

You can use something like this in your ~/.vimrc to adjust to use spaces/tabs as appropriate:

" By default, use spaced tabs.
set expandtab

" Display tabs as 4 spaces wide. When expandtab is set, use 4 spaces.
set shiftwidth=4
set tabstop=4

function TabsOrSpaces()
    " Determines whether to use spaces or tabs on the current buffer.
    if getfsize(bufname("%")) > 256000
        " File is very large, just use the default.
        return
    endif

    let numTabs=len(filter(getbufline(bufname("%"), 1, 250), 'v:val =~ "^\\t"'))
    let numSpaces=len(filter(getbufline(bufname("%"), 1, 250), 'v:val =~ "^ "'))

    if numTabs > numSpaces
        setlocal noexpandtab
    endif
endfunction

" Call the function after opening a buffer
autocmd BufReadPost * call TabsOrSpaces()
6

There are a variety of plugins for that, for example (starting with my own):

  • My IndentConsistencyCop examines the indent of the buffer and analyzes the used indent widths and methods. If there are conflicting ones or if bad combinations of tabs and spaces are found, it alerts you and offers help in locating the offenders - just like a friendly policeman: >
  • Indent Finder is a Python script and Vim plugin that scans any loaded buffer and configures the appropriate indent settings
  • yaifa.vim is a port to vimscript of the above
  • detectindent.vim by Ciaran McCreesh tries to auto-detect the indentation settings
  • GuessIndent is based on detectindent.vim
  • indentdetect.vim by Eric Van Dewoestine performs a simple detection and can set defaults based on the filespec
  • matchindent.vim detects tabs, 2 and 4-space indents and adapts the indent settings accordingly
  • sleuth.vim by Tim Pope automatically adjusts 'shiftwidth' and 'tabstop' heuristically (via a simplistic sampling that does not check for bad or inconsistent indents) or by looking at other files of the same type
  • filestyle highlights tabs when 'expandtab' is set, trailing spaces, and lines longer than 'textwidth', but doesn't actually check conformance to indent
  • ShowSpaces highlights spaces inside indentation, per buffer / filetype.
  • Indent Detector run when a file is opened or written, has warnings about mixed tab / space indent, and can adapt Vim's corresponding options automatically.
3
  • I saw another one, but know nothing about it. 12 stars and recently updated. github.com/timakro/vim-yadi Commented Aug 17, 2022 at 8:30
  • And one other mention: vim-polyglot, which contains syntax rules for lots of different languages, includes its own indentation detector. Commented Aug 22, 2022 at 2:08
  • So which one do you like the most? Always have a hard time choosing plugins... I'm a neovim user btw.
    – Niing
    Commented Jan 10, 2023 at 12:55
1

One of the plugins posted earlier should be good for general use.

However, if you are working on a shared project, where people may be using different editors, then I recommending using a .editorconfig file to share the config across all editors. For example:

[*]
indent_style = space
indent_size = 4
end_of_line = lf
insert_final_newline = true

It is a fairly well known standard. Some editors respect it out-of-the box, but others need a plugin. Here is a plugin for Vim:

https://github.com/editorconfig/editorconfig-vim

With this plugin installed, I find I am able to jump into someone else's project, and my editor will immediately respect their formatting conventions, even on new files.

And if I create a .editorconfig file, that makes it easier for other people opening my project.

1
  • Thank you for sharing this :) I finally need this after doing some (wrong) contributions on a neovim plugin. (all because of those literal tabs...)
    – Niing
    Commented Jan 10, 2023 at 11:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .