Tip 1649 Printable Monobook Previous Next

created March 15, 2010 · complexity basic · author Rdineiu · version 7.0

If you work in different environments, each with its own different coding standards and rules, then your projects are going to be in separate directories that usually do not interact with each other. You can use that to let Vim know, from the path in which the projects reside, what rules to apply for each individual project.

For example, you might have a few projects with the following details:

  • The first project resides in /home/user/projects/Project1, and the rules state that tabs should be replaced with 4 spaces in all files that are not YAML, and with 2 spaces in YAML files.
  • All other projects reside in /home/user/projects and have the rules that tabs remain tabs in all files, and they should be treated as being equal with 4 spaces.

To accommodate the above scenario, you could define an autocmd in your vimrc for switching between rules, depending on the file path:

function! SetupEnvironment()
  let l:path = expand('%:p')
  if l:path =~ '/home/user/projects/Project1'
    setlocal expandtab smarttab textwidth=0
    if &filetype == 'yaml'
      setlocal tabstop=2 shiftwidth=2
      setlocal tabstop=4 shiftwidth=4
  elseif l:path =~ '/home/user/projects'
    setlocal tabstop=4 shiftwidth=4 noexpandtab
autocmd! BufReadPost,BufNewFile * call SetupEnvironment()

Related pluginsEdit

Many plugins allow this capability and many other project-related features. Just a few of them are listed here:


Alternatively, if you tend to start Vim from the root of my "project" directory, you can simply add following into .vimrc:

silent! so .vimlocal

This allows putting a file `.vimlocal` in a project root directory, where one can set additional options or override defaults from .vimrc. Thanks to `!silent`, it will be simply ignored if there is no `.vimlocal` file in the directory you start vim from.

Ad blocker interference detected!

Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.