Execute

It's a little bit like a $(eval)

:execute "echom 'Hello, world!'"

:execute "rightbelow vsplit " . bufname("#")

Is Execute Dangerous?

In most programming languages the use of such an "eval" construct to evaluate strings as program code is frowned upon (to put it lightly). Vimscript's execute command doesn't have the same stigma for two reasons.

First, most Vimscript code only ever takes input from a single person: the user. If the user wants to input a tricky string that will cause an execute command to do something bad, well, it's their computer! Contrast this with other languages, where programs constantly take input from untrusted users. Vim is a unique environment where the normal security concerns simply aren't common.

The second reason is that because Vimscript has sometimes arcane and tricky syntax, execute is often the easiest, most straightforward way to get something done. In most other languages using an "eval" construct won't usually save you much typing, but in Vimscript it can collapse many lines into a single one.

Exercises

Skim :help execute to get an idea of some of the things you can and can't use execute for. Don't dive too deeply yet -- we're going to revisit it very soon.

Read :help leftabove, :help rightbelow, :help :split, and :help :vsplit (notice the extra colon in the last two topics).

Add a mapping to your ~/.vimrc file that opens the previous buffer in a split of your choosing (vertical/horizontal, above/below/left/right).

Last updated

Was this helpful?