Contact Us

Vim script : How to use an expression in a Vim command

Reading Time: 3 min

Tags:  Vim

Many Vim commands do not support variables or expressions.

cd ProjectRootGet()

A command like the above one will not work in Vim, you have to supply literal values as parameters. But Vim provides ways of working around this limitation.

Backtick Equals

If the Vim command expects a filename or directory as a parameter, as in the above example, you can use the backtick equals option to supply an expression as parameter. Surround the expression with backticks and put an equals sign just after the first backtick.

cd `=ProjectRootGet()`

If you don’t put the equals after the backtick, the string inside the backticks will be treated as a shell command.

Setting Vim options using an expression

Options in Vim are usually set using the set command. Options are special internal variables of Vim which can be accessed by prefixing the option name with an ampersand.

set background=dark
let &background="dark"

The above two statements have the same effect. Option variables can be of type number, string, or boolean. For setting boolean variables use 0 or 1 as usual. For setting local options use let &l:option_name=value and for setting global options use let &g:option_name=value.

Execute

The execute command is the most popular option for using expressions in Vim commands. It executes a string as an Ex command. We can build the string dynamically using Vim expressions. Execute is a powerful option since we can build a whole command dynamically, not just the arguments to a command. Execute does not have the limitations of the other options but use it with caution and use it only when required.

execute "cd" ProjectRootGet()

You can supply more than one expression as argument to Execute; the arguments are concatenated with a space in between. If you don’t want the space, use string concatenation to combine multiple expressions into a single expression.

execute "source" $MY_HOME . '/run_in_terminal.vim'

Execute command and the normal command

Execute is often useful when using the normal command. It allows you to use special keys in the normal command.

execute "normal mxA;\<Esc>`x"

This adds a semicolon to the end of the current line and moves the cursor back to its original position. Since the command is a string we can use the special key <Esc> to re-enter normal mode. This would not be possible if we use the normal command directly. Please see “:help expr-string” for a list of all special characters.

Eval()

If you want to build an expression dynamically and evaluate it, use the eval() function. It evaluates a string and returns the result.

let a="5+4"
echo eval(a)

This would print 9. The type returned by eval is whatever the expression inside the string evaluates to. So in the above example, 9 is a number, not a string. This works for other types like Floats, Lists, Dictionaries, etc.

let a=eval("[1,2,3] + [4,5,6]")
echo a "prints [1,2,3,4,5,6] echo a[0] "prints 1

Share