With $TERM='screen-256color' under tmux, H

2020-05-20 09:20发布

问题:

I have tmux set up with $TERM being set to screen-256color correctly. This works fine, and colours are set correctly, however it prevents me from sending HOME and END keys to the terminal, which are instead printed as F\n and H\n.

I should add that home appears to work in irssi, but not vim. Home seems to send (According to Ctrl+v <HOME>), ^[OH

It may be worth adding that I am well aware of the ability to use ^ and $ to move to the start and end of the lines, however $ does not go to the end, rather the penultimate character, and I prefer to use HOME and END (as I can under other $TERM settings).

Can anyone explain why this is, and how I can fix it?

As a part-way fix, I set the vim keybindings to map a <Home> and <End> keypress to <Esc>OH and <Esc>OF. This isn't ideal, but works for the moment! See https://github.com/jvc26/dotfiles/blob/master/.vimrc for details.

Thanks!

回答1:

The above mapping solution doesn't affect the command mode or visual mode. The following is a more ideal solution until either tmux or vim fixes the bug (put in your .vimrc):

""""""""""""""
" tmux fixes "
""""""""""""""
" Handle tmux $TERM quirks in vim
if $TERM =~ '^screen-256color'
    map <Esc>OH <Home>
    map! <Esc>OH <Home>
    map <Esc>OF <End>
    map! <Esc>OF <End>
endif


回答2:

As a fix, I set the vim keybindings to map a <Home> and <End> keypress to <Esc>OH and <Esc>OF.

" Handle TERM quirks in vim
if $TERM =~ '^screen-256color'
    set t_Co=256
    nmap <Esc>OH <Home>
    imap <Esc>OH <Home>
    nmap <Esc>OF <End>
    imap <Esc>OF <End>
endif


回答3:

It is also possible to set the keybindings in the .inputrc as explained on archlinux Home_and_End_keys_not_working or .zshrc as on zshwiki/zle/bindkeys. This other stackoverflow question has some additional useful tips home-end-keys-do-not-work-in-tmux