vimtricks.wiki Concise Vim tricks, one at a time.

How do I evaluate a Vimscript expression and use the result as my command-line input?

Answer

<C-\>e

Explanation

Pressing <C-\>e on the command line opens a special prompt that lets you type a Vimscript expression. When you press <CR>, the entire command line is replaced with the result of evaluating that expression. This is a powerful but little-known feature for dynamically constructing commands from live editor state.

How it works

  • <C-\>e interrupts the current command-line entry and prompts = (same as the expression register)
  • You type any valid Vimscript expression — a function call, string concatenation, arithmetic, etc.
  • Pressing <CR> substitutes the full command line with the expression's string result
  • Pressing <Esc> cancels the expression and restores the previous command line

Example

Suppose your cursor is on a filename and you want to open a split with the corresponding test file. Start typing:

:vsplit 

Then press <C-\>e and type:

substitute(expand('%'), 'src/', 'test/', '') . '_test.go'

The command line becomes:

:vsplit test/mypackage_test.go

Other practical uses:

" Insert current file's directory into a command
<C-\>e expand('%:h') . '/'

" Build a :grep command using the word under cursor
<C-\>e ':grep -r ' . expand('<cword>') . ' .'

Tips

  • The expression result must be a string; use string() to convert numbers if needed
  • You can access any Vimscript function: getcwd(), expand(), shellescape(), getline('.'), etc.
  • Combine with <C-r><C-w> (insert word under cursor) for a simpler alternative when you only need the current word
  • Works in both / search and : command modes

Next

How do I rename a variable across all case variants (snake_case, camelCase, MixedCase) at once?