Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim Set Option With String Concatenation

Tags:

vim

neovim


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?

like image 539
weilbith Avatar asked May 07 '18 09:05

weilbith


People also ask

How do I concatenate in Vim?

To concatenate a string in Vim, use the . operator.

What is string concatenation with example?

In Java, String concatenation forms a new String that is the combination of multiple strings. There are two ways to concatenate strings in Java: By + (String concatenation) operator. By concat() method.


1 Answers

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.

like image 107
Andrew Radev Avatar answered Oct 10 '22 09:10

Andrew Radev