bash 在 tmux 下使用 $TERM='screen-256color' 时,HOME 和 END 键不起作用。为什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8604150/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 01:12:39  来源:igfitidea点击:

With $TERM='screen-256color' under tmux, HOME and END keys don't work. Why?

bashterminaltmux

提问by jvc26

I have tmux set up with $TERMbeing set to screen-256colorcorrectly. This works fine, and colours are set correctly, however it prevents me from sending HOMEand ENDkeys to the terminal, which are instead printed as F\nand H\n.

我有TMUX设置了$TERM被设置为screen-256color正确。这工作正常,并且颜色设置正确,但是它阻止我向终端发送HOMEEND键,而是打印为F\nH\n

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

我应该补充一点,home 似乎在 irssi 中有效,但在 vim 中无效。家好像送(据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 HOMEand END(as I can under other $TERM settings).

可能值得补充的是,我很清楚使用^$移动到行首和行尾的能力,但是$没有走到最后,而是倒数第二个字符,我更喜欢使用HOMEEND(尽可能在其他 $TERM 设置下)。

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>OHand <Esc>OF. This isn't ideal, but works for the moment! See https://github.com/jvc26/dotfiles/blob/master/.vimrcfor details.

作为部分修复,我将 vim 键绑定设置为将 a<Home>和keypress 映射<End><Esc>OH<Esc>OF。这并不理想,但目前有效!有关详细信息,请参阅https://github.com/jvc26/dotfiles/blob/master/.vimrc

Thanks!

谢谢!

采纳答案by Alaska

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 或 vim 修复错误(放入您的.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

回答by here

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

另外,也可以设定键绑定在.inputrc作为上的archlinux解释Home_and_End_keys_not_working.zshrc作为 zshwiki / ZLE / bindkeys。这个其他的 stackoverflow 问题有一些额外的有用提示home-end-keys-do-not-work-in-tmux

回答by jvc26

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

作为修复,我将 vim 键绑定设置为将 a<Home>和keypress 映射<End><Esc>OH<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