Set Vim's shiftwidth and tabstop based on directory

霸气de小男生 提交于 2019-12-20 10:38:23

问题


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?


回答1:


The venerable Tim Pope just released a plugin call Sleuth, which automagically determines shiftwidth and tabstop (among other buffer options) based on what's used in the current file (or, incredibly, if it's a new buffer, based on nearby files).




回答2:


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.




回答3:


If you don't worry about a central configuration of those project directory exceptions, the :autocmd solution from Nikita Kouevda is simple and effective. On the other hand, if you want the specific configuration stored with the project (and don't want to embed this in all files via modelines), you have the following two options:

If you always start Vim from the project root directory, the built-in

:set exrc

enables the reading of a .vimrc file from the current directory. You can place the :set ts=2 sw=2 et commands in there.

Otherwise, you need the help of a plugin; there are several on vim.org; I can recommend the localrc plugin.




回答4:


Some time ago I wrote plugin exactly for these needs: Vimprj. This is a plugin for managing options for different projects.

One guy even wrote nice article about it.

Shortly: you need to create .vimprj file at the root of your project (or .vimprj directory with some .vim files), and every time you switch to this project, this file or directory .vimprj will be sourced.

Read plugin page for details, and feel free to ask me if you have any problems with this plugin.




回答5:


embear/vim-localvimrc https://github.com/embear/vim-localvimrc worked well.

I create a .lvimrc file containing:

setlocal noexpandtab
setlocal shiftwidth=8
setlocal tabstop=8

and it beats the default autocmd FileType configurations from my .vimrc for every file in a child directory that contains .lvimrc.

The plugin sources every .lvimrc it finds on a parent directory, root-side first, so that the the deeper the .lvimrc the more precedence.

EditorConfig https://github.com/editorconfig/editorconfig-vim is a great option if you control the source tree, for settings which everyone must use, as it contains editor agnostic settings (and a Vim implementation).



来源:https://stackoverflow.com/questions/14406944/set-vims-shiftwidth-and-tabstop-based-on-directory

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!