Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set Vim's shiftwidth and tabstop based on directory

I have a project I'm working on currently where the coding standard is to use 2 space indentation. On other projects, however, I use 4 space indentation.

Is there an easy way to tell vim that all files within a certain directory should have a tabstop of 2 spaces?

like image 763
tghw Avatar asked Jan 18 '13 20:01

tghw


People also ask

How do you set your editor to use 4 spaces indentation?

Push 'Edit' and select the tab 'Indention' in the appeared Profile window. Under the group 'Tab policy' select 'Spaces only' in the drop down menu. Set 'Indentation size' to 4. Set 'Tab size' to 4.

How do I set tab to two spaces in Vim?

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.


1 Answers

Use an autocmd that matches the directory and sets whatever options you need:

au BufRead,BufNewFile,BufEnter /path/to/dir/* setlocal ts=2 sts=2 sw=2

This will apply to files in subdirectories as well.

As Ingo Karkat points out, such commands should use setlocal instead of set so as to be specific to buffers.

like image 151
Nikita Kouevda Avatar answered Sep 24 '22 15:09

Nikita Kouevda