Like source files in .vimrc

I am trying to use a simple way to search for a file based on the file type of a new open file. For example, I want the source python.vimrc when the new * .py file. 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 processing function LoadFileTypeDefault:

line 4:

E484: cannot open vimrcfile file

My python.vimrc is in my runpath. Moreover, if I replaced

 source vimrcfile 

with

 source python.vimrc 

everything works as needed.

What am I missing?

+4
source share
3 answers

The source command expects a file name, not an expression. You gave it a vimrcfile , so it searches for a file named vimrcfile . Use execute to combine a command from one or more expressions.

 exe 'source' vimrcfile 

(If you pass multiple arguments, they will be concatenated with spaces before execution.)


What you probably really want is just to add

 autocmd Filetype python set expandtab " etc 

in .vimrc .


Or, if you really have a lot of settings, put them in ~/.vim/after/ftplugin/python.vim .

+4
source

Shouldn't it be execute "source " . vimrcfile execute "source " . vimrcfile ?

+2
source

Vim has a system for dealing with similar things called file plugins or ftplugins.

In your ~/.vim (or something else standard for your OS), go to the ftplugins directory (create if it does not exist) and create a python directory in it. Any .vim file in this directory will automatically load when the .py file is downloaded.

Note that you need to have filetype plugin on .

Alternatively, you can do this with auto-commands, although this is a bit cumbersome:

 autocmd BufNewFile,BufRead *.py source python.vimrc 

Put this in your regular vimrc and it will do what you want.

0
source

Source: https://habr.com/ru/post/1332635/


All Articles