How to run a terminal inside of vim?

Tag: vim Author: zhwei1987 Date: 2009-07-20

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.

Is :! <command> what are you looking for?

Best Answer

I would definitely recommend screen for something like this. Vim is a text editor, not a shell.

I would use Ctrl+AS to split the current window horizontally. Then use Ctrl+ATab (or equivalently, Ctrl+ACtrl+I which may be easier to type) to switch between the windows. There are other commands to change the size and arrangement of the windows.

Or a less advanced use of screen is just to open multiple full-screen windows and toggle between them. This is what I normally do, I only use the split screen feature occasionally.

The GNU Screen Survival Guide question has a number of good tips if you're unfamiliar with its use.


+1 I only just recently recieved the screen gospel. I wonder how I lived without it.
You can also use screen with Vim plugin Vicle to send commands edited in vim to a screen session.
I'm not sure if you can do it in the stock screen, but Ubuntu's version lets you do vertical splits with Ctrl+A | (that's a vertical pipe). Much nicer, imo.
i would rather choose tmux instead of screen.
Yes, of course one can run screen, or tmux, or terminator, or open another window and alt-tab, or even use a KVM switch activated with a footpedal. But that's not the point -- all those solutions sacrifice integration between the terminal and VIM, which is why one would bother looking for solutions in this space. I don't understand how this answer is even an answer, let alone top or accepted answer. Think I'll go try Conque.

Other Answer1

Check out Conque shell. Lets you run any interactive program inside vim, not just a shell.


This is the coolest thing I have ever seen.
:ConqueTerm bash <CR>, vim <CR>, :ConqueTerm bash.... the recursive horror!!
Using Conque you can run Vim inside your Vim inside your Vim inside your vim...
In order to understand recursion, you must first understand recursion.
@?ukasz Niemier VIMCEPTION

Other Answer2

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


And if you want to copy the command's output to the current buffer: :r! dir
I use both Emacs and Vim (and have a lot more experience with Vim). But I have found that often it is nicer and easier to work with two views at the same time (one to see what you are editing and another to work within an interactive shell - for example to test the effect of a configuration change that you have just edited). It also helps with my cognition, retention and mental workflows when important things I may need to monitor closely don't disappear off the screen (like what happens when you use Ctrl-Z or :shell in Vim).

Other Answer3

The way that I get around this is to pause Vim with Ctrl+z, play in the terminal, then return to Vim with the command fg.


:!bash is a nicer way of doing that
Thanks for this tip! Excellent.
I think he wants a kind of dreampie like functionality
@Oatman Why is :!bash nicer than Ctrl+z? (Just wondering)
@SebastiánGrignoli because with Ctrl+Z, for example, you can move to another folder, then ls then git status, etc. Ctrl+Z is a "pause", you can "resume" with fg. But with :!bash, as I know so far, you only can run a single command at once, or even if you can run more than a command at once, it's still not convenient. So overall, 'Ctrl+Z' is much better

Other Answer4

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


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>

Other Answer5

No, you cannot:


DO NOT TAKE THE ADVICE OF splitvt - it mangles the color control codes and makes vim utterly unusable.
"No" is a bad answer. A better answer is "By default, no." Technically, it can be done, and perhaps you need to write new source code and compile it yourself, or write a plugin. Is it easy? Maybe not, but it can be done. Simply saying No is more like saying "no, I won't do it."
Perhaps you want to delete this answer now? Since it is clearly possible.
@AnishRamaswamy It is still not possible to run a shell within a vim buffer using stock vim (unlike emacs). That is what the OP's question was.

Other Answer6

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


this is awesome, but mangles the escape characters in zsh

Other Answer7

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":

Other Answer8

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


To be pedantic, vim-shell is a patch, not an 'add-on' (which to me, at least, implies that it's a script). I haven't tried it personally, but the vim-shell page on the Vim wiki unfortunately suggests that it won't compile with Vim 7.1+.
Changed to appease the pedant. :) (Had to type something longer than 15 characters. 'Fixed' wasn't long enough)

Other Answer9

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