How do I run a terminal inside of Vim?
Asked Answered
D

25

416

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.

Durkin answered 6/8, 2009 at 1:36 Comment(4)
related: https://mcmap.net/q/87338/-how-to-temporarily-exit-vim-and-go-back/1569Laevo
Is :! <command> what are you looking for?Encephalitis
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).Sasnett
Using :term, to quit from terminal split, use ctrl W and :qAdiell
M
223

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.

Matildematin answered 6/8, 2009 at 2:9 Comment(16)
+1 I only just recently recieved the screen gospel. I wonder how I lived without it.Mafia
You can also use screen with Vim plugin Vicle to send commands edited in vim to a screen session.Vitrify
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.Spite
If I could I'd vote twice for this. I heard about screen long ago but never used it. This tip will save me a LOT of time. thanks.Blastula
Unfortunately, using a vertical split slows down STDOUT (at least on my machine). ls -R is a lot slower with the split than without, for example.Sastruga
You can use :! to execute system commands from within vim. It's not a complete terminal but it's still useful for quick stuff. Vim 7 allows you to tab and autocomplete so if you're just looking for a file it's great. Things like logging into mysql/opening an ssh terminal really should be done through Screen or byobuLaughlin
in this video, blogs.jetbrains.com/pycharm/2011/03/… at 32:30 , it shows how emacs/terminal integration can be nice for debugging... when you step into a function using the debugger in the shell window, the editor window auto-updates to show the location in the code where the debugger is currently at. This shows how emacs seems closer to being like an IDE than vim. It would be nice if VIM had this ability. (I'm a VIM user, not an emacs user)Outlet
i would rather choose tmux instead of screen.Eudemonia
I like screen as much as the next guy, but -1 as the comment around Conque better answers the question.Jamal
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.Stigmasterol
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.Quark
My opinion is same. This is a good option, but it doesn't answer the questionPhelia
This answer should be updated due to new vim 8.1 feature Terminal WindowCockshy
Doesn't answer the question as far as I understand it. Running vim in screen or tmux isn't the same as running a terminal inside of vim and all.Capybara
Unfortunately if you're using screen or tmux instead of a GUI vim (like MacVim), then you're not going to have sane text selection (e.g., turn on line numbers or use a split and try to copy/paste multiple lines with mouse) and you're going to need to connect the vim clipboard with the system clipboard.Colonize
flagged as not an answer because I've always felt this answer was inappropriate on the question. There was a real answer even at the time of this answer, and now there's an even better one since vim comes with the feature. I think this answer should at least have its acceptance revoked.Robbirobbia
T
235

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.

Timbuktu answered 6/8, 2009 at 1:47 Comment(3)
And if you want to copy the command's output to the current buffer: :r! dirFribourg
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).Garrison
Nice answer! To compliment it even further, I would also add the :r! command [ENTER] mentioned in the comments.Measles
N
234

Outdated from August 2011

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

Nydia answered 26/5, 2010 at 14:45 Comment(12)
:ConqueTerm bash <CR>, vim <CR>, :ConqueTerm bash.... the recursive horror!!Tsarevitch
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.Derris
In order to understand recursion, you must first understand recursion.Stigmasterol
@Łukasz Niemier VIMCEPTIONEringo
Works with zsh, sweet! d-_-bWeakly
@psihodelia now it does :DRosenarosenbaum
@hauleth Actually, I do have a use case for that. Unfortunately some commands are run by the innermost vim, while other commands are run by the outermost vim, sigh...Rosenarosenbaum
I love Conque plugin and also Conque-GDB to debug with standard C/C++ inside vim that acta as a full IDE. BUT if you need multiwindows fast scrolling / realtime printout, I find that tmux is a better solution. Please read the answer here below about ViMUX: a tmux/vim integrationMandel
Don't think you need Conque for vimception, though? You can just do !bash, then vim, then !bash, then vim and so on. Or just do !vim then !vim then !vimNita
any chance you know how to install this from a script instead of manually starting vim and using :so %Berriman
:ConqueTermVsplit bash <CR>, Then <esc> to get out of here.Tlingit
It would seem that this is no longer supported, anyone have an alternate?Efren
M
223

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.

Matildematin answered 6/8, 2009 at 2:9 Comment(16)
+1 I only just recently recieved the screen gospel. I wonder how I lived without it.Mafia
You can also use screen with Vim plugin Vicle to send commands edited in vim to a screen session.Vitrify
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.Spite
If I could I'd vote twice for this. I heard about screen long ago but never used it. This tip will save me a LOT of time. thanks.Blastula
Unfortunately, using a vertical split slows down STDOUT (at least on my machine). ls -R is a lot slower with the split than without, for example.Sastruga
You can use :! to execute system commands from within vim. It's not a complete terminal but it's still useful for quick stuff. Vim 7 allows you to tab and autocomplete so if you're just looking for a file it's great. Things like logging into mysql/opening an ssh terminal really should be done through Screen or byobuLaughlin
in this video, blogs.jetbrains.com/pycharm/2011/03/… at 32:30 , it shows how emacs/terminal integration can be nice for debugging... when you step into a function using the debugger in the shell window, the editor window auto-updates to show the location in the code where the debugger is currently at. This shows how emacs seems closer to being like an IDE than vim. It would be nice if VIM had this ability. (I'm a VIM user, not an emacs user)Outlet
i would rather choose tmux instead of screen.Eudemonia
I like screen as much as the next guy, but -1 as the comment around Conque better answers the question.Jamal
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.Stigmasterol
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.Quark
My opinion is same. This is a good option, but it doesn't answer the questionPhelia
This answer should be updated due to new vim 8.1 feature Terminal WindowCockshy
Doesn't answer the question as far as I understand it. Running vim in screen or tmux isn't the same as running a terminal inside of vim and all.Capybara
Unfortunately if you're using screen or tmux instead of a GUI vim (like MacVim), then you're not going to have sane text selection (e.g., turn on line numbers or use a split and try to copy/paste multiple lines with mouse) and you're going to need to connect the vim clipboard with the system clipboard.Colonize
flagged as not an answer because I've always felt this answer was inappropriate on the question. There was a real answer even at the time of this answer, and now there's an even better one since vim comes with the feature. I think this answer should at least have its acceptance revoked.Robbirobbia
C
195

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.
Cari answered 11/8, 2009 at 3:15 Comment(7)
:!bash is a nicer way of doing thatIntricate
I think he wants a kind of dreampie like functionalityElevon
@Oatman Why is :!bash nicer than Ctrl+z? (Just wondering)Eringo
@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 betterGrogshop
+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.Ungracious
@Grogshop in my experience, i using :! /bin/bash and doing everything in a bash including change directory, pulling from git, compiling, run test, run apps. after that, i just type exit to exit from bash and get back to vim. CMIIWPerch
You can also make a "magic ctrl-z" in zsh that allows you to ctrl-z again to return to vim (see VII): sheerun.net/2014/03/21/how-to-boost-your-vim-productivity/aDotation
P
164

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.

Plentiful answered 19/10, 2017 at 3:9 Comment(6)
+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.Intercolumniation
Is it possible to vertical split using :term?Sapienza
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!Shekinah
@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.Plentiful
esc ctrl-w w allows you to switch between windowsMede
@user1869582, On my vim installation, Ctrl-w w alone switches windows (i.e., without an initial <esc>).Plentiful
K
99

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

Keep answered 25/8, 2011 at 4:59 Comment(2)
I think ctrl+z and fg are slightly faster, given you don't have to type :sh in that case.Breebreech
@Breebreech 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.Discretion
M
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.

Mercie answered 19/5, 2019 at 1:35 Comment(4)
this works perfectly, thanks! your answer is much more in the spirit of the original questionStretcherbearer
Hi, do you know how to quit from this mode?Oilcloth
like in the normal terminal, exit will quit from the terminal, then use :q to close the windowMercie
@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.Axum
P
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.

Posticous answered 4/4, 2018 at 19:16 Comment(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.Esbensen
@Esbensen 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 :)Posticous
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.Esbensen
2017? You mean 2018?Axum
@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/…Plentiful
A
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.

Axum answered 22/5, 2018 at 19:44 Comment(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#L18814Phenanthrene
don't get what you mean by "To send a literal ^W input to the terminal, press Ctrl-W ." can you explain? thxKerstin
@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.Axum
@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?Thuthucydides
R
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

Robbirobbia answered 27/3, 2015 at 4:15 Comment(1)
This also works on gvim and vim normal, so not only limited to neovim.Queenstown
I
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

Infusion answered 7/11, 2012 at 0:49 Comment(1)
Care to explain why it was downvoted? (besides being an answer from 6 years ago)Infusion
N
16

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!

Nichol answered 12/6, 2021 at 21:7 Comment(0)
E
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.

Everett answered 19/5, 2016 at 1:51 Comment(3)
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-devFreshen
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.Bracing
:terminal cannot do the same as :VimShell in terms of navigation and editing, and in fundamental ways. See explanation edit above.Everett
S
9

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

Various commands

Spillman answered 10/8, 2009 at 19:10 Comment(1)
So don't use zsh with vim. set shell=/bin/sh will make vim use a quieter shell for its commands.Mooncalf
K
7

No, you cannot:

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

Kinghood answered 6/8, 2009 at 1:39 Comment(7)
DO NOT TAKE THE ADVICE OF splitvt - it mangles the color control codes and makes vim utterly unusable.Battledore
"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."Persephone
Perhaps you want to delete this answer now? Since it is clearly possible.Slunk
@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.Kinghood
@Amber, Yeah that would be fine except the OP made no mention of using the stock vim.Slunk
@AnishRamaswamy I think we'll just have to agree to disagree here.Kinghood
Someone should just edit this answer to say you cannot do it with stock vim.Rosenarosenbaum
P
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:

Petulancy answered 25/4, 2021 at 11:51 Comment(4)
Whoa, your fonts are f'ed up.Dacron
Which font and theme are you using? ThanksHinojosa
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.Petulancy
Thank you so much. I can find them <3Hinojosa
N
3

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

Nowise answered 6/8, 2009 at 1:40 Comment(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+.Spite
Changed to appease the pedant. :) (Had to type something longer than 15 characters. 'Fixed' wasn't long enough)Nowise
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)Methodism
A
3

I use this now, you may can try. VimShell

Attention answered 7/12, 2014 at 14:23 Comment(0)
Z
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
Zoilazoilla answered 3/6, 2020 at 2:4 Comment(1)
Good tip, puts the screen under all the open windows (At least it did that for me)Karlakarlan
A
3

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

Astilbe answered 7/4, 2021 at 10:9 Comment(0)
C
3

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!

Circumambient answered 19/5, 2022 at 3:36 Comment(0)
G
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

Galarza answered 22/10, 2013 at 14:16 Comment(0)
B
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.

Bartizan answered 16/1, 2020 at 6:32 Comment(0)
B
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

Brewing answered 21/5, 2020 at 11:47 Comment(0)
C
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.

Cupboard answered 14/4, 2023 at 9:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.