2

I want something like this in one of my syntax files:

let s:mask1="some chars here"
let s:mask2="and some chars here"

syntax region MyOwnRegionName start=s:mask1 end=s:mask2

(the idea is to use variable s:mask1 in several region definitions.)

But that wouldn't work. Vim reports following errors:

E402: Garbage after pattern: s:mask1 end=s:mask2
E475: Invalid argument: bmlRecsOps start=s:mask1 end=s:mask2

I managed to workaround this problem:

execute 'syntax region MyOwnRegionName start=/'.s:mask1.'/ end=/'.s:mask2.'/'

But I do not quite understand why and how it works. I am not sure it is a right way. Preparing a string which contains a command - there is something wrong about it. Or is it?

1 Answer 1

4

Vim's evaluation rules are not like those found in common programming languages. Most Ex commands do not take variables, but expect literal values. This makes it more convenient for interactively issuing the commands: no string quoting is necessary.

In Vimscript, as you've already correctly discovered, you need to use :execute to get Vimscript variables evaluated into the command. It evaluates all of its arguments, and then executes the resulting string as an Ex command. If string concatenation is too clunky, you can also use printf() to place the variable contents into the command.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .