[vim] How do I run a terminal inside of Vim?

I am used to Emacs, but I am trying out Vim to see which one I like better.

One thing that I like about Emacs is the ability to run a terminal inside Emacs. Is this possible inside of Vim? I know that you can execute commands from Vim, but I would like to be able to run a terminal inside of a tab.

This question is related to vim

The answer is


Split the screen and run command term ++curwin to run the terminal inside the Vim buffer. Following command does both and worked for me:

:bo 10sp | term ++curwin

Someone already suggested https://github.com/Shougo/vimshell.vim, but they didn't mention why. Consequently, when I came away from this question I wasted a lot of other time trying the other (much higher ranked) options.

Shougo/vimshell is the answer. Here's why:

In addition to being a terminal emulator, VimShell allows you to navigate through terminal output in normal and visual mode. Thus, if a command you run results in output that you'd like to copy and paste using the keyboard only...VimShell covers this.

None of the other options mentioned, including the :terminal command in NeoVim do this. Neovim's :terminal comes close, but falls short in at least the following ways as of 2/18/2017:

  • Moves the cursor to the end of the buffer, instead of at the last keeping it in the same spot like VimShell does. Huge waste of time.

  • Doesn't support modifiable = 1 see a discussion on this at Github, so helpful plugins like vim-easymotion can't be used.

  • Doesn't support the display of line numbers like Vimshell does.

Don't waste time on the other options, including Neovim's :terminal. Go with VimShell.


I acknowledge that I am not strictly answering your question, but what has worked better for me when using Vim and Terminals in the same window is Tmux (which is kind of a "run in the background software" like, similar to screen, although this one works better with splits and tabs).

This post will help you to understand how they work together: 'Tmux and Vim — even better together'.

This way we can convert Vim into a powerful IDE


:term

Added in Vim 8.1.

Keep in mind that whenever a terminal window is active, most keystrokes will simply be passed to the terminal instead of having their usual functions. Ctrl-W and its subcommands are the main exception. To send a literal ^W input to the terminal, press Ctrl-W .. You can also open the Vim : command line by pressing Ctrl-W :. The other Ctrl-W commands work as normal, so managing windows works the same no matter what type of window is currently selected.


If enabled in your version of Vim, a terminal can be started with the :term command.

Terminal window support was added to Vim 8. It is an optional feature that can be enabled when compiling Vim with the +terminal feature. If your version of Vim has terminal support, :echo has('terminal') will output "1".

Entering :term will place you in Terminal-Job mode, where you can use the terminal as expected.

Within Terminal-Job mode, pressing Ctrl-W N or Ctrl-\ Ctrl-N switches the mode to Terminal-Normal, which allows the cursor to be moved and commands to be ran similarly to Vim's Normal mode. To switch back to Terminal-Job mode, press i.

Other answers mention similar functionality in Neovim.


The way that I get around this is:

  • pause Vim with Ctrl + Z,
  • play in the terminal,
  • then return to exactly where you left with Vim by just typing the command fg.

Only way I know of is by using vim-shell, a third-party patch.



Eventually a native :terminal command was added to vim in 2017.

Here is an excerpt from the :terminal readme:

This feature is for running a terminal emulator in a Vim window. A job can be started connected to the terminal emulator. For example, to run a shell:

 :term bash

Or to run build command:

 :term make myprogram

The job runs asynchronously from Vim, the window will be updated to show output from the job, also while editing in another window.


I use this now, you may can try. VimShell


I'm not sure exactly what you're trying to achieve (I've never used Emacs), but you can run commands in Vim by typing:

:! somecommand [ENTER]

And if you want to type in several commands, or play around in a shell for a while, you can always use:

:! bash (or your favourite shell) [ENTER]

Once the command or shell terminates, you'll be given the option to press Enter to return to your editor window

Vim is intentionally lightweight and lacking in the ability to do non-editorish type things, just as running a full-blown shell inside a Vim pane/tab, but as mentioned above there are third-party addons such as vim-shell that allow you to do that sort of thing.

Typically if I want to switch between Vim and my shell (Bash), I just hit CTRL+Z to pause the Vim process, play around in my shell, then type 'fg' when I want to go back to Vim - keeping my editor and my shell nice and separate.


The main new feature of Vim 8.1 is support for running a terminal in a Vim window.

:term will open the terminal in another window inside Vim.


This question is rather old, but for those finding it, there's a new possible solution: Neovim contains a full-fledged, first-class terminal emulator, which does exactly what ConqueTerm tried to. Simply run :term <your command here>.

<C-\><C-n> will exit term mode back to normal-mode. If you're like me and prefer that escape still exit term mode, you can add this to your nvimrc:

tnoremap <ESC><ESC> <C-\><C-N>

And then hitting ESC twice will exit terminal mode back to normal-mode, so you can manipulate the buffer that the still-running command is writing to.

Though keep in mind, as nvim is under heavy development at the time I'm posting this answer, another way to exit terminal mode may be added. As Ctrl+\Ctrl+n switches to normal mode from almost any mode, I don't expect that this answer will become wrong, but be aware that if it doesn't work, this answer might be out of date.

https://github.com/neovim/neovim


Outdated from August 2011

Check out Conque Shell (also on GitHub). Lets you run any interactive program inside vim, not just a shell.


You might want to take a look at the :sh command (see :help sh in Vim).

Various commands


:sh then ctrl+d to get back in (bash)

Update:

You could map ctrl+d in vim to run :sh, which allows you to toggle between bash and vim quickly.

noremap <C-d> :sh<cr>


Assuming your version of vim supports +term command first, set shell for vim to use in one command (e.g. set=/usr/bin/zsh), and then run the command +term (i.e. bo 15vs +term). you may have to do some additional maneuvering of your windows (i.e. deleting one and rotating), but you'll have your terminal.


I know that I'm not directly answering the question, but I think it's a good approach. Nobody has mentioned tmux (or at least not as a standalone answer). Tmux is a terminal multiplexor like screen. Most stuff can be made in both multiplexors, but afaik tmux it's more easily to configure. Also tmux right now is being more actively developed than screen and there's quite a big ecosystem around it, like tools that help the configuration, ecc.

Also for vim, there's another plugin: ViMUX, that helps a lot in the interaction between both tools. You can call commands with:

:call VimuxRunCommand("ls")

That command creates a small horizontal split below the current pane vim is in.

It can also let you run from a prompt in case you don't want to run the whole command:

<Leader>vp :VimuxPromptCommand<CR>

As it weren't enought, there are at least 6 'platform specific plugins':

Here is a nice "use case": Tests on demand using Vimux and Turbux with Spork and Guard


Try vterm, which is a pretty much full feature shell inside vim. It is slightly buggy with its history and clear functions, and still in development, but it still is pretty good