Is it possible to split a window in Vim/Vi with a terminal?
Asked Answered
F

8

51

Is there a way to split a window inside Vi/Vim so that one window will be a terminal?

P.S. Solutions like installing new text editors and such will not help me.

Fugacious answered 12/7, 2016 at 13:6 Comment(7)
I'd recommend using something like tmux, split the console window, use one split for vimIsolating
As far as I know, splitting vim and giving half screen to terminal won't work. Vim is just a editor , dont expect it to be a terminal.Syncopated
neovim has a terminal in a split. :help :terminalCharlatanism
@Fugacious It would help us if you explained why you can't use solutions like tmux or screen. These are the natural solutions to problems like this, and will usually produce better results than other solutions (like conque_term). Understanding why you can't use them will help us to suggest something that you can use.Headrail
Possible duplicate of How do I run a terminal inside of Vim?Sherilyn
Why not tmux is that if the terminal is in vim/neovim, you can hop in and out of edit mode the same way. That means that when not in insert mode, you can use vim commands on the text buffer from the terminal for e.g. copy paste. So you can yank text from a text buffer, paste it to the terminal, and then hit a for append and then enter to execute. You use the awkward C-\ C-n to get out (rationale being there is no practical use for this combo in a terminal). Then you can e.g. column select text in the terminal, yank it, and paste it. Etc.Cf
It does have a lot of uses you can't replicate with tmux. (By default, I use tmux, but I like vim's terminal. Also my shared webhost only has vim, no tmux, and so the vim terminal is the only option available.)Cf
S
8

There's no way to do this without a plugin. Here are a couple of ways to get similar functionality.

  • Use tmux, or another terminal window manager. In response to your P.s., tmux is not another text editor. It just allows you to split your terminal screen, so you would still be using vim for your text editing.
  • You can also run terminal commands and view the output from inside vim. Just run a command, but preface it with an exclamation point. For example, if you run :!ls from within vim, you will see a list of the files in your current directory. Any other commands such as :!pwd or :!git add * will also work. If you want to read the output of a command into your current vim buffer you can use the read command. For example, if you run :read !ls vim will enter a list of the files in your current directory into your current buffer at the cursor position.
Shrine answered 12/7, 2016 at 19:48 Comment(1)
This should no longer be the accepted answer. See the :vertical terminal answer from @Plummet at https://mcmap.net/q/344576/-is-it-possible-to-split-a-window-in-vim-vi-with-a-terminal.Courier
P
116

In Vim 8, if it is compiled with the +terminal option, you can split the current window horizontally and add a terminal with the command :terminal or :term for short-hand.

enter image description here

If you want to split the window vertically, the best way I know is to do a regular vertical split with :vsp or <c-w>v. Then, split one of the windows to have a terminal window (:term), then finally move to the smaller, non-terminal window and close it.

enter image description here

Edit: ...and literally right after I wrote this I found how to easily vertically split the terminal window...

:vertical terminal

" OR

:vert term

The terminal will open in something similar to insert mode, and pressing <c-w>N will put you in the "normal" mode where you can have regular Vim motions and can run Vim commands. Note that in many shells (I know for sure in Bash and Zsh), you can run set -o vi to be able to hit <c-[> or <esc> and use Vim motions anyways. The best part about that is hitting v when in "normal" mode where the current command is opened in a new Vim instance and is run upon exiting Vim.

Plummet answered 18/7, 2018 at 13:36 Comment(3)
Instead of <c-w> you can use exit and then :q!Ambiguous
But.. how do I close the terminal window?Wardrobe
@Wardrobe Fastest way is to do Ctrl+D when at the bash prompt like you normally would.Plummet
S
15

Maybe adding the string

rightb vert term

or

bel vert term

to your .vimrc (hidden file with editor settings; it is in the user home directory by default: ~/.vimrc) will solve your problem. Thus, if you type vim file_name.txt in terminal emulator, you will get two split windows: on the left side - txt-file, on the right - terminal emulator window.

ps: you can move between split windows with ctrl + double "w" (press "w" two times).

also, from my experience, the "term"-command is not supported in 8.0 vim version, unlike 8.2 version.

Swelling answered 16/12, 2018 at 12:53 Comment(0)
S
8

There's no way to do this without a plugin. Here are a couple of ways to get similar functionality.

  • Use tmux, or another terminal window manager. In response to your P.s., tmux is not another text editor. It just allows you to split your terminal screen, so you would still be using vim for your text editing.
  • You can also run terminal commands and view the output from inside vim. Just run a command, but preface it with an exclamation point. For example, if you run :!ls from within vim, you will see a list of the files in your current directory. Any other commands such as :!pwd or :!git add * will also work. If you want to read the output of a command into your current vim buffer you can use the read command. For example, if you run :read !ls vim will enter a list of the files in your current directory into your current buffer at the cursor position.
Shrine answered 12/7, 2016 at 19:48 Comment(1)
This should no longer be the accepted answer. See the :vertical terminal answer from @Plummet at https://mcmap.net/q/344576/-is-it-possible-to-split-a-window-in-vim-vi-with-a-terminal.Courier
S
8

For anyone using NeoVim:

The highest voted answer uses the vim commands. This doesn't work on NeoVim (at least for me). However, it's still fairly simple:

:vsplit term://bash

term:// is a NeoVim way of opening a terminal

bash is the kind of shell you want to use (e.g. I use zsh, so my command is actually :tabe term://zsh)

Some helpful commands that I created:

" open terminal
if has('nvim')
    command Terminal vsplit term://zsh
    command TerminalTab tabe term://zsh
else
    command Terminal vert term
    command TerminalTab tab ter
endif
Shaky answered 21/1, 2022 at 10:26 Comment(2)
Note the split window will be in NORMAL MODE, press i to enter TERMINAL MODE, and CTRL+\ CTRL+N to leave and back to NORMAL MODEStreamliner
@Streamliner true :) , I find that annoying, I use an autocommand to enter terminal mode automatically: vi.stackexchange.com/a/36586/40187Shaky
M
4

No need for extra plugins, just vanilla Vim.

Vim

:term will open a horizontal split with a terminal.

:vert term will open a vertical split with a terminal.

By default, you will be in insert mode and the terminal will behave normally.

Ctrl+(\,n) to go to normal mode in the terminal window, in case you want to yank some output, or change some setting. Read as: Press and hold Control Key, then press back-slash, then press n.

To go back into normal terminal behavior just go back to insert mode, for instance by pressing i in normal mode. To close the terminal just exit the terminal by executing exit command.

Note: there are problems with vim's understanding of a buffer and how the terminal works. For instance, if you close the terminal the split will close. For this reason, for an intense use of the terminal this might not be the best option.

NeoVim

Previous answer still applies but, by default, the terminal will start in normal mode. So you will have to go to insert mode like explained previously.

In case line numbering is visible :set nonu nornu while in normal mode.

Mugwump answered 28/12, 2022 at 19:34 Comment(0)
G
2

The plugin conque_term gives you the possibility to run a shell inside the vim buffer.

BUT I recommend screen linux command which has almost the same behavior as vim (especially the moving commands) when entering the copy mode by executing Ctrl+a Esc

Gaucherie answered 12/7, 2016 at 15:3 Comment(0)
C
1

This is what I have in my .vimrc:

if has('nvim') 
  nnoremap <C-w><C-t><C-v> :vsplit +term<cr>
  nnoremap <C-w><C-t><C-t> :split +term<cr>
  nnoremap <C-w><C-t><C-y> :term<cr>
else
  nnoremap <C-w><C-t><C-v> :vert term<cr>
  nnoremap <C-w><C-t><C-t> :term<cr>
  nnoremap <C-w><C-t><C-y> :tabnew +term<cr><C-\><C-n><C-w>j:q<cr>i
endif
Cf answered 20/10, 2023 at 9:10 Comment(0)
S
1

No one answered this command so I thought I should

:belowright terminal

it says what it does open terminal below

:rightbelow vertical terminal

same way you can open a terminal on the right side vertically

also if you don't use the belowright command or the rightbelow command the opposite will happen as in terminal will be created top and for the second command on the left side.

why i provided these 2 solutions with the rightbelow and belowright command is because they don't shift the code

and then if you want to switch between these 2 or more window panes do ctrl + w + w

note these commands only work in vim and none of the commands in this post work in vi editor

my answer is similer to uch's answer but i also added the below split command

happy coding

Sumptuous answered 20/4 at 17:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.