I am a new Vim user. I can do : :! <some command> in Vim to run a bash command in Linux. After that bash command is executed, I can press Enter to go back to Vim. What happens if the bash command takes a long time to run (I am running some simulations), so I need to go back to code and read while waiting for the command to finish executing. I can do Ctrl + Z, then open Vim again, then go back to terminal and type fg. But is there a better way of doing this? I would like my bash running, and I can switch back between Vim and the running bash.

  • 3
    Just mentioning, you should look into tmux and at least :h terminal – klaus Feb 28 '19 at 20:34
  • @klaus you could write an answer... – D. Ben Knoble Mar 1 '19 at 17:27
  • @mle0321, by the way, using :!<command> is probably best useful for using outside programs as filters in vim. E.g. you can do :!column -s"=" -t to align some lines around =. It's more handy to launch programs that don't have to do with text editing from it's own terminal instance – klaus Mar 1 '19 at 18:07

For general purpose multi-window terminal experience, look into tmux. Tmux, a terminal multiplexer, provides functionalities to split a terminal window into several vertical and horizontal splits, managing windows from one terminal emulator instance and managing different sessions. Here is a basic bird's eye view:

|   +-------------------------------------------+   |
|   |                                           |   |
|   |   +-----------+-----------+-----------+   |   |
|   |   |           |           |           |   |   |
|   |   |  SPLITS   |  SPLITS   |  SPLITS   |   |   |
|   |   |           |           |           |   |   |
|   |   +-----------+----+------+-----------+   |   |
|   |   |                |                  |   |   |
|   |   |     SPLITS     |      SPLITS      |   |   |
|   |   |                |                  |   |   |
|   |   +----------------+------------------+   |   |
|   |                                           |   |
|   |                 WINDOWS                   |   |
|   +-------------------------------------------+   |
|                     SESSIONS                      |

There are many more advantages of tmux. For example, you can kill your terminal emulator instance and don't have to fear for losing your work, you can just attach to the tmux session. You can share your tmux screen with your co-worker etc.

Enough of tmux advertising. Let's get to native vim solutions. From vim version 8.1, you can use :terminal to get a buffer which will act as a shell from within vim. Since, it is a vim buffer you can manage it like any other buffer.

For your mentioned use cases, I'd something like this:

  • open vim
  • open file
  • split vertically (vim term, tmux splitting has totally different term!)
  • run :term make (although if you're using just make, you should look into :h quickfix and :h :make)
  • switch back and forth, etc.

N.B The above workflow is completely compatible with tmux.

|improve this answer|||||
  • @mle0312, if you fall into any problem regarding installing or using tmux or using terminal in vim, you can ask here. – klaus Mar 1 '19 at 17:59
  • Actually for problems regarding tmux installation or usage U&L or SuperUser would be a more appropriate place :) – statox Mar 3 '19 at 12:46
  • You're right... – klaus Mar 4 '19 at 10:24

If you're sitting at a console, one option is the screen command.

If you're using XWindows, there are a couple of options,

  • Use gvim to do your editing, and switch between it and your terminal
  • Use a tabbed terminal, and switch between the tabs.
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.