Since v8.1 (May 2018), Vim has shipped with a built-in terminal. See https://vimhelp.org/terminal.txt.html or type :help terminal
for more info.
Why use this? Mainly because it saves you jumping to a separate terminal window. You can also use Vim commands to manipulate a shell session and easily transfer clipboard content between the terminal and files you're working on.
Here are some simple key bindings to help you spin up a new terminal easily. As they use the ++close
option, they'll automatically exit when the terminal exits, so just type exit
or ctrl-D
. Or to force-close it via Vim, ctrl-w q!
" vim-powered terminal in split window
map <Leader>t :term ++close<cr>
tmap <Leader>t <c-w>:term ++close<cr>
" vim-powered terminal in new tab
map <Leader>T :tab term ++close<cr>
tmap <Leader>T <c-w>:tab term ++close<cr>
To be proficient with this, get familiar with Vim's regular window and tab features because terminal is built directly on top of them. The terminal is just another editor window with some special handling.
With terminal in a split window, you can use Vim's regular window commands, e.g. ctrl-w n
and ctrl-w p
to move between next and previous windows in the current tab (which could be a regular editing window or another terminal, it doesn't matter). See :help windows
.
With terminal in a separate tab, again it's just regular Vim stuff. You can navigate Vim's regular window commands, e.g. <c-w> n
and <c-w> p
to move between next and previous windows in the current tab (which could be a regular editing window or another terminal, it doesn't matter). See :help tabpage
.
Mostly you'll use the terminal like any other terminal and just type in shell commands and so on. There's a few optional extras that will help you interact with the rest of Vim.
Use ctrl-w N
to switch to "terminal-normal mode", which will let you navigate around. It could be useful to then copy content to the clipboard. Then return to regular terminal mode, simply type i
just like how you'd enter insert mode from a regular window.
ctrl-w :
will open command mode like in regular Vim.
ctrl-w ""
will paste, which is useful if you want to enter something from a file in another window. More generally, you can paste recent or saved clipboard contents using :ctrl-w " {reg}
, where reg
identifies the register to paste. Type ctrl-w :display
(or ctrl-w :dis
) to see all available registers and their content.
Nice! @malcomx1121