Sublime Forum

Underscore character throws off substitution of sublime variables

#1

The way build systems are defined and invoked is great in Sublime, but I have a feeling that variable substitution is thrown off by underscores.

In my case, I type a code listing in Sublime and then use the build system to apply various transformations to it. One of those, produces an intermediate file with the same base filename (including the file path) but with the last part changed to "$file_path/$file_base_name_something.txt ".

You can probably see what the problem is here, this throws the substitution off. To see what this string is going to look like, I am using xmessage as the cmd. If I leave the _something.txt exactly adjacent to $file_base_name (where it should be), $file_base_name vanishes entirely. If I insert a space, the substitution DOES happen but of course the filename will not be found on disk.

In this case, Sublime seems to be trying to substitute $file_base_name_something, does not find it (but does not complain either) and simply decides to output “” right there.

The other thing I tried, was to escape the second _ but this still throws substitution off because…well, basically, the substitution is not even visible to Sublime because it happens at the JSON marshaling level :confused:

Perhaps Sublime is not trying to substitute file, file_base_name ` and the rest of the variables as these specific strings but it is trying a more generic regexp that matches parts that are not necessarily variables it can understand (?).

Is there a way to overcome this or does it need special care? If it will need further work, would it be possible to have it complain when something that looks like a variable but is not, is parsed?

All the best

0 Likes

#2

In build systems (and in various other places in Sublime as well), anything that starts with a $ is considered to be a variable of some sort, and the value of variables defaults to an empty string if they don’t exist.

For that reason if you ever want an actual $ character to appear in what you’re doing, you need to enter it as \\$ (the file is JSON, so \$ is not a valid escape character and the extra \ is required).

What you’re seeing here is that the variable $file_base_name_something doesn’t exist, so it gets replaced with an empty string.

To disambiguate your variables, you can wrap their bodies in {} characters. Try something like $file_path/${file_base_name}_something.txt instead, that should do what you want.

1 Like

#3

This is great, thank you very much for such a quick response.

1 Like