54

I work on a range of JavaScript projects. In most of these projects, the indentation of JavaScript files is 2 spaces, so I've set my default indentation width to that. However it becomes 1 or 4 in some other projects.

Is there a way to ask Vim to change the indentation style based on the style already used in the current file?

4

2 Answers 2

47

Install this plugin: tpope/vim-sleuth

3
  • 5
    Sleuth seems to work a bit better than detectindent, in my experience, and also will try to determine the proper indent settings for new files by looking at other files nearby in the filesystem.
    – jamessan
    Commented Feb 3, 2015 at 17:45
  • @jamessan didn't know that, good to know. Commented Feb 3, 2015 at 17:47
  • 3
    Please read our policy on plugin-recommendation answers and add some detail about how exactly to use your recommended plugin to solve the problem. Commented Feb 6, 2015 at 22:17
18

ciaranm/detectindent might be what you're looking for. It's unmaintained (last updated in 2015, there was also a fork that was last updated in 2016: roryokane/detectindent) and I haven't used it, but looks like it does what you want:

A Vim plugin, for automatically detecting indent settings. This plugin adds a :DetectIndent command, which tries to intelligently set the 'shiftwidth', 'expandtab' and 'tabstop' options based upon the existing settings in use in the active file.

0

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.