How do I run shell commands without leaving Vim?
Answer
:!ls -la
Explanation
Vim lets you execute any shell command directly from within the editor using the :! (bang) command. This is invaluable for running build tools, checking file permissions, managing git operations, or performing any system task without switching to another terminal.
How it works
The :! command passes everything after it to your default shell for execution. Vim temporarily suspends its display, runs the command, shows the output, and then returns to the editor when you press Enter. The command runs in the same working directory as your Vim session.
Vim provides special characters you can use within shell commands: % expands to the current file name, # expands to the alternate (previous) file name, and %:p gives the full path of the current file. These make it easy to operate on the file you are editing.
For commands you run frequently, you can also use :! with !! in normal mode, which filters the current line through a command, or use :terminal to open a persistent terminal split inside Vim.
Example
Common uses of :! include:
" List files in the current directory
:!ls -la
" Compile the current C file
:!gcc % -o %:r
" Run the current Python script
:!python3 %
" Check git status
:!git status
" Make the current file executable
:!chmod +x %
" View the difference with the saved version
:!diff % /tmp/backup.txt
If you want to run a shell interactively, use :shell or :sh to drop into a full shell session. Type exit to return to Vim. For a quick one-off command where you need to see the output briefly, :! is the fastest approach.