How do I insert shell command output without leaving the command line?
Answer
<C-r>=system('cmd')
Explanation
In command-line mode, <C-r>=system('command') evaluates the shell command and inserts its output into the command line.
How it works
- While typing a
:command, press<C-r>= - Type
system('shell_command') - Press
<CR>to insert the output
Example
To create a file named with today's date:
:e <C-r>=system('date +%Y-%m-%d')<CR>.md
Opens a file like 2026-02-17.md.
Tips
system()returns the output including trailing newlinetrim(system('cmd'))removes the trailing newline- Works in both
:and/command lines <C-r>=strftime('%Y')uses Vim's built-in date function- Useful for dynamic file paths and command construction