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
DrAl Dec 12 '09 at 15:39 2009-12-12 15:39
source share