Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can vim recognize indentation styles (tabs vs. spaces) automatically?

Tags:

I'm working on a large codebase, where each file has different indentation conventions: tabs, 4 spaces, 3 spaces, etc.

I currently default on tabs and do set shiftwidth=N expandtab smarttab when I come across a spaces-indented file, but that's annoying.

Is there any functionality in Vim, or a plugin, which can recognize that, for instance, the current buffer uses an indentation with three spaces per shift?

like image 531
Stefan Majewsky Avatar asked Mar 07 '12 21:03

Stefan Majewsky


People also ask

Does vim use tabs or spaces?

Use spaces for indenting vimrc' file. The first line enables expandtab option in Vim. This option makes sure that spaces are used for indenting lines, even when you press the 'Tab' key. The second option tabstop takes a numerical value.

How many spaces are used for indentation and it is preferred over using tabs?

Four spaces should be used as the unit of indentation. The exact construction of the indentation (spaces vs. tabs) is unspecified. Tabs must be set exactly every 8 spaces (not 4).

How do I indent a tab in Vim?

In normal mode, press Tab or Shift-Tab to adjust the indent on the current line and position the cursor on the first nonblank character; in insert mode, press Shift-Tab to unindent; in visual mode, press Tab or Shift-Tab to adjust the indent on selected lines.


2 Answers

Various plugins exist that attempt to handle that situation. Here are a few I found by search for detect indent at vim.org

  • sleuth
  • DetectIndent
  • yafia
  • IndentConsistencyCop
  • IndentFinder
like image 89
jamessan Avatar answered Oct 30 '22 14:10

jamessan


Good question,I'm facing the same problem too, and recently I wrote such a vim plugin for myself:https://github.com/luochen1990/indent-detector.vim

it has the following features:

  • detect mixed indent and echo warnning on bufEnter and bufWrite automatically.
  • switch setting about indenting to fit the current indenting style automatically.
  • detecting time is limited, so you don't need to worry about opening huge files.

I think it is well designed, and need to be known, post an issue on github if you have any suggestion :)

like image 39
luochen1990 Avatar answered Oct 30 '22 12:10

luochen1990