How can I run Unix commands while I'm inside vim?
6 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.
- 6,129
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,471
- 3
- 13
- 25
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.
- 429
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.
- 167
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.
- 101
- 2
21.1 Suspend and resume
Like most Unix programs Vim can be suspended by pressing CTRL-Z. This stops
Vim and takes you back to the shell it was started in. You can then do any
other commands until you are bored with them. Then bring back Vim with the
"fg" command.
CTRL-Z
{any sequence of shell commands}
fg
You are right back where you left Vim, nothing has changed.
In case pressing CTRL-Z doesn't work, you can also use ":suspend".
Don't forget to bring Vim back to the foreground, you would lose any changes
that you made!
Only Unix has support for this. On other systems Vim will start a shell for
you. This also has the functionality of being able to execute shell commands.
But it's a new shell, not the one that you started Vim from.
When you are running the GUI you can't go back to the shell where Vim was
started. CTRL-Z will minimize the Vim window instead.
21.2 Executing shell commands
To execute a single shell command from Vim use ":!{command}". For example, to
see a directory listing:
:!ls
:!dir
The first one is for Unix, the second one for MS-Windows.
Vim will execute the program. When it ends you will get a prompt to hit
<Enter>. This allows you to have a look at the output from the command before
returning to the text you were editing.
The "!" is also used in other places where a program is run. Let's take
a look at an overview:
:!{program} execute {program}
:r !{program} execute {program} and read its output
:w !{program} execute {program} and send text to its input
:[range]!{program} filter text through {program}
Notice that the presence of a range before "!{program}" makes a big
difference. Without it executes the program normally, with the range a number
of text lines is filtered through the program.
Executing a whole row of programs this way is possible. But a shell is much
better at it. You can start a new shell this way:
:shell
This is similar to using CTRL-Z to suspend Vim. The difference is that a new
shell is started.
When using the GUI the shell will be using the Vim window for its input and
output. Since Vim is not a terminal emulator, this will not work perfectly.
If you have trouble, try toggling the 'guipty' option. If this still doesn't
work well enough, start a new terminal to run the shell in. For example with:
:!xterm&
From help documentaion of Vim verison 8.2. You can get it by command:
:helpgrep Suspend and resume
If you wanna know more about helpgrep, you can run:
:help helpgrep
If you run the command:
:help
and skim through the documentation, you will know how to find most of the helps you needed.
- 181