Arrow keys in terminal insert mode

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Arrow keys in terminal insert mode

Jan Larres
Hi all,

I recently experimented with getting the arrow keys to work in insert
mode in a terminal, and since I don't seem to be the only one who had
problems with that I thought I should document my findings.

There are two pages in the Wiki describing the problem [1][2]. The
proposed solutions either advise to change the 'term' setting (which can
have various side effects) or using the output of a ^V + arrow key to
either set 't_ku' and the corresponding other options or to create an
imap with it, so it would look either like this

  set t_ku=^[OA

or like this:

  imap ^[OA <Up>

both with ^[ being a real escape sequence, of course. But those key
setting never really worked for me, and setting 'term' has side effects
like I said.

So when I read the Xterm control sequences manual a few days ago for a
completely different reason I stumbled upon an interesting section [3].
It said that the escape sequences for the cursor keys only consist of
ESC + A … D, without the 'O' in between that is generated when using ^V.
So I first tried setting the 't_ku' options like this:

  set t_ku=^[A

but that didn't work. Then I tried this:

  imap ^[A <Up>

and suddenly it worked! In every terminal I tried! Even remotely! I have
no idea why ^V apparently produces an escape sequence that doesn't work
(at least not in all cases), but the Xterm description seems to be the
really correct one. I tried it in xterm, rxvt-unicode, gnome-terminal
and the Linux console under Linux, and remotely under NetBSD. So the
complete configuration now looks like this:

  imap ^[A <Up>
  imap ^[B <Down>
  imap ^[C <Right>
  imap ^[D <Left>

again with ^[ as a real escape sequence entered using ^V<Esc>. Important
to note is that the 't_ku' options should *not* be set, if I set those
the mappings stopped working.

So it would probably be helpful if some more people could try if it
works for them and then I could add it to the Wiki. I think it would
also be a good idea to merge the two existing articles into one.

Cheers,
        Jan

 [1] http://vim.wikia.com/wiki/Fix_broken_arrow_key_navigation_in_insert_mode
 [2] http://vim.wikia.com/wiki/Fix_arrow_keys_that_display_A_B_C_D_on_remote_shell
 [3] http://invisible-island.net/xterm/ctlseqs/ctlseqs.html#PC-Style%20Function%20Keys

--
-[ OpenPGP key ID: 00A0FD5F ]-
It is better to risk sparing a guilty person than to condemn an innocent
one.
                -- Voltaire

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
Reply | Threaded
Open this post in threaded view
|

Re: Arrow keys in terminal insert mode

Badmagic
On 9/02/2011 1:21 AM, Jan Larres wrote:

> Hi all,
>
> I recently experimented with getting the arrow keys to work in insert
> mode in a terminal, and since I don't seem to be the only one who had
> problems with that I thought I should document my findings.
>
> There are two pages in the Wiki describing the problem [1][2]. The
> proposed solutions either advise to change the 'term' setting (which can
> have various side effects) or using the output of a ^V + arrow key to
> either set 't_ku' and the corresponding other options or to create an
> imap with it, so it would look either like this
>
>    set t_ku=^[OA
>
> or like this:
>
>    imap ^[OA<Up>
>
> both with ^[ being a real escape sequence, of course. But those key
> setting never really worked for me, and setting 'term' has side effects
> like I said.
>
> So when I read the Xterm control sequences manual a few days ago for a
> completely different reason I stumbled upon an interesting section [3].
> It said that the escape sequences for the cursor keys only consist of
> ESC + A … D, without the 'O' in between that is generated when using ^V.
> So I first tried setting the 't_ku' options like this:
>
>    set t_ku=^[A
>
> but that didn't work. Then I tried this:
>
>    imap ^[A<Up>
>
> and suddenly it worked! In every terminal I tried! Even remotely! I have
> no idea why ^V apparently produces an escape sequence that doesn't work
> (at least not in all cases), but the Xterm description seems to be the
> really correct one. I tried it in xterm, rxvt-unicode, gnome-terminal
> and the Linux console under Linux, and remotely under NetBSD. So the
> complete configuration now looks like this:
>
>    imap ^[A<Up>
>    imap ^[B<Down>
>    imap ^[C<Right>
>    imap ^[D<Left>
>
> again with ^[ as a real escape sequence entered using ^V<Esc>. Important
> to note is that the 't_ku' options should *not* be set, if I set those
> the mappings stopped working.
>
> So it would probably be helpful if some more people could try if it
> works for them and then I could add it to the Wiki. I think it would
> also be a good idea to merge the two existing articles into one.
>
> Cheers,
>          Jan
>
>   [1] http://vim.wikia.com/wiki/Fix_broken_arrow_key_navigation_in_insert_mode
>   [2] http://vim.wikia.com/wiki/Fix_arrow_keys_that_display_A_B_C_D_on_remote_shell
>   [3] http://invisible-island.net/xterm/ctlseqs/ctlseqs.html#PC-Style%20Function%20Keys
>
Hi Jan,

not sure what OS you're using but if you're using BSD/Linux, you can
correct this problem by changing your TERM settings.

For example, if you're using BASH, try putting one of the following in
your ~/.bashrc file:

export TERM='linux'
export TERM='xterm-256color'
export TERM='xterm'

and type    source ~/.bashrc

Then try your arrow keys in Vim. Worth a try!

Regards,
Steve Laurie


--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
Reply | Threaded
Open this post in threaded view
|

Re: Arrow keys in terminal insert mode

Jan Larres
Hi Steve,

BadMagic <[hidden email]>:
> not sure what OS you're using but if you're using BSD/Linux, you can
> correct this problem by changing your TERM settings.
>
> For example, if you're using BASH, try putting one of the following
> in your ~/.bashrc file:
>
> export TERM='linux'
> export TERM='xterm-256color'
> export TERM='xterm'

the problem with changing TERM is that is has various side effects. At
best it just means that you can't use any advanced features that your
terminal might have, but it can also mess up other control sequences,
colours, and who knows what else. It would also lead to problems if a
remote system doesn't have a termcap/terminfo entry for the TERM setting
that is being used. Using the mappings that I suggested doesn't have any
of these problems and should therefore be the best way to do it. The
only thing I'm not sure about is whether this really works everywhere
(or at least almost everywhere), so some testing by other people would
be useful. But thanks anyway!

Cheers,
        Jan

--
-[ OpenPGP key ID: 00A0FD5F ]-
A radioactive cat has eighteen half-lives.

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php