Run A Command Or A Shell Inside Vim

In this article I will show you a usefull Vim trick: how to run commands inside the Vim text editor. Here you can find the other vim tricks.

Watch Free Movies

Vim is a powerfull text editor with the ability of doing non-editorish things, such as running commands inside it. And also to run a shell inside vim.

Normally, if I decide to run a command when I am inside vim, I have to send the process to background, run my commands and bring the editor back to foreground.

But, all this long story can be avoided, because vim can run commands from inside it:

To run a command inside Vim, place a :! in front of the command.

Examples:

1:
$ vim myfile
:! ls -l
[enter]

Press Enter to get back to the edited file.

2:
$ vim myfile
:! grep 1000 /etc/passwd
[enter]

Press Enter to get back to the edited file.

Running a shell inside vim is not different than running a command.

$ vim myfile
:! /bin/bash
command1_here
command2_here
exit
[enter]

After exiting the shell, press enter to get back to the edited file.

Related Reading: Get sudo access inside Vim

Scroll to Top