Skip to content

Instantly share code, notes, and snippets.

@mahemoff
Last active September 29, 2024 16:52
Show Gist options
  • Save mahemoff/8967b5de067cffc67cec174cb3a9f49d to your computer and use it in GitHub Desktop.
Save mahemoff/8967b5de067cffc67cec174cb3a9f49d to your computer and use it in GitHub Desktop.
Vim Terminal Mode - A short introduction

Vim has a Terminal Mode!

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.

Key Bindings

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>

Navigation Tips

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.

In-Terminal Tips

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.

@martasd
Copy link

martasd commented Aug 13, 2022

Hi @mahemoff. When I use the ++close option to launch zsh shell, I get the following error:

 zsh: bad option: -+

 [Process exited 1]

Do you know how to pass this option if the shell is zsh?

@Samuelodan
Copy link

Seems the ++close option isn't required anymore, cos I can't tell a difference with and without. exit just closes the terminal window as expected. Thanks for the helpful tips.

@malcomx1121
Copy link

:vert term
to open vertical terminal window

@Samuelodan
Copy link

Samuelodan commented Nov 10, 2022

:vert term to open vertical terminal window

Nice! @malcomx1121

@soumit-r-mitruka
Copy link

move vim active directory to last cd on terminal?? (thinking)

@WebDragon
Copy link

move vim active directory to last cd on terminal?? (thinking)

I mapped out an FKey for changing the active directory to that of the current file, long ago; maybe this could be additionally repurposed for this?

" map 'F12' to change the pwd of vim to the cwd of the current file
" and yes there's reasons why we do all this extra folderol
noremap :cd =expand("%:p:h")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment