I can't figure out how to go back to Normal
mode after entering Terminal
in Insert
mode...
Escape just doesn't seem to work.
Any idea?
You are supposed to press CTRL+\CTRL+n.
See :help terminal-emulator
for a mapping suggestion.
:tnoremap <Esc> <C-\><C-n>
Commented
Apr 27, 2021 at 17:25
vim.api.nvim_set_keymap( 't' , '<Leader><ESC>' , '<C-\\><C-n>' , {noremap = true} )
The answer by @romainl is correct but I wanted to add a bit more info for anyone that may have stumbled on this from google like I did.
Directly from the docs we get this note:
Terminal mode has its own namespace for mappings, which is accessed with the "t" prefix. It's possible to use terminal mappings to customize interaction with the terminal. For example, here's how to map to exit terminal mode:
:tnoremap <Esc> <C-\><C-n>
With that knowledge you will most likely need to create some terminal mappings to stay inline with your current workflow (tab/window movement, etc.)
tnoremap kj <C-\><C-n>
tnoremap <leader><Esc> <C-\><C-n>
. <Esc>
is a very common key to use in the terminal, and by remapping just this you can't use <Esc>
in the terminal any more. One bizarre scenario is if you end up in the inception world of starting up vim inside the vim terminal (it happens :)), but there's other examples such as running FZF on the terminal and you want to escape out of the search.
According to vim documentation, there are two ways:
CTRL-W N
(note it's uppercase N)CTRL-\ CTRL-n
Use CTRL-W N (or 'termwinkey' N) to switch to Terminal-Normal mode. Now the contents of the terminal window is under control of Vim, the job output is suspended. CTRL-\ CTRL-N does the same.
tnoremap <Esc> <C-\><C-n>:q!<CR>
In Vim 8, this also works:
tnoremap <ESC> <C-w>:q!<CR>
N
in <c-w>N
, you can simply press :
after <c-w>
. And if you really want to close Vim with all its open buffers and windows, than :qa!
might be the better choice. But one would loose all changes in that case.
Commented
Sep 26, 2018 at 14:33
tnoremap <Esc> <C-\><C-n>:bd!<CR>
, which force closes the terminal buffer but not the entire Vim session.
Commented
Jul 27, 2020 at 17:48
Another simple solution that works for neovim:
If you have the mouse support, simply using the mouse wheel will switch to normal mode.
If you are using Tmux, you may have installed some "vim-like" bindings to move around in the window panes which among other things grab the "C-\" before Vim has a chance to use it. If so, remove these from your .tmux.conf
and restart tmux:
bind-key -n C-\ if-shell "$is_vim" "send-keys C-\\" "select-pane -l"
bind-key -T copy-mode-vi C-\ select-pane -l
You can just press Ctrl + D
in terminal to close it. But if you don't want to close it, just want to switch windows you can do Ctrl-W w
(if you have multiple panes you can specify to which you switch with the seccond letter (h,j,k,l), W just switches vim windows.
Well, I am surprised that no other commment mention it so maybe it's bad practice but I personnaly just typed exit
and it worked.
<Ctrl-D>
feeds an end-of-file keycode, closing most shells, and by extension the terminal. Pressing two keys (with one hand) is quicker than 5 keys (with two hands). :)
In my case (I use <leader>x, to close any buffer), as I have init.lua instead of init.vim my configs are:
-- map helper
local function map(mode, lhs, rhs, opts)
local options = {noremap = true}
if opts then options = vim.tbl_extend('force', options, opts) end
vim.api.nvim_set_keymap(mode, lhs, rhs, options)
end
local autocmds = {
packer = {
{ "BufWritePost", "plugins.lua", "PackerCompile" };
};
terminal_job = {
{ "TermOpen", "*", [[tnoremap <buffer> <Esc> <c-\><c-n>]] };
{ "TermOpen", "*", [[tnoremap <buffer> <leader>x <c-\><c-n>:bd!<cr>]] };
{ "TermOpen", "*", [[tnoremap <expr> <A-r> '<c-\><c-n>"'.nr2char(getchar()).'pi' ]]};
{ "TermOpen", "*", "startinsert" };
{ "TermOpen", "*", "setlocal listchars= nonumber norelativenumber" };
};
restore_cursor = {
{ 'BufRead', '*', [[call setpos(".", getpos("'\""))]] };
};
save_shada = {
{"VimLeave", "*", "wshada!"};
};
resize_windows_proportionally = {
{ "VimResized", "*", ":wincmd =" };
};
toggle_search_highlighting = {
{ "InsertEnter", "*", "setlocal nohlsearch" };
};
lua_highlight = {
{ "TextYankPost", "*", [[silent! lua vim.highlight.on_yank() {higroup="IncSearch", timeout=400}]] };
};
ansi_esc_log = {
{ "BufEnter", "*.log", ":AnsiEsc" };
};
}
nvim_create_augroups(autocmds)
For me what works is ctr+\+n
that combination of keys is what takes me off the insert mode in the terminal mode.
Type exit
into the terminal to kill it and it should close.
How about, simply using the below to delete the terminal session? (Note that !
is important)
:bd!
! is needed because stand bd
fails if there has been any modification in the buffer and when those modifications are not saved.
:help bd
in vim, and that's definitively a command I should put in my toolbox and use all the time, with any sort of buffers not only the terminal mode. Many thanks for the indication ! I edit your answer to add some explanation about the usage/reason of !
Commented
Dec 27, 2022 at 11:45
I just press i
to go into insert mode.
bd!