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.
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.
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.
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.