Bash commands inside vi

29,300

Solution 1

Yes, e.g if you want to do ls, try:

:!ls

To spawn a shell, use

:shell

Solution 2

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.

Solution 3

Since you mentioned "get the stdout", note that you can do

:r! command

and the output of the command will be added to the file, or

:<range>!command

to have the content of the range filtered through the command [e.g. :1,$!wc]

Solution 4

My preferred idiom is

!!command

which is shorthand for

:.!command

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:

!!sh

Or

!<motion>command

such as (for example)

!apsh

to run the whole of the current paragraph through the shell.

Solution 5

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, :r!which perl.

Share:
29,300

Related videos on Youtube

harp
Author by

harp

Updated on September 18, 2022

Comments

  • harp
    harp over 1 year

    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.

  • harp
    harp over 11 years
    awesome, how could I live without this?
  • Eliot Ball
    Eliot Ball over 11 years
    This can get sticky if you get caught up in what you're doing on the command line, and forget that you have backgrounded vim.
  • Rqomey
    Rqomey over 11 years
    Yes, it is possible, I have done it myself. For me it is worth it to have shell constancy. I will update my answer to take your comment into account
  • Kedar Vaidya
    Kedar Vaidya over 11 years
    I think you would be better off using gnu screen.
  • Rqomey
    Rqomey over 11 years
    I use tmux myself... but for speed, and if logged in to a server without tmux or screen, it is handy
  • Eliot Ball
    Eliot Ball over 11 years
    :%!command looks pretty useful
  • Random832
    Random832 over 11 years
    Note for !} 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)
  • Mark D
    Mark D over 11 years
    In fact something I often do is forget to run vi as root when editing something. This command has saved me from lengthy rewrites more times than I can remember. :w !sudo tee %
  • Matthew
    Matthew over 11 years
    i wish there were some way to do this in gnu nano
  • Walter A
    Walter A over 9 years
    I use this often for sorting lines [e.g. :.,+20 !sort ]
  • VaTo
    VaTo over 7 years
    and then how do you go back to the editor after using :shell?
  • Raman Kathpalia
    Raman Kathpalia about 7 years
    @VaTo : To go back, you can type: exit