Some questions on .vimrc and vim configuration

2019-04-06 04:03发布

问题:

Generally on a unix system, there is a global vimrc file in the directory /etc or /etc/vim. You can also have a .vimrc file in your home directory that can customize your vi session.

Is it possible to have a .vimrc elsewhere in your directory tree so you can use different vi properties in different directories? This would be convenient because the editor properties that help you edit Python most quickly are different from those for editing, say, HTML.

This sort of thing does not seem to work be default on my mac or linux lappies. Is there a way to make it happen?

回答1:

Vim has built functionality for this:

:se exrc
Enables the reading of .vimrc, .exrc and .gvimrc in the current
directory.  If you switch this option on you should also consider
setting the 'secure' option (see |initialization|).  Using a local
.exrc, .vimrc or .gvimrc is a potential security leak, use with care!
also see |.vimrc| and |gui-init|.

See http://damien.lespiau.name/blog/2009/03/18/per-project-vimrc/

For proper project support, there are several plugins have similar features. (which I don't use, so I can't recommend any).



回答2:

If this is really a question of having different settings for different filetypes (rather than different locations on disk), then the correct thing to do is to put these files in ~/.vim/ftplugin. For example, this is the contents of my ~/.vim/ftplugin/haskell.vim:

setlocal autoindent
setlocal noexpandtab
setlocal tabstop=4
setlocal softtabstop=4
setlocal shiftwidth=4

To find out the right name for the script, simply open the kind of file you want to edit and use the :set ft? command (short for :set filetype?). Much more information is available via :help ftplugin.



标签: vim vi