Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When pressing an arrow key, vim terminal mode is cancelled

Tags:

vim

When using vim's terminal mode (access by :term) if I put in two double quotes and then press the left button (such as if you were to type in git commit -m "" and then go back one character to start the message), the following happens:

  1. The terminal is closed.
  2. A new line is added above the cursor in the buffer.
  3. Insert mode is entered.
  4. The character D is inserted.

The exact typed sequence is :term<cr>git commit -m ""<left>

Why is it that this series of events happens, and how can I prevent it in the future?

like image 749
user24984 Avatar asked Oct 25 '25 03:10

user24984


1 Answers

It turns out that in some terminals the arrow keys are represented with escape key sequences, such as the left arrow represented by Esc O D. In my vimrc I had the mapping tnoremap <esc> <c-w>:q!<cr> which was meant to close the terminal when I pressed the escape key. But due to how vim interpreted my arrow key sequences, it would close out the terminal when I pressed any of the arrows. I solved this by changing the mapping to tnoremap <esc><esc> <c-w>:q!<cr>.

More background can be found on this error here: https://github.com/vim/vim/issues/2216#issuecomment-337566816

like image 162
user24984 Avatar answered Oct 28 '25 02:10

user24984