408

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.

4
  • 3
    related: stackoverflow.com/q/1879219/1569 Dec 5, 2012 at 14:46
  • 61
    Is :! <command> what are you looking for?
    – takeshin
    Jul 12, 2013 at 10:40
  • 85
    Since Vim 8.0, there is a :term command that opens a new terminal in a new split window. If it does not work for you, check vim --version | grep -o .terminal to see if this feature is available (+terminal) or unavailable (-terminal).
    – Susam Pal
    Jun 1, 2018 at 4:25
  • 6
    Using :term, to quit from terminal split, use ctrl W and :q
    – Hrushi
    Jul 20, 2020 at 17:06

25 Answers 25

234

Outdated from August 2011

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

12
  • 24
    :ConqueTerm bash <CR>, vim <CR>, :ConqueTerm bash.... the recursive horror!!
    – keflavich
    Jan 25, 2012 at 5:11
  • 2
    psihodelia seems to be right. I've just compiled vim with --enable-pythoninterp (cygwin doesn't distribute vim with +python) and after installing ConqueTerm, I get an error from #set_buffer_settings. I haven't been able to track down where that function lives, or get any idea of what the error means. The message includes a statement about an invalid argument "edit ++enc=utf-8 bash\ -\ 2". Argh. Jul 20, 2012 at 4:55
  • 57
    In order to understand recursion, you must first understand recursion.
    – Stabledog
    Feb 3, 2013 at 14:26
  • 21
    @Łukasz Niemier VIMCEPTION Feb 16, 2013 at 2:06
  • 1
    Works with zsh, sweet! d-_-b
    – qmmr
    Sep 21, 2014 at 19:20
233

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.

3
  • 21
    And if you want to copy the command's output to the current buffer: :r! dir Aug 12, 2009 at 19:03
  • 7
    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).
    – mvanle
    Mar 24, 2014 at 22:02
  • Nice answer! To compliment it even further, I would also add the :r! command [ENTER] mentioned in the comments.
    – Jeach
    Aug 2, 2019 at 13:49
220

Updated answer (11 years later...):

  • I would recommend using tmux instead of screen as suggested in the original answer below, if you choose to use that solution.
  • Vim 8.1 now has a built in terminal that can be opened with the :term command. This provides much more complete integration with the rest of the Vim features.

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, or in Ubuntu's screen and other patched versions, you can use Ctrl+A|(pipe) to split vertically. Then use Ctrl+ATab (or equivalently on some systems, 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.

16
  • 11
    +1 I only just recently recieved the screen gospel. I wonder how I lived without it.
    – guns
    Aug 11, 2009 at 3:30
  • 131
    i would rather choose tmux instead of screen. Sep 2, 2011 at 4:52
  • 90
    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.
    – Stabledog
    Feb 3, 2013 at 13:53
  • 41
    how is this the top answer??? you need to unvote it and vote up the next answer down, which actually answers your question. Using screen does not run a shell within vim. That's like telling someone to start another shell. And for those who keep throwing out ":!" he ALREADY SAID he knew how to execute commands, which is NOT what he asked for... sheesh. The arrogance is so thick in here you can cut it with a knife. Aug 15, 2014 at 20:19
  • 9
    This answer should be updated due to new vim 8.1 feature Terminal Window Jul 19, 2018 at 19:01
193

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.
7
  • 16
    :!bash is a nicer way of doing that
    – 0atman
    Mar 16, 2012 at 11:14
  • I think he wants a kind of dreampie like functionality Jan 12, 2013 at 22:04
  • 16
    @Oatman Why is :!bash nicer than Ctrl+z? (Just wondering) Feb 16, 2013 at 2:07
  • 3
    @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
    – Leo
    Jun 6, 2013 at 4:17
  • +1 because this works in pretty much any environment. Working on a foreign system that I can't customize, and through a cheap PuTTY shell, this works great.
    – t3sture
    Dec 17, 2017 at 2:45
163

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.

6
  • 11
    +1 for how to switch back into Terminal cmd mode with i after switching from ctrl-w-N browse mode. This is a killer feature even over tmux and works on Windows too.
    – radke
    Oct 24, 2017 at 13:36
  • 3
    Is it possible to vertical split using :term? Nov 8, 2017 at 10:03
  • 2
    super! Great that the terminal is built in so we do not have mess with more plugins. Upgraded to vim 8 via brew install vim --override-system-vi and :term works perfectly. Thanks for sharing! Feb 20, 2018 at 5:25
  • 23
    @VitalyZdanevich, :vertical :term will vertically split with the terminal in the left window. :vertical :botright :term will vertically split with the terminal in the right window. Alternatively, you can manually layout windows, and then use :term ++curwin to open a terminal in the active window.
    – dannyadam
    May 25, 2018 at 16:08
  • 2
    esc ctrl-w w allows you to switch between windows Apr 9, 2020 at 0:02
96

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

2
  • 2
    I think ctrl+z and fg are slightly faster, given you don't have to type :sh in that case.
    – Alex
    Dec 6, 2016 at 17:51
  • 2
    @Alex no you don't have to type :sh if you do the map. After noremap, you can just Ctrl+D from vim to sh, and since ^D is EOF, which terminates the sh, so you would return back to vim. The one key can toggle between sh and vim.
    – InQβ
    Oct 10, 2019 at 1:20
54

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.

4
  • 3
    this works perfectly, thanks! your answer is much more in the spirit of the original question
    – subelsky
    Jun 12, 2019 at 13:36
  • Hi, do you know how to quit from this mode?
    – Coconut
    Jun 13, 2019 at 10:36
  • 2
    like in the normal terminal, exit will quit from the terminal, then use :q to close the window Jun 13, 2019 at 12:31
  • 4
    @user3289737 Alternatively, you can press Ctrl+W twice in a row to switch between windows. This is not specific to terminal windows, but works just as well.
    – Sparkette
    Jun 24, 2019 at 4:24
45

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.

5
  • The terminal runs in a buffer in the same tab. so if you are used to using multiple files in vim, you may face some difficulty. switching buffers will eventually come to the terminal from where it is a point of no return, you have to kill the terminal. as long as you are aware of this, it should be fine. hope the developers come with a workaround for this.
    – ArunMKumar
    Jul 6, 2018 at 4:54
  • 2
    @ArunMKumar Use [Ctrl+W] to "escape" from the terminal, and then do :bn as usual. No need to kill the terminal and it is not a point of no return :)
    – fjardon
    Jul 6, 2018 at 9:09
  • Thanks, buddy, totally missed it. I have keymapped :bn to a key and it stopped working, it is working back again. It was my bad.
    – ArunMKumar
    Jul 10, 2018 at 8:17
  • 2017? You mean 2018?
    – Sparkette
    Nov 3, 2018 at 2:16
  • 1
    @flarn2006, regarding 2017 versus 2018, Vim 8.1 was announced on vim.org on May 18, 2018, with "support for running a terminal in a Vim window" listed as its "main new feature". The feature was first available earlier, on July 7, 2017, in patch 8.0.0693. github.com/vim/vim/commit/…
    – dannyadam
    Apr 24, 2020 at 22:03
45

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

4
  • It was added before vim 8.1. It's just the main big feature that vim 8.0.001 didn't have. That's why it's advertised as a vim 8.1 novelty. See Daniel's answer from oct 2017, and fjardon's one in apr 2018. -> github.com/vim/vim/blob/master/runtime/doc/version8.txt#L18814 May 23, 2018 at 6:39
  • don't get what you mean by "To send a literal ^W input to the terminal, press Ctrl-W ." can you explain? thx
    – abernier
    Mar 12, 2021 at 7:28
  • 1
    @abernier: Sure. What I mean is, say you're running a program in the Vim-terminal that does something when you press Ctrl-W. If you just press Ctrl-W, the program won't see it, because Vim will pick it up instead. So what you do is, you press Ctrl-W, and then you press the period (.) key, and that will make the program in the terminal see the Ctrl-W.
    – Sparkette
    Mar 14, 2021 at 4:52
  • @flarn2006 Is there any way to see the output of long commands? For example, if I cat a long file (in the tab where :term is active), then I can't scroll up or down to see all the output. One way is to use Ctrl-W N and then go back with i, but is there another alternative?
    – ado sar
    Sep 5, 2022 at 14:16
37

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

1
  • This also works on gvim and vim normal, so not only limited to neovim. Jul 24, 2022 at 4:45
20

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

1
  • Care to explain why it was downvoted? (besides being an answer from 6 years ago) Nov 23, 2018 at 23:02
15

It's possible to open a new tab with a terminal in vim since 2017 as @fjardon said:

Just type: :terminal. It will open a tab by default above your current tab.

If you want it to open in another place you can try the following options:

  • :below terminal : open the terminal below current tab.
  • :below vertical terminal : open the terminal always vertically to the right.

You can play with these until you find what you like. After this you can set a map in your .vimrc configuration file, for me, I use:

nmap <leader>tt :below vertical terminal<CR>

This way I can type <space>tt (space my leader key) to open it quickly.

As a side note:

  • You can switch between your tabs (terminal and other buffers) with Ctrl+W Ctrl+W.
  • You can enter an editable mode in your terminal if you want to copy your commands with Ctrl+W N and go to normal terminal mode with i or a.

Cheers!

14

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.

3
  • 1
    NeoVim does allow you to navigate and copy from the terminal output in normal/visual mode. I do this all the time. Using NVIM 0.1.6-dev Oct 27, 2016 at 9:17
  • Thanks, I’ll try VimShell. I’ve taken the liberty of removing an incorrect statement from the answer, however. As @Alice pointed out, NeoVim’s :terminal can absolutely do the same. Feb 9, 2017 at 11:10
  • :terminal cannot do the same as :VimShell in terms of navigation and editing, and in fundamental ways. See explanation edit above. Feb 18, 2017 at 17:24
9

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

Various commands

1
  • So don't use zsh with vim. set shell=/bin/sh will make vim use a quieter shell for its commands. Feb 13, 2015 at 18:18
7

No, you cannot:

http://vimdoc.sourceforge.net/htmldoc/tips.html#shell-window

7
  • 2
    DO NOT TAKE THE ADVICE OF splitvt - it mangles the color control codes and makes vim utterly unusable.
    – new123456
    Jun 24, 2011 at 23:22
  • 17
    "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."
    – trusktr
    Apr 6, 2013 at 1:53
  • 2
    @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.
    – Amber
    Feb 26, 2014 at 22:47
  • 1
    @Amber, Yeah that would be fine except the OP made no mention of using the stock vim. Feb 26, 2014 at 22:50
  • 3
    Someone should just edit this answer to say you cannot do it with stock vim. Apr 7, 2014 at 14:25
5

By far, I have tried a lot of solutions mentioned here, what I really wanted is to keep the terminal open while coding a similar experience in VsCode. Then I came across this solution which is working perfectly for me.

Before Installing:

  • I am using Nvim 0.5 but I think it can work for any version and checked also on vim
  • I am using macOS Catalina Version 10.15.7

Setup your integrated terminal

Step -1-

  • Create a script with the name myQuickTerminal.vim or whatever name you want.
  • Put the following script
"==============================================================================
"
" ▒█▀▀█ █░░█ ░▀░ █▀▀ █░█   ▀▀█▀▀ █▀▀ █▀▀█ █▀▄▀█ ░▀░ █▀▀▄ █▀▀█ █░░
" ▒█░▒█ █░░█ ▀█▀ █░░ █▀▄   ░▒█░░ █▀▀ █▄▄▀ █░▀░█ ▀█▀ █░░█ █▄▄█ █░░
" ░▀▀█▄ ░▀▀▀ ▀▀▀ ▀▀▀ ▀░▀   ░▒█░░ ▀▀▀ ▀░▀▀ ▀░░░▀ ▀▀▀ ▀░░▀ ▀░░▀ ▀▀▀
"
"==============================================================================
" " This is a script that will trigger a terminal quickly than the FloatTerminal
" open new split panes to right and below
"link: https://betterprogramming.pub/setting-up-neovim-for-web-development-in-2020-d800de3efacd
"==============================================================================
set splitright
set splitbelow
" turn terminal to normal mode with escape
tnoremap <Esc> <C-\><C-n>
" start terminal in insert mode
au BufEnter * if &buftype == 'terminal' | :startinsert | endif
" open terminal on ctrl+n
function! OpenTerminal()
  split term://zsh
  resize 10
endfunction
nnoremap <leader> n :call OpenTerminal()<CR>

NOTE: if you want to run bash instead of zsh for a particular reason then replace zsh with bash.

Step -2-

Lets source it, put this in init.vim for neovim or `.vimrc' for vim

source $HOME/.config/nvim/modules/mySpecialScripts/myQuickTerminal.vim

This will be preloaded ahead as you save and resource it, you can use source $MYVIMRC for quick reloading the init.vim file.

Step -3-

I mapped as you can see in the script n to open a terminal in a new pane, my is the (Space bar) and once I click (space + n) a terminal will be triggered and I will enjoy writing my code while the terminal is opened.

  • To quit insert mode in the terminal, press Esc. Now, to switch to the code editor pane, use CTRL+w w. This shortcut can get annoying once you have more than two panels open, so I added the following shortcuts too. I mapped these too for quick jumping among opened panes, use these
" Better window navigation
nnoremap <C-h> <C-w>h
nnoremap <C-j> <C-w>j
nnoremap <C-k> <C-w>k
nnoremap <C-l> <C-w>l

Optional

If you want your terminal to exit the current buffer with key. you can use

tnoremap <Esc> <C-\><C-n>:q!<CR>

But since I want to switch in between several buffers I use without close the terminal I use instead:

tnoremap <Leader><Esc> <C-\><C-n>:q!<CR>

Final results


enter image description here

Reference:

4
  • Whoa, your fonts are f'ed up.
    – mattalxndr
    May 31, 2021 at 10:18
  • Which font and theme are you using? Thanks
    – HuyVu
    Mar 4, 2022 at 14:36
  • 1
    I use: Font style: VictorMono NF SemiBold size 16 (notice NF is for nerdfont) Theme: Gruvbox Dark If you have a problem finding them, I can share with you their links, let me know.
    – Dr Neo
    Mar 8, 2022 at 7:51
  • Thank you so much. I can find them <3
    – HuyVu
    Mar 9, 2022 at 18:45
3

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

3
  • 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+.
    – Yewge
    Aug 6, 2009 at 14:38
  • Changed to appease the pedant. :) (Had to type something longer than 15 characters. 'Fixed' wasn't long enough)
    – romandas
    Aug 6, 2009 at 16:52
  • Shougo/vimshell (github.com/Shougo/vimshell.vim) seems to work rather well. It does require building a dynamic library, but it is quite simple (cd <vim>/vimproc; make) Oct 27, 2015 at 18:53
3

I use this now, you may can try. VimShell

0
3

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
1
  • 1
    Good tip, puts the screen under all the open windows (At least it did that for me) Feb 28, 2021 at 15:44
3

If you are interested in quick answer, here is it: :vert term. It will split your screen vertically and open up terminal.

2

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

2

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.

2

With vim 8.1.3741, just type :terminal to start a terminal inside of vim.

Try map :nnoremap ]t :terminal<CR> to do that quicker!

0

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

0

You could also try Floaterm plug-in. It's quite easy to use. Just type :FloatermToggle() and a floating terminal tab appears in the middle of your vim tab.

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