5

I'm having some problems remapping my home key; it works when I run Vim from tmux but not when I run it from a regular xterm without tmux. It looks like they're sending different escape codes and Vim only picks up on tmux's one for some reason.

I tried mucking about with a lot of TERM values, to no avail. So I figured I'd just hard-code this escape code in my vimrc and move on:

set nocompatible

" `^[` is the escape character.
set <Home>=^[OH
set t_kh=^[OH

echo &t_kh

noremap <Home> :echoerr "home!"<CR>

Starting with $ vim -u vimrc Vim outputs ^[OH, which is expected. But after I've started Vim the setting seems to be not set:

:set t_kh?
E846: Key code not set: t_kh?
:verbose set t_kh?
E846: Key code not set: t_kh?

And the remapped home key doesn't work. Manually running :set <Home> or :set t_kh fixes the problem; I can now use my custom <Home> mapping.

Why aren't these options taking effect? How can I make them take effect?

I have no plugins and am using Vim 8.0.586 on Arch Linux.

  • Is something setting 'term' after your vimrc is loaded? – jamessan May 15 '17 at 14:46
  • Don't think so @jamessan, :verbose set term? just gives term=xterm-256color without extra information. Also tried without plugins/vimrc, and I get exactly the same results. – Martin Tournoij May 15 '17 at 14:49
  • You say you have no plugins but do you have anything else? See :scriptnames – laktak May 17 '17 at 8:07
  • It loads the default plugins from ` /usr/share/vim/vim80/plugin/` @laktak, but other than that nothing. – Martin Tournoij May 17 '17 at 11:03
  • I guess I'm out of ideas - I have the same version on arch but can't replicate your issue. Try a different terminal? I'm using urxvt. – laktak May 17 '17 at 19:40
3

I found this in :help xterm-function-keys:

An xterm can send function keys F1 to F4 in two modes: vt100 compatible or
not.  Because Vim may not know what the xterm is sending, both types of keys
are recognized.  The same happens for the <Home> and <End> keys.
                        normal                  vt100
        <F1>    t_k1    <Esc>[11~       <xF1>   <Esc>OP     <xF1>-xterm
        <F2>    t_k2    <Esc>[12~       <xF2>   <Esc>OQ     <xF2>-xterm
        <F3>    t_k3    <Esc>[13~       <xF3>   <Esc>OR     <xF3>-xterm
        <F4>    t_k4    <Esc>[14~       <xF4>   <Esc>OS     <xF4>-xterm
        <Home>  t_kh    <Esc>[7~        <xHome> <Esc>OH     <xHome>-xterm
        <End>   t_@7    <Esc>[4~        <xEnd>  <Esc>OF     <xEnd>-xterm

When Vim starts, <xF1> is mapped to <F1>, <xF2> to <F2> etc.  This means that
by default both codes do the same thing.  If you make a mapping for <xF2>,
because your terminal does have two keys, the default mapping is overwritten,
thus you can use the <F2> and <xF2> keys for something different.

Setting both <Home> and <xHome> seems to work well! Because we only want this to take effect inside xterm I added a &term check (tmux is screen-256color):

set nocompatible

if &term == "xterm-256color"
    " `^[` is the escape character.
    set <Home>=^[OH
    set <xHome>=^[OH
endif

noremap <Home> :echoerr "home!"<CR>

I'd be lying if I'd say that I fully comprehend all of the above, but it seems to work well without ill effects in my testing.

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.