How can I search/replace the output filename in my build system command? For example if the file I'm editing was called file_src
, then I would want my build system to output it as file_build
.
"shell_cmd": "my_command ${file} > ${file_name_but_search_replaced}"
Variable expansions in build systems are performed using the same underlying mechanism as that which is used in snippet substitutions, which is:
${var_name/regex/format_string/options}
As outlined in the link above, in snippets the variable named can be snippet fields or other special variables that apply only in snippets such as $SELECTION
. Inside of a sublime-build
file, the build variables are used instead.
How you might apply that depends on your needs and the filenames involved. For example:
{
"shell_cmd": "echo '${file_path}/${file_name/_src/_build/}'"
}
The thing to keep in mind is that the replacement will apply to the first match in the file (unless you specify the option to make it global, in which case it applies to all), so here the expansion is the path of the file followed by the name of the file with the substitution in just that field.
That makes sure that only the filename is modified in cases where the path might also contain the regex. Depending on your use case that may not matter (or be desirable), in which case you could apply it to $file
instead.