7


I like to do the following in my .vimrc:

if has('nvim')
  let g:base_data_folder = $HOME . '/.nvim'
else
  let g:base_data_folder = $HOME . '/.vim'
endif

set backupdir = g:base_data_folder . '/backup'

Unfortunately the last line doesn't work and I get an error on starting Vim / NeoVim.
Any ideas how to get this working?

weilbith
  • 564
  • 4
  • 20
  • 1
    Possible duplicate of [Vimscript: use vim settings as variables / How to check if specific guioption is set or not](https://stackoverflow.com/questions/5296659/vimscript-use-vim-settings-as-variables-how-to-check-if-specific-guioption-is) – Doktor OSwaldo May 07 '18 at 10:44
  • Not rly, cause this problem can be solved by setting the option inside the condition blocks. – weilbith May 07 '18 at 16:59
  • I do not understand your comment. If your marked answer is your solution, it is a duplicate. The marked answer nearly contains the title of the other question. Please explain to what you wan't to say with your comment – Doktor OSwaldo May 09 '18 at 05:43
  • Hmm okay, maybe I missed something here. What I thought the difference is in the question bodies. It looks for me like he tries to have a simple condition to an option to `a` or `b`. My question was more about to define a variable `let g:base_dir = '~/.vim'` and reuse it for multiple options like `set backupdir = g:base_dir . '/backup“` and where else `set swap = g:base_dir . '/swap'` and so on. – weilbith May 09 '18 at 05:54
  • So I have a single point of changement. That in my case I want to initially define this `base_dir` variable based on a condition make the question looks more likely the other one, but doesn't hit the core of the intention. Do u get me? – weilbith May 09 '18 at 05:55
  • I get that, it is exactly the first part of the question "use vim settings as variables". Of course, while beeing the same question in a technical sense, the question itself is quite different and valid. That's why SO does not delete Duplicates but link them (And also why the question is perfectly valid, and should not recieve downvotes for beeing a duplicate). And yes of course, the marked thread has a strange secondary title about a simple condition, but that is not the essence of the question. Else it would be a question about how an `if` works... Anyway thank you for explaining. – Doktor OSwaldo May 09 '18 at 06:03
  • Okay. I'm sry for that. Glad we clarified this. – weilbith May 09 '18 at 06:06

1 Answers1

14

Settings use special syntax. The values you set to them are not really expressions. This gives them the benefit that you don't need quotes or escaping, but they're less flexible when it comes to using with programmatic expressions.

One way to avoid this that's common for other kinds of Vim syntax is the execute command (:help :execute ):

exe 'set backupdir = ' . g:base_data_folder . '/backup'

That said, for settings, there's a dedicated syntax (:help :let-& ) that I'd recommend instead:

let &backupdir = g:base_data_folder . '/backup'

Basically, this allows settings to be treated as "variables" of sorts. I recommend you read all the docs about "let" to get a better understanding of how this works, for other special cases as well.

Andrew Radev
  • 3,982
  • 22
  • 35
  • Are there known side effects on using let for options instead of set them? – weilbith May 08 '18 at 12:50
  • Not really. It's the exact same effect. It's just that `set` is easier/more readable when you have static values (simple syntax, and you can use `setlocal`, `set – Andrew Radev May 09 '18 at 08:06