How would I go about completing the following function in vimscript?
fun! Foo() let l:bar = "Hello there, world!" # Perform a substitution on l:bar, changing "world" to "kitten" endfun
That is, how do I perform a substitution on a variable, rather than the current buffer.
I know that in order to substitute on the buffer, I can write
silent :%s/world/kitten/g
but what is the equivalent command for substituting on a variable?
A substitution variable is a user variable name preceded by one or two ampersands (&). When SQL*Plus encounters a substitution variable in a command, SQL*Plus executes the command as though it contained the value of the substitution variable, rather than the variable itself.
The name of a variable is a placeholder for its value, the data it holds. Referencing (retrieving) its value is called variable substitution.
Variable substitutions are a flexible way to adjust configuration based on your variables and the context of your deployment. You can often tame the number and complexity of your variables by breaking them down into simple variables and combining them together using expressions.
You can define variables, called substitution variables, for repeated use in a single script by using the SQL*Plus DEFINE command. Note that you can also define substitution variables to use in titles and to save your keystrokes (by defining a long string as the value for a variable with a short name).
See :help substitute
of :help substitute()
.
It is the counterpart of the substitute command (See :help :substitute
).
substitute({expr}, {pat}, {sub}, {flags}) *substitute()* The result is a String, which is a copy of {expr}, in which the first match of {pat} is replaced with {sub}. This works like the ":substitute" command (without any flags). But the matching with {pat} is always done like the 'magic' option is set and 'cpoptions' is empty (to make scripts portable). 'ignorecase' is still relevant. 'smartcase' is not used. See |string-match| for how {pat} is used. And a "~" in {sub} is not replaced with the previous {sub}. Note that some codes in {sub} have a special meaning |sub-replace-special|. For example, to replace something with "\n" (two characters), use "\\\\n" or '\\n'. When {pat} does not match in {expr}, {expr} is returned unmodified. When {flags} is "g", all matches of {pat} in {expr} are replaced. Otherwise {flags} should be "". Example: > :let &path = substitute(&path, ",\\=[^,]*$", "", "") This removes the last component of the 'path' option. :echo substitute("testing", ".*", "\\U\\0", "") results in "TESTING".
In your example I guess thatlet l:bar = substitute(l:bar, "world", "kitten", "")
should work
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With