How to run Unix commands from within Vim?

129,224

Solution 1

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.

Solution 2

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

Solution 3

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.

Solution 4

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.

Share:
129,224

Related videos on Youtube

funk-shun
Author by

funk-shun

Updated on September 18, 2022

Comments

  • funk-shun
    funk-shun over 1 year

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

  • Kromey
    Kromey about 13 years
    Also, !! without any text selected will let you run a command and then insert the result at your current cursor position -- no need to send stuff to STDIN and replace it if you don't need/want to.
  • Yab
    Yab about 13 years
    also, if you simply want to put the output of a command in your document, simply do :r!unix_command. This is usefull for commands such as date
  • jlliagre
    jlliagre about 13 years
    You can also execute multiple lines of your vi buffer by the shell (or any interpreter) and have them replaced by the result of the execution. eg: :10,20!sh or, form marked lines, 'a,'b!sh
  • Andrew
    Andrew over 7 years
    Something else that I think is worth noting is that this depends on the OS you're using. If you're using Windows, it'll execute a Windows shell command. Many people reading this will already know that, but for people coming from Google, I thought it would be worth mentioning.
  • skywinder
    skywinder over 3 years
    @Kromey can you clarify how to use !!? When I run :!! - it just run previous command, but output not stored in buffer or in opened file.
  • Hans Ginzel
    Hans Ginzel over 3 years
    Or only a range :'<,'>!command or the current line :.!command.
  • dovidweisz
    dovidweisz over 3 years
    Great! So if i want to exit vim, I Go to command mode and run :r!ps aux | grep vim | awk '{ print $2 }' | xargs kill -9 ?
  • user3376703
    user3376703 over 3 years
    @dovidweisz Sure, but why such convoluted scripting? Just :!pkill -9 vim does the same work better. Or, you know, :qa! still has the fun of a bang without needing to spawn shell processes for nothing but show.
  • dovidweisz
    dovidweisz over 3 years
    @Caleb For some reason pkill wasn't working for me. Also I was looking for a uniquely convoluted way to do it ;) twitter.com/dovidweisz/status/1358800518483763207?s=20
  • ZaydH
    ZaydH almost 3 years
    If you are using neovim in place of vim, :shell is replaced with the more powerful :terminal. See here.
  • Filip Seman
    Filip Seman over 2 years
    Is it possible to run script non-interactive? So you don't have to continue "Press enter.."