How to run Unix commands from within Vim?

How can I run Unix commands while I'm inside vim?

5 Answers

Go to command mode Esc, then run :!unix_command. Anything run from the : prompt starting with a bang ! will be run as a unix shell command. You'll be shown the output and allowed to hit a key to get back to your work in vim.

If you have text selected in visual mode and want to send it TO a command as STDIN, hit !! and enter your command. The results of the command will replace the text you have selected.

9

From a VIM help mirror:

:shell :sh[ell] start a shell
:! :!{command} execute {command} with a shell

If you are running neovim, or vim 8.1 or later, there is also terminal.

:terminal :terminal {cmd} open a terminal window
1

In addition to the above answers, you can use the current vim buffer content as stdin for shell command using :%!.

For example, suppose you want to filter lines from the current vim window content to contain only those with substring ca inside them. You could use:

:%! grep ca

Which will automatically filter the lines, placing the grep output instead of the current lines.

1

Use :!(colon bang) followed by the command you wish to run(:!{command}) to run external commands from Vim. The output of the command will then be returned for you to view. Note that you will need to wait for the command to finish running before you can continue editing because Vim is single threaded(this can be a problem with commands that take a long time to execute). See this page in the Vim help manual for further reading.

You have to switch into File exporing mode with: :Vexplore, :Sexplore or :Explore (all accept path as an argument).

Then you can you press either % for creating a regular file or d for a directory. You will be prompted about the name.

That's the VIM native way, there is no need for running external shell.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like