Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using an eval()-like function to set a variable in Vim

Tags:

vim

In a script, I have the following not working code:

set eval(rules[formatoption])=value

where rules is a dictionary and formatoption and value are a variable. I want to make Vim read the variable name from rules[formatoption] and set it to value. How can I make Vim set a variable this way? I think there should be a function like setvar(name, value)or something similar, that sets name(string) to value. That line of code would save me from writing about 30 lines of code in a 70 lines script.

like image 798
cruizh Avatar asked Jul 07 '12 18:07

cruizh


1 Answers

Use :execute:

execute 'set' rules[formatoption] . '=value'

You can also change Vim options via :let &optionname = ..., but that doesn't help here. There's also the obscure :help curly-braces-names, but that won't work here, neither.

like image 137
Ingo Karkat Avatar answered Sep 28 '22 01:09

Ingo Karkat