141

I can open a shell by using the :shell command in Vim, however I can't edit a file and at the same time use the shell.

Is there any way to split Vim in many Windows (or tabs), and have a shell opened in one of them?

Martín Fixman
  • 9,055
  • 9
  • 38
  • 46
  • 1
    There used to be a shell patch out there for vim but it doesn't compile anymore afaik. The closest you're going to get to a true shell is to use screen or tmux. This is the biggest thing I miss in vim. – Randy Morris May 06 '10 at 20:53
  • 2
    Looks like a duplicate of http://stackoverflow.com/questions/1236563/how-to-run-a-terminal-inside-of-vim – Brad Cupit Oct 01 '15 at 14:33

11 Answers11

145

Neovim and Vim 8.2 support this natively via the :ter[minal] command.

See terminal-window in the docs for details.

prater
  • 2,330
  • 1
  • 17
  • 15
47

Well it depends on your OS - actually I did not test it on MS Windows - but Conque is one of the best plugins out there.

Actually, it can be better, but works.

Zsolt Botykai
  • 50,406
  • 14
  • 85
  • 110
  • 4
    If you are using Vundle to manage the plugins, you may add Bundle 'jewes/Conque-Shell' to your .vimrc file, and run :BundleInstall to install it. – Mingjiang Shi Feb 08 '14 at 09:33
  • One thing to look out for: The +python or +python3 requirement for vi can be a killer in some work environments. – cfi Oct 29 '15 at 07:26
  • This is the updated URL (with screenshots): https://github.com/wkentaro/conque.vim – Walty Yeung Nov 15 '16 at 06:07
19

:vsp or :sp - splits vim into two instance but you cannot use :shell in only one of them.

Why not display another tab of the terminal not another tab of vim. If you like the idea you can try it: Ctrl-shift-t. and move between them with Ctrl - pageup and Ctrl - pagedown

If you want just a few shell commands you can make any shell command in vim using !

For example :!./a.out.

vladv
  • 660
  • 6
  • 12
  • 2
    And to run a currently open executable you can use `vim` path magic like so: `:!./%` – cprn Aug 25 '15 at 20:51
  • 1
    _Why not display another tab of the terminal_ – not everyone is using vim in a terminal emulator above a GUI… From `Ctrl`-whatever I suppose your answer assumes gnome-terminal or a similar emulator. – törzsmókus May 09 '19 at 12:30
8

You can use tmux or screen (second is able to do only horizontal splits without a patch) to split your terminal. But I do not know the way to have one instance of Vim in both panes.

ZyX
  • 52,536
  • 7
  • 114
  • 135
8

I guess this is a fairly old question, but now in 2017. We have neovim, which is a fork of vim which adds terminal support.

So invoking :term would open a terminal window. The beauty of this solution as opposed to using tmux (a terminal multiplexer) is that you'll have the same window bindings as your vim setup. neovim is compatible with vim, so you can basically copy and paste your .vimrc and it will just work.

More advantages are you can switch to normal mode on the opened terminal and you can do basic copy and editing. It is also pretty useful for git commits too I guess, since everything in your buffer you can use in auto-complete.

I'll update this answer since vim is also planning to release terminal support, probably in vim 8.1. You can follow the progress here: https://groups.google.com/forum/#!topic/vim_dev/Q9gUWGCeTXM

Once it's released, I do believe this is a more superior setup than using tmux.

chriz
  • 1,826
  • 2
  • 24
  • 28
8

If you haven't found out yet, you can use the amazing screen plugin.

Conque is also exceptional but I find screen much more practical (it wont "litter" your buffer for example and you can just send the commands that you really want after editing them in your buffer)

ahfoss
  • 155
  • 7
Eelvex
  • 8,975
  • 26
  • 42
3

Shougo's VimShell, which can auto-complete file names if used with neocomplcache

kuroz
  • 287
  • 2
  • 9
1

Not absolutely what you are asking for, but you may be interested by my plugin vim-notebook which allows the user to keep a background process alive and to make it evaluate part of the current document (and to write the output in the document). It is intended to be used on notebook-style documents containing pieces of code to be evaluated.

Thomas Baruchel
  • 7,236
  • 2
  • 27
  • 46
1

With Vim 8.0 or later you can run a terminal emulator in a vim window by using the terminal feature. BTW if you want to simulate modern IDE terminal (like VSCode integrated terminal) in gVim or MacVim, you can put the following configuration in you vimrc.

set shell=/path/to/shell

" Make sure to replace `sh.exe` in BufNr("sh.exe") with your shell executable.
nnoremap <expr> <space> BufNr("sh.exe") > 0 ? (&buftype == 'terminal' ? '<c-^>' : ':b '. BufNr("sh.exe") . '<cr>') : ':terminal ++curwin<cr>'

function! BufNr(pattern)
  let bufcount = bufnr("$")
  let currbufnr = 1
  let nummatches = 0
  let firstmatchingbufnr = 0
  while currbufnr <= bufcount
    if(bufexists(currbufnr))
      let currbufname = bufname(currbufnr)
      if(match(currbufname, a:pattern) > -1)
        let nummatches += 1
        let firstmatchingbufnr = currbufnr
      endif
    endif
    let currbufnr = currbufnr + 1
  endwhile
  return firstmatchingbufnr
endf

Now you can use space in normal mode (or whatever mapping you chosen) to:

  1. Open a terminal in current window if terminal doesn't exists yet.
  2. Switch to terminal buffer if current buffer is not a terminal type.
  3. Switch to previous buffer if current buffer is a terminal buffer.
mapkts
  • 11
  • 3
0

You may want to open a "screen" program, split screen, open shell on one and vim on another. Works for me.

  • Cancelled the -1 because there was no explanation. Besides, I tested the vim screen plugin solution and it is not that different from the solution of mrcybul. @mrcybul : tanks for trying to help. To avoid being downvoted, read carefully the other answers and add your own only if you think it brings something new. Also include why your solution may be better in certain cases. – Titou May 03 '17 at 11:44
0

I am currently using tmux.

Installation: sudo apt-get install tmux Run it: tmux

Ctrl + b followed by Ctr + % : it splits your terminal window in two vertical halves.

Ctrl + "arrow left | arrow right" : moves between terminals.