I am trying a simple means of sourcing a file based on the filetype of the new opened file. For example, I want to source python.vimrc when a new file is *.py. This code (in .vimrc)
function LoadFileTypeDefaults()
let vimrcfile = &filetype . '.vimrc'
if filereadable(vimrcfile)
echo vimrcfile
source vimrcfile
endif
endfunction
gives the following error when I do vi new.py
python.vimrc
Error detected while processing function LoadFileTypeDefault:
line 4:
E484: Can’t open file vimrcfile
My python.vimrc is in my runpath. Moreover, if I replace
source vimrcfile
with
source python.vimrc
everything works as required.
What am I missing?
The
sourcecommand expects a file name, not an expression. You gave itvimrcfileso it’s looking for the file calledvimrcfile. Useexecuteto string together a command from one or more expressions.(If you pass multiple arguments they will be joined with spaces before executing.)
What you probably really want is to just add
in your
.vimrc.Or if you really have a lot of settings, put them in
~/.vim/after/ftplugin/python.vim.