Is it possible to type a bash command inside vi and get the stdout?
I find it often tedious to close and reopen vi just because I want to look something up in the shell.
Stack Exchange network consists of 181 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.Visit Stack Exchange
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.Sign up to join this community
Yes, e.g if you want to do
To spawn a shell, use
:w !sudo tee %
nanoSep 13, 2012 at 20:56
I tend to use
ctrl+z to sleep vi, run what I need to in the shell, then
fg to resume vi. Not exactly an answer to your question, but I find it a very fast way to work.
If you do forget that you have vi open in the background (this can happen when you are busy), when you try to log out of the shell using
ctrl+d you will be warned that you have background jobs running, and you can either close them, or hit ctrl+d again to log out. Also, if you try to reopen the same file twice, you get a recovery warning, so you can press
a to abort,and run fg to go back to vi.
Since you mentioned "get the stdout", note that you can do
and the output of the command will be added to the file, or
to have the content of the range filtered through the command [e.g.
:%!commandlooks pretty useful Sep 11, 2012 at 15:10
My preferred idiom is
which is shorthand for
which pipes the current line through the command and inserts the output into the file. Most common case for me is to edit the command with vi and run:
such as (for example)
to run the whole of the current paragraph through the shell.
!}you have to be positioned at the start of the paragraph, try
!ap. (I do not know if this is in every vi or only vim) Sep 11, 2012 at 16:38
By using colon and exclamation point you can run any command in the shell path and shell scripts, keep in mind that the command runs under the user running the vi/vim program, and using it's environment.
Since vi uses the environment that was in effect when it was run, it uses the same path that was in the parent command. Keep in mind that some shells keep a cache of the programs in the path, and vi may not work with this cache (dependent on the shell it uses and it's version), so you may end up running the wrong binary in case you have more than one binary with the same name.
You can see the output of a command by typing, for example,
:!ls -lh in command mode.
If you'd like to insert the output of the command into the file you're editing, just add an
r. For example,
! is obviously the correct answer, but if you're switching back and forth frequently, something like screen or tmux might be helpful, too.
I have this in my .vimrc. Found on Stack Overflow a while back:
" Executes the current line in a bash shell; syntactically similar to slimv's ,d feature :nmap ,,d Y:!<C-R>"<C-H><CR>
I would, and do, get round this easily in Linux by opening two terminal command line windows, running vi in one and the other available for other work at the same time.