Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using substitute on a variable

Tags:

vim

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?

like image 533
Sebastian Paaske Tørholm Avatar asked Feb 01 '11 14:02

Sebastian Paaske Tørholm


People also ask

How do you use substitute variables?

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.

How does variable substitution work?

The name of a variable is a placeholder for its value, the data it holds. Referencing (retrieving) its value is called variable substitution.

What is meant by 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.

What is a substitution variable used for in SQL?

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).


1 Answers

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 that
let l:bar = substitute(l:bar, "world", "kitten", "") should work

like image 195
Xavier T. Avatar answered Oct 10 '22 11:10

Xavier T.