How do I insert the output of a shell command into my buffer?
Answer
:read !date
Explanation
The :read !{command} syntax runs a shell command and inserts its output below the current line. This lets you pull in system information, generate content, or use any command-line tool to produce text directly into your buffer.
How it works
:read !{command}— insert command output below current line:0read !{command}— insert at the top of the file:$read !{command}— insert at the end of the file:read {file}— insert contents of a file (without!)
Example
" Insert current date
:read !date
" Insert directory listing
:read !ls -la
" Insert git log
:read !git log --oneline -5
" Insert content of another file
:read header.txt
Before (cursor on line 3):
line 1
line 2
line 3
After :read !date:
line 1
line 2
line 3
Thu Jan 15 14:30:00 UTC 2024
Tips
- Use
:.!{command}to replace the current line with command output instead of inserting :%!{command}pipes the entire buffer through a command and replaces it:read !curl -s https://example.comfetches web content!!{command}in normal mode is a shortcut for:.!{command}