Suppose ALT+h to mapped to <C-\><C-N><C-w>j as following Nvim documentation: nvim_terminal_emulator. We have two horizontally split windows with terminal buffers and the right window is activated.

After hitting ATL+h, the left window will be activated but the right window will be switched to normal mode automatically. Is it possible to let the right window stay in terminal mode for neovim? Or does neovim has equivalent terminal-typing which allows us to not leave the terminal mode.


It's impossible "to stay" when you have already "left". What you can do is to re-enter terminal-insert mode automatically.

Sort of,

if has('nvim')
    augroup nvim_terminal | au!
        " entering terminal buffer for the first time
        autocmd TermEnter term://* startinsert
        " switching to terminal window/buffer
        autocmd BufEnter term://* startinsert
    augroup end

See also this question about problem with switching to terminal window by mouse click.

UPD. :h <Cmd> pseudokey specific to Neovim is supposed to answer this problem:

if has('nvim')
    tnoremap <alt-h> <Cmd>wincmd h<CR>
    tnoremap <alt-j> <Cmd>wincmd j<CR>
    tnoremap <alt-k> <Cmd>wincmd k<CR>
    tnoremap <alt-l> <Cmd>wincmd l<CR>

however currently it doesn't preserve terminal-insert mode and switches to terminal-normal anyway due to the bug tnoremap wincmd doesn't preserve mode · Issue #12246 · neovim/neovim. You have to wait for a newer release of neovim which will fix this bug. Or you can manually merge this pull request and build your own neovim.

  • Thanks for the answer! But sometimes I don't want to re-enter terminal-insert mode automatically. For example after running a large test suite in neovim's terminal, I need to go back and forth between test output and source code to find out why some tests failed. When switching back to the test output, I want to check the output in the middle of the screen but startinsert will automatically scroll the screen to bottom. The problem for neovim is in order to run a command in terminal-insert mode, we have to leave this terminal-insert mode via <C-\><C-N> first.
    – Coiby
    Jun 1 '20 at 14:30
  • Considering we can run a command in insert mode without leaving insert mode, for example, <C-r> REGISTER to paste some text from a register, it doesn't make sense we have to leave terminal-insert mode first to run a command.
    – Coiby
    Jun 1 '20 at 14:39
  • This is reported as a bug tnoremap <Cmd>wincmd doesn't preserve mode · Issue #12246 · neovim/neovim on github. Hopefully it will be fixed soon.
    – Coiby
    Jun 1 '20 at 15:03
  • @Coiby Note that the issue is about Neovim-specific <cmd>. <c-\><c-n> should and will work the way it does.
    – Matt
    Jun 1 '20 at 15:16
  • Yes. Sorry for not being clear enough. What I mean is that <Cmd> is supposed to be the answer to my question but the aforementioned bug makes it impossible to "stay in terminal-insert mode" after running a command.
    – Coiby
    Jun 1 '20 at 15:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.