26

I have the following mappings to make it easy to move out of a terminal pane in neovim:

tnoremap <C-h> <C-\><C-n><C-w>h
" Workaround since <C-h> isn't working in neovim right now
tnoremap <C-w>h <C-\><C-n><C-w>h
tnoremap <C-j> <C-\><C-n><C-w>j
tnoremap <C-k> <C-\><C-n><C-w>k
tnoremap <C-l> <C-\><C-n><C-w>l

The only problem is that when I move back into the terminal pane, I have to press i to get back into insert mode. I always want it to be in insert mode when I move into a neovim pane that is running terminal. What's the best way to achieve this?

4 Answers 4

31

A terminal buffer name always starts with term://:

autocmd BufWinEnter,WinEnter term://* startinsert

Note that this will always put you in insert mode when you move to the terminal window, regardless of the mode you were using when leaving the window.


You can also use a similar autocommand to always get back in normal mode when you leave the terminal window, no matter how you leave this window:

autocmd BufLeave term://* stopinsert

5
  • With this solution, I experience this problem. Would you know of a way around it? Apr 19, 2018 at 10:18
  • The BufEnter autocmd looks like it does not have this problem. Apr 19, 2018 at 10:49
  • This did not work for me, but this answer (vi.stackexchange.com/a/22956/13656) did Jul 22, 2022 at 15:16
  • The first autocmd only worked for me when I switched the command to silent! normal i.
    – Cookie04
    Aug 18 at 18:57
  • for those using lua vim.api.nvim_create_autocmd({ "TermOpen", "WinEnter term://*" }, { command = "startinsert" }) ```
    – Will Munn
    Sep 7 at 13:50
14

On neovim, you can do:

          autocmd TermOpen * startinsert

Found in :help :terminal .

3

Entering a terminal buffer sets buftype to terminal, you can actually use this instead:

:au BufEnter * if &buftype == 'terminal' | :startinsert | endif
2
  • 1
    I found that :startinsert does not work for native vim 8 terminals as of this post. It is actually explicitly stated in :help terminal that, ":startinsert is ineffective." I found that exec 'normal! i' works instead.
    – Kevin
    Feb 6, 2018 at 15:11
  • 1
    "I found that :startinsert does not work for native vim 8 terminals as of this post." That's sad. Vim has lots to learn from NeoVim.
    – trusktr
    Mar 2, 2018 at 3:52
1

Or even slightly better:

  function! TerminalOptions()
    " /* folded code */
    silent! au BufEnter <buffer> startinsert!
    silent! au BufLeave <buffer> stopinsert!
  endfunction
  au TerminalOpen * call TerminalOptions()

Which the <buffer> will make the au command to be applied locally, so only for the termial buffer type. This is also usefull for changing so setting for the terminal only. Such as swtich from relative lines to absolute ;)

Also..... I cant get :startinsert to work, even if all docs point to that. I had to do the following:

    au BufEnter <buffer> exec "normal! i"
2
  • Note that silent needs to be silent! and go before the actual command, e.g. I ended up with autocmd BufEnter <buffer> silent! exec "normal! i". (Vim 8.2) Jun 3, 2021 at 0:29
  • Thanks ! I've update the post ;)
    – aemonge
    Jun 10, 2021 at 8:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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