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

How do I insert the output of a shell command directly into my buffer?

Answer

:put =system('cmd')

Explanation

:put =system('cmd') lets you insert the output of any shell command as new lines in your buffer without leaving Vim. It combines the :put command with the expression register and Vim's built-in system() function, making it ideal for embedding dynamic content like timestamps, file listings, or command output directly into your editing session.

How it works

  • :put — inserts text as a new line below the cursor
  • = — signals that what follows is a Vimscript expression (uses the expression register)
  • system('cmd') — executes cmd in the shell and returns its output as a string

The output is inserted as a single block of text. Trailing newlines are preserved as separate lines.

Example

To insert the current date and time at the cursor:

:put =system('date')

Result (inserted below the cursor):

Tue Jan  1 12:00:00 UTC 2025

To insert the list of files in the current directory:

:put =system('ls -1')

Tips

  • Use :put! =system('cmd') to insert above the cursor instead of below
  • For multi-line output, systemlist('cmd') returns a list; use :put =join(systemlist('cmd'), "\n") for more control
  • Embed the current filename using system('wc -l ' . shellescape(expand('%')))
  • Compare with :r !cmd which reads shell output similarly but handles newlines slightly differently — :put =system() is pure Vimscript and works even in restricted environments

Next

How do I delete text from the cursor to the next occurrence of a pattern?