11

I would like to know, if there is a way in vim/vi to provide a different vimrc file. I am looking at managing 2 different vimrc files. For the contents I checkout/checkin in version control I want to use different set of commands and not the normal one in my .vimrc file

For Example.

My .vimrc file contains a command to automatically delete any trailing space while saving a file. But, I don't want this feature to be activated for specific files.

So any way to provide a different vimrc file ( maybe at command line, giving it as parameter each time as vim --vimrc=somefile file-to-open) ?

1
  • 4
    Read man vim and look for -u and -U...
    – user62916
    Mar 28, 2014 at 10:37

4 Answers 4

13

See the manpage:

-u {vimrc} Use the commands in the file {vimrc} for initializations. All the other initializations are skipped. Use this to edit a special kind of files. It can also be used to skip all initializations by giving the name "NONE". See ":help initialization" within vim for more details.

1
  • 1
    I created an alias on my server for loading a certain vimrc file, leaving the normal one clean for other people using it. I called it jim. Jun 25, 2014 at 13:55
13

So any way to provide a different vimrc file ( maybe at command line, giving it as parameter each time as vim --vimrc=somefile file-to-open) ?

Yes, use the -u parameter:

vim -u ~/.my-custom-vimrc

From man vim:

   -u {vimrc}  Use  the  commands in the file {vimrc} for initializations.
               All the other initializations are  skipped.   Use  this  to
               edit  a special kind of files.  It can also be used to skip
               all initializations by giving the name "NONE".  See  ":help
               initialization" within vim for more details.

My .vimrc file contains a command to automatically delete any trailing space while saving a file. But, I don't want this feature to be activated for specific files.

There's a different solution if you need special handling for some known filetypes; use the autocmd command in your vimrc. For example, this is my filetype-dependent configuration:

" Mapping of filetypes to options
au FileType freerad      setl noexpandtab sts=0 sw=8
au FileType make         setl noexpandtab sts=0
au FileType php          let php_sql_query=1
au FileType php          let php_htmlInStrings=1
au FileType python       setl shiftwidth=2 expandtab
au FileType sql          setl noexpandtab sts=0 sw=8
" global options I do not want any filetype to override
au FileType *            setl formatoptions-=ro " don't continue comments

If you need to apply options to something that vim doesn't automatically assign a certain filetype, you can add your own mapping (do that before using the autocmd command):

" mapping of filenames to filetypes
au FileReadPost,BufReadPost,BufNewFile /etc/freeradius/* setl ft=freerad
au FileReadPost,BufReadPost,BufNewFile *.xt              setl ft=xt
1
  • Thanks for detailed explanation. But i have files with same extension for which I need to have different formatting, hence need a way to give vimrc file. And got it it's the -u option that is needed.
    – mtk
    Mar 28, 2014 at 14:19
3

You can also set exrc in your main .vimrc to make vim load a .vimrc from the current directory automatically.

1

If you store your Vim config directory under Git (I hope so) - you can use different branches, and checkout between them.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .