Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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) ?

share|improve this question
4  
Read man vim and look for -u and -U... –  yeti Mar 28 at 10:37

3 Answers 3

up vote 7 down vote accepted

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.

share|improve this answer
    
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. –  Matt Fletcher Jun 25 at 13:55

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
share|improve this answer
    
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 at 14:19

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.