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

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.


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.

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

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.

Tags:

Vi

Vim