146

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?

Matthias Braun
  • 474
  • 4
  • 17
Stephane Rolland
  • 1,857
  • 2
  • 13
  • 23
  • 5
    Why does this feel like How do I exit Vim? all over again? :) – icc97 May 16 '21 at 10:46
  • @icc97 It doesn't really feel like it for me. really. I ask: hey Escape does not work as I think it should do in terminal mode. My question contains the words "Normal mode" "Teminal" "Insert mode" and the "Escape" key. The Vim meme related to exiting vim, is more targeted at pure beginners on one part. And total experts that challenges the most funny ways to do it on the other part. – Stephane Rolland May 16 '21 at 13:49
  • 1
    Well it had me spamming various keys trying to guess :) Plus the only way I solved it was googling again to find this answer. – icc97 May 16 '21 at 15:28
  • 1
    @icc97 yeah, imagine my feeling googling about this question and to find after a click my very own question and then reading your comment about googling this again. Sort of funny and recursive :) The good thing is that today I learn a new vim/neovim command thanks to one of the new answers: bd! – Stephane Rolland Dec 27 '22 at 12:05

13 Answers13

159

You are supposed to press CTRL+\CTRL+n.

See :help terminal-emulator for a mapping suggestion.

Alex Nolasco
  • 103
  • 3
romainl
  • 40,486
  • 5
  • 85
  • 117
  • 2
    In Vim 8.2 it's :h Terminal-mode. – MikaelF Feb 20 '20 at 03:32
  • I guess it's not exactly NeoVim, but this also works for the Terminal mode added to vanilla VIM – polynomial_donut May 18 '20 at 20:26
  • or use <c-w><N> (uppercase "N" !!!) as mentioned below https://vi.stackexchange.com/a/27089/2239 by @Xuan. for me this is more vim-natural then <c-> – MacMartin Sep 07 '20 at 12:27
  • 7
    What kind of "shortcut" is this? It's not natural at all! To remap to Esc use :tnoremap <Esc> <C-\><C-n> – adelriosantiago Apr 27 '21 at 17:25
  • 1
    @adelriosantiago Vim comes with very few "shortcuts". This is not one of them and no one is pretending it to be one but you. – romainl Apr 27 '21 at 17:34
  • 1
    If mapping this in init.lua for neovim, you'll need to escape the backslash, so use: vim.api.nvim_set_keymap( 't' , '<Leader><ESC>' , '<C-\\><C-n>' , {noremap = true} ) – Chris Jun 11 '21 at 10:40
  • For me, only the mapping worked – Ralph Aug 23 '22 at 18:21
  • Is there any point of normal mode on a terminal window though? I want to go back to my file not stay in the terminal interface :-( – samuelnihoul Oct 31 '22 at 10:30
  • @samuelnihoul, I think that whole :terminal thing is pointless to begin with, so normal mode in terminal… As for your use case, I would argue that, if you don't want to interact with the terminal, then there is no point poping up a terminal in the first place. – romainl Oct 31 '22 at 11:58
  • 1
    @samuelnihoul you can enter visual mode, select stuff, yank it, go to another buffer and paste it there etc. etc. – CervEd Dec 20 '22 at 09:36
83

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.)

Dan Bradbury
  • 1,216
  • 8
  • 10
  • 1
    Hi, I set up that exact mapping, but I'm pressing and nothing happens. Any idea why? – Kevin Burke Jun 02 '16 at 01:16
  • if you add it from Ex mode are the results the same? might be updating the wrong config file.. – Dan Bradbury Jun 02 '16 at 01:29
  • 2
    Thanks. This helped me add my preferred mapping of tnoremap kj <C-\><C-n> – Nick Merrill Oct 30 '16 at 17:57
  • 4
    I made a slight adjustment to include the leader: 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. – icc97 May 16 '21 at 10:31
  • 1
    I just implemented this map myself and noticed that it breaks fzf, where you can not use escape anymore to close the file search. So keep that in mind if you are using fzf and instead go with the suggestion of @icc97. – Peter Lehnhardt Sep 02 '22 at 13:54
15

According to vim documentation, there are two ways:

  1. CTRL-W N (note it's uppercase N)
  2. 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.

Xuan
  • 251
  • 2
  • 2
8

Exit & close

tnoremap <Esc> <C-\><C-n>:q!<CR>

In Vim 8, this also works:

tnoremap <ESC> <C-w>:q!<CR>
Serge Stroobandt
  • 841
  • 9
  • 22
  • 1
    You don't need the 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. – Christian Brabandt Sep 26 '18 at 14:33
  • 3
    Much better is tnoremap <Esc> <C-\><C-n>:bd!<CR>, which force closes the terminal buffer but not the entire Vim session. – Josh Friedlander Jul 27 '20 at 17:48
5

Another simple solution that works for neovim:

If you have the mouse support, simply using the mouse wheel will switch to normal mode.

piertoni
  • 151
  • 1
  • 4
  • 2
    Welcome to [vi.se]! This actually doesn't work for me, which is surprising, since I do that all the time for tmux. (vim 8.2.4050 + alacritty) – D. Ben Knoble Jan 27 '22 at 13:45
  • 1
    Hi! And thanks for feedback. I have tested it with neovim 0.6.0 on Windows and neovim 0.4.4 on Linux and seems to work fine. I tried on Vim 8.2.1522 on Windows and it does not work. Seems like to work only for neovim. If you agree I can edit the answer to address only neovim... – piertoni Jan 27 '22 at 14:29
  • 2
    Yes that sounds fine – D. Ben Knoble Jan 27 '22 at 14:42
  • Most of the answers refer to vim but this one actually addresses neovim. – user8162 Mar 18 '23 at 12:31
3

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

Mark Stosberg
  • 511
  • 4
  • 7
2

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.

  • Ctrl + D is not an option as it exits the terminal. I just wanted to go back to Normal mode. On the contrary, your suggesting "changing window" is a nice option, since it is often the case I have another window pane. +1. – Stephane Rolland Feb 06 '21 at 23:10
1

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.

cassepipe
  • 385
  • 2
  • 13
  • 8
    Not bad practice, but you’ve misunderstood the Q i think. The idea is usually to keep the terminal running, but navigate it in normal mode for a brief moment. Your solution ends the process (and doesnt work for non-shell-like things), while the ctrl-\ ctrl-n allows me to navigate, yank, etc., and then press i and keep working in that terminal – D. Ben Knoble Jun 25 '20 at 12:29
  • 1
    Just to append to the answer, <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). :) – mazunki Nov 06 '20 at 09:24
1

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-&gt;<c-n>]] }; { "TermOpen", "", [[tnoremap <buffer> <leader>x <c-&gt;<c-n>:bd!<cr>]] }; { "TermOpen", "", [[tnoremap <expr> <A-r> '<c-&gt;<c-n>"'.nr2char(getchar()).'pi' ]]}; { "TermOpen", "", "startinsert" }; { "TermOpen", "", "setlocal listchars= nonumber norelativenumber" }; }; restore_cursor = { { 'BufRead', '', [[call setpos(".", getpos("'&quot;"))]] }; }; 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)

SergioAraujo
  • 1,165
  • 11
  • 12
-1

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.

Stephane Rolland
  • 1,857
  • 2
  • 13
  • 23
  • Just come back from :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 ! – Stephane Rolland Dec 27 '22 at 11:45
  • I removed the +1 because when in terminal mode, and while interacting with the shell, I just simply cannot enter anything in command mode, so simply cannot type :bd! . So as to type :bd! I need to use the accepted answer's Ctrl+\ Ctrl+n and then only I can act in neovim as usual – Stephane Rolland Jul 01 '23 at 08:44
-1

Type exit into the terminal to kill it and it should close.

unrealapex
  • 277
  • 1
  • 9
  • 3
    Hey thanks for your first contribution on this site! If you look at cassepipe's answer and its comments you'll notice that you don't really answer OP's question. If you want to participate more on this site, my friendly advice would be to try to answer more recent questions with less existing answers: There's more chances your answer gets noticed and help other people :) – statox May 10 '22 at 08:30
-1

For me what works is ctr+\+n that combination of keys is what takes me off the insert mode in the terminal mode.

-2

I just press i to go into insert mode.

Dom
  • 3,314
  • 5
  • 21
  • 38
  • 2
    Welcome to [vi.se]! I think you've missed the thrust of the question, which is to go to (Terminal) Normal mode, not Insert mode – D. Ben Knoble Jun 02 '22 at 18:04