How to switch between multiple vim configurations using a command or local vimrc files?

I work in several groups, each of which has its own tab / indentation / spacing standards in C.

Is there a way to have separate selectable VIM configurations for each so when I edit the file:

  • I am doing something like set group=1 to select a configuration
  • the local .vimrc, which is located in the working directory, is used to automatically configure the configuration
+73
vim
Dec 11 '09 at 17:29
source share
10 answers

In summary

There are several ways to do this, most of which have been suggested, but I thought I would let them down in two additional ways:

  • Per-directory vimrc - has the disadvantage that Vim must be run in the right directory: if your project is located in ~/project1 and you have ~/project1/.vim.custom and do cd ~ ; vim project1/file.c cd ~ ; vim project1/file.c , user settings will not be found.
  • Modelines are very efficient, but have the disadvantage of having to add them to all files (and don't forget to add them to new files).
  • Catalog specific autocommands are very effective
  • Scanning a specific header in a file (see below) is the one that I used most in the past when I worked for different companies or on clearly named projects.
  • Per-directory vimrc, which checked when a file is open (see below). Another is quite simple to implement, especially if the project code is in one place.

Header Scan

Many organizations have a standard heading (with copyright notice and project name, etc.) at the top of each source file. If so, you can force Vim to automatically scan the first (for example) 10 lines of a file looking for a keyword. If he finds this, he can change your settings. I changed this to make it easier than the form I use (which does a lot of other things), but create ~/.vim/after/filetype.vim (if you don't already have one) and add something like this :

 au FileType * call <SID>ConfigureFiletypes(expand("<amatch>")) " List of file types to customise let s:GROUPNAMETypes = ['c', 'cpp', 'vhdl', 'c.doxygen'] func! <SID>CheckForGROUPNAMECode() " Check if any of the first ten lines contain "GROUPNAME". " Read the first ten lines into a variable let header = getline(1) for i in range(2, 10) let header = header . getline(i) endfor if header =~ '\<GROUPNAME\>' " Change the status line to make it clear which " group we're using setlocal statusline=%<%f\ (GROUPNAME)\ %h%m%r%=%-14.(%l,%c%V%)\ %P " Do other customisation here setlocal et " etc endif endfunc func! <SID>ConfigureFiletypes(filetype) if index(s:GROUPNAMETypes, a:filetype) != -1 call <SID>CheckForGROUPNAMECode() endif endfunc 

Whenever a file of any type is opened and the file type is set ( au FileType * string), the ConfigureFiletypes function is called. This checks if the file type is in the list of file types associated with the current group (GROUPNAME), in this case "c", "cpp", "vhdl" or "c.doxygen". If so, it calls CheckForGROUPNAMECode (), which reads the first 10 lines of the file and, if they contain GROUPNAME, it does some tweaking. Besides setting up expandtabs or something else, it also changes the status bar to show the name of the group so that you know that it worked at a glance.

Verify configuration on opening

Like the JS Bangs suggestion, using a custom configuration file can be helpful. However, instead of loading it into vimrc, consider something like this, which will check when the .c file is opened for .vim.custom in the same directory as the .c file.

 au BufNewFile,BufRead *.c call CheckForCustomConfiguration() function! CheckForCustomConfiguration() " Check for .vim.custom in the directory containing the newly opened file let custom_config_file = expand('%:p:h') . '/.vim.custom' if filereadable(custom_config_file) exe 'source' custom_config_file endif endfunction 
+36
Dec 12 '09 at 15:39
source share

I have this in $HOME/.vimrc :

 if filereadable(".vim.custom") so .vim.custom endif 

This allows me to put the .vim.custom file into each directory to load commands and options specific to that directory. If you are working on several projects with deep directory structures, you may need something more complex (for example, walk to the directory tree until .vim.custom is found), but the same basic idea will work.

UPDATE:

Now I am doing something similar to read the .vim file from the same directory as the edited file, regardless of what the current directory is.

 let b:thisdir=expand("%:p:h") let b:vim=b:thisdir."/.vim" if (filereadable(b:vim)) execute "source ".b:vim endif 
+60
Dec 11 '09 at 17:36
source share

You can also put autocommands into your .vimrc , which sets specific parameters for each path.

 au BufRead,BufNewFile /path/to/project1/* setl sw=4 et au BufRead,BufNewFile /path/to/project2/* setl sw=3 noet 
+33
Dec 11 '09 at 17:44
source share

The plugin does the right thing: http://www.vim.org/scripts/script.php?script_id=441

"This plugin searches for local vimrc files in the file system tree of the open file. By default, it searches for all .lvimrc files from the file directory to the root directory and downloads them in reverse order. The file name and number of downloaded files are configured using global variables."

+17
Feb 23 2018-11-11T00:
source share

Assuming your fellow developers will not complain about this, you can always add vim settings to each file in the comments.

 /* * vim:ts=4:sw=4:expandtab:... */ int main(int argc, char **argv) { ... 
+11
Dec 11 '09 at 17:38
source share

I created an open source tool for this purpose. Forget the headers, scans, configurations, and local vimrc files.

Try to swim.


Swim

swim is a quick tool to switch vimrc files and create handy aliases. Here is a short list of uses. See the Github repo for a download and download guide:


Using

 swim add ~/dotfiles/myVimrc favorite #Add new swim alias swim ls #Show available swim aliases swim add https://raw.githubusercontent.com/dawsonbotsford/swim/master/exampleVimrcs/vimrcWikia.vim example swim with favorite #Set alias favorite as primary .vimrc swim with main #Set alias main as primary .vimrc 


More details

https://github.com/dawsonbotsford/swim

+5
Sep 30 '15 at 16:43
source share

After polling the localvimrc plugin suggested by the previous poster, I really like to have non-futzy control over vim settings for each project.

It asks for confirmation before downloading the .lvimrc file by default, but there is an option to automatically download .lvimrc files. Some may see this as a security hole, but it works as advertised.

I selected .gitignore .lvimrc files. Alternatively, you can check them as a form of general settings (tab / space expansion, tabs, other settings for the project).

+4
Nov 21 '11 at 23:14
source share

As mentioned in the sleigh , using this plugin is the best option I've seen and use. jerseyboy noted that it is recommended that the utility ask for confirmation before downloading (that is, after opening each file). To avoid this, simply set in your main .vimrc list of local .lvimrc files:

let g:localvimrc_whitelist='/development/kernel/.lvimrc'

+1
Jun 21 '12 at 17:32
source share

Here's a variation on jamessan's

 function! ConditionalLoad() let cwd = getcwd() if getcwd() =~ $HOME . "/src/mobile" so $HOME/.vim.mobile endif endfunction autocmd VimEnter * call ConditionalLoad() 

I often run vi without a specific file, which I go to, so this allows you to load the configuration conditionally based on the current working directory. The disadvantage is that the configuration is not file-based, but disconnected from the working directory.

+1
May 03 '13 at
source share

In search of most of the same problem, I also found the Sauce plugin: http://www.vim.org/scripts/script.php?script_id=3992

He claims:

Sauce is a lightweight multi-vimrc file manager that can be used to load various settings for different environments. In short, you can support many different vim settings files and only download the ones you need when you need them.

I am particularly interested in the fact that it saves the configuration in its data directory, rather than expecting the user to cover dotfiles through the file system. This, although often rather an indicator of personal taste.

I still need to check this out.

0
Jan 18 '13 at 17:06
source share



All Articles