0

Suppose I have the following code:

let s:user_config=fnamemodify('user.vim', ':p')
echom 'file found: ' . s:user_config
if filereadable(s:user_config)  
  source s:user_config
else
  echom s:user_config . 'was not found'
endif

If I run this, I get:

file found: /home/ninrod/vim_test/user.vim
Error detected while processing /home/ninrod/vim_test/test.vim:
line    4:
E484: Can't open file s:user_config
Press ENTER or type command to continue

on the echo line, the variable s:user_config was correctly interpolated. How do I tell vim to interpolate the s:user_config on the source line?

2

As it is written, your code will execute literally source s:user_config and you want to execute source [content of the variable].

You can use execute() like this:

execute "source " . s:user_config

:h :execute:

:exe[cute] {expr1} ..   Executes the string that results from the evaluation
            of {expr1} as an Ex command.
            Multiple arguments are concatenated, with a space in
            between.  To avoid the extra space use the "."
| improve this answer | |
1

I'm not sure you're aware of it, but most of the time we don't need to do what you're trying to achieve the way you're doing it.

If you respect vim standard way of organizing scripts, if you drop your file into ~/.vim/plugin/ it'll be automatically loaded. You cannot know when (regarding the load order of all the scripts in the same directory -- it seems to be sorted lexicographically nowadays), but it'll be loaded. This also means: drop your configuration files in ~/.vim not in ~/vim_test.

If you need a mapping/command/abbreviation defined in that plugin, you can enforce its load with :runtime plugin/user.vim. Don't forget to add inclusion guards to avoid the script to be loaded twice.

If you need a function from the plugin, then make it an autoload plugin, drop it in: ~/.vim/autoload. Prefix the function name with the plugin name, like user#my_function(), and simply call that function from where you need it: call user#my_function(parameters).

I haven't described the situation with filetype specific configuration, but it isn't much different.

| improve this answer | |
  • Thanks, but I'm doing all stuff with docker containers so users can't simply drop their customizatiosn into the aforementioned dir. – ninrod Feb 21 '18 at 18:54
  • 1
    Sometimes I cannot use $HOME as I'd like. In those case, I create a $HOME/luc directory and I have a $LUCHOME variable pointing to it. My vimrc detects that case and fixes the 'runtimepath' option to start everything from ~/luc/.vim. In the end, I have plugin directory and everything. – Luc Hermitte Feb 21 '18 at 20:06
  • that's clever, thanks for sharing. my specific situation is that I have to honor user.vim if that exists in a very custom directory structure: ~/.user/user.vim, if that exists. ~/.vim is taken and version controled. So I'm thinking that the solution in the question is simpler. – ninrod Feb 23 '18 at 0:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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