14

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?

17

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

  • With this solution, I experience this problem. Would you know of a way around it? – Rastapopoulos Apr 19 '18 at 10:18
  • The BufEnter autocmd looks like it does not have this problem. – Rastapopoulos Apr 19 '18 at 10:49
3

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

:au BufEnter * if &buftype == 'terminal' | :startinsert | endif
  • 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 '18 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 '18 at 3:52
0

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"

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.