bash Home/End 键在 tmux 中不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18600188/
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 06:27:04  来源:igfitidea点击:

Home/End keys do not work in tmux

linuxbashtmux

提问by Ben Davis

I'm currently using tmux with xterm-256color $TERM variable. When in bash under tmux, pressing home/end would insert tilde characters (~). Outside of tmux the home/end keys work fine.

我目前正在使用带有 xterm-256color $TERM 变量的 tmux。在 tmux 下的 bash 中,按 home/end 将插入波浪号字符 (~)。在 tmux 之外,home/end 键工作正常。

Using cat and tput, I could see that there was a mismatch between the generated and expected sequences:

使用 cat 和 tput,我可以看到生成的序列和预期的序列之间存在不匹配:

$ cat -v # pressing home, then end
^[[1~^[[4~
$ tput khome | cat -v; echo
^[OH
$ tput kend | cat -v; echo
^[OF

To fix this, I decided to add the following to my .bashrc:

为了解决这个问题,我决定将以下内容添加到我的 .bashrc 中:

if [[ -n "$TMUX" ]]; then
    bind '"\e[1~":"\eOH"'
    bind '"\e[4~":"\eOF"'
fi

That fixed the problem for bash, however in other readline programs, such as within a REPL such as ipython, it still inserts a tilde for home/end.

这解决了 bash 的问题,但是在其他 readline 程序中,例如在诸如 ipython 之类的 REPL 中,它仍然为 home/end 插入波浪号。

Why exactly is this a problem in the first place? Why is the generated sequence different when I'm inside tmux vs outside it? How can fix this so that it's not an issue in any programs?

为什么这首先是一个问题?为什么当我在 tmux 内部和外部时生成的序列不同?如何解决这个问题,使其在任何程序中都不是问题?

采纳答案by Ben Davis

It appears the main problem is with using xterm-256color for $TERM. I switched $TERM to screen-256color and the problem went away.

看来主要问题是将 xterm-256color 用于 $TERM。我将 $TERM 切换到 screen-256color 并且问题消失了。

回答by sumanta

In tmux 2.0, you can just add these 2 lines in your .tmux.conf:

在 tmux 2.0 中,您只需在您的.tmux.conf:

bind -n End send-key C-e
bind -n Home send-key C-a

回答by Swivel

Add the following to your .tmux.conf:

将以下内容添加到您的.tmux.conf:

bind-key -n Home send Escape "OH"
bind-key -n End send Escape "OF"

And you're done!

大功告成!



Explanation

解释

After attempting each one of these, and several others I saw while perusing other answers and documentation, this finally worked for me in every scenario I threw at it. I can't promise the same for you, because everyone's scenarios are different, but this is what I ended up with.

在尝试了其中的每一个以及我在阅读其他答案和文档时看到的其他几个之后,这最终在我投入的每个场景中都对我有用。我不能给你同样的承诺,因为每个人的情况都不一样,但这就是我的结局。

This was discovered after introducing the same trial/error and logic from this somewhat related article. The only difference is where the translation is occurring; in my case, this happens within my .tmux.conf, rather than .bashrcor .zshrc(Mainly because my home/end worked fine outside of tmux)

这是在从这篇有点相关的文章中引入相同的试验/错误和逻辑后发现的。唯一的区别是翻译发生的位置;就我而言,这发生在我的.tmux.conf, 而不是.bashrcor .zshrc(主要是因为我的家/终端在 之外运行良好tmux

Debugging

调试

You can debug this issue by using cat -vlike referenced in the article above.

您可以使用cat -v上面文章中引用的类似方法来调试此问题。

Run cat -v, then press the Homeand Endkeys. Exit using Ctrl+C.

运行cat -v,然后按HomeEnd键。使用Ctrl+退出C

$ cat -v

Here's what my output looked like within tmux using zsh, zsh, and bash:

下面是我的输出看起来像内tmux using zshzshbash

tmux

多路复用器

?  ~ cat -v
^[[1~^[[4~^C

zsh

zsh

?  ~ cat -v
^[[H^[[F

bash

猛击

bash-3.2$ cat -v
^[[H^[[F

Solutioning

解决方案

Compare the above examples to what we're expecting to see, by pairing tputwith cat -v:

通过配对tput,将上述示例与我们期望看到的进行比较cat -v

$ tput khome | cat -v; echo
^[OH
$ tput kend | cat -v; echo
^[OF

Conclusion

结论

Because this problem exists solely with tmux, and not within the emulators themselves, I opted to make the bind changes within the tmux configuration instead. By using bind-keypaired with send, we can use the Escapekeyword paired with the sequence we want to achieve our translation. Thus:

因为这个问题只存在于tmux,而不是模拟器本身,所以我选择在 tmux 配置中进行绑定更改。通过使用bind-keypaired with send,我们可以使用Escape与我们想要实现翻译的序列配对的关键字。因此:

bind-key -n NAME_OF_KEY send Escape SEQUENCE_GOES_HERE

This debugging and solutioning process can be applied to any other key translation issues. But, don't go too crazy. Some keys are mapped to certain escape sequences for a reason. Notice how bashand zshreceived the ^[[Hsequence for Homeinstead of ^[OH; it's probably not recommended we override this in our .zshrcunless we're having major issues with this in zsh.

此调试和解决过程可应用于任何其他关键转换问题。但是,不要太疯狂。出于某种原因,某些键被映射到某些转义序列。请注意如何bashzsh接收^[[H序列 forHome而不是^[OH; 它可能不建议我们在重写这个.zshrc除非我们在有这个重大问题zsh

回答by Oleg Kr

If you want to stay with xterm-256color in tmux for some reason - use arch solutionwith inputrc. I tested it in tmux with rxvt, ruby irb, python, lua and home/end keys are ok. Probably every readline app will be ok.

如果您出于某种原因想在 tmux 中使用 xterm-256color - 使用带有 inputrc 的arch 解决方案。我在 tmux 中使用 rxvt、ruby irb、python、lua 和 home/end 键对其进行了测试。可能每个 readline 应用程序都可以。

From the arch wiki:

从拱形维基:

First things first:

第一件事:

do not set $TERM manually - let the terminal do it.

不要手动设置 $TERM - 让终端来做。



Many command line applications use the Readline library to read input. So properly configuring Readline can fix Home and End in many cases.

许多命令行应用程序使用 Readline 库来读取输入。因此,正确配置 Readline 可以在许多情况下修复 Home 和 End。

the default /etc/inputrc file does not include a mapping for home/end keys.

默认的 /etc/inputrc 文件不包括 home/end 键的映射。

To check what the emitted escape sequence for these keys is:

要检查这些键发出的转义序列是什么:

1. Ctrl + V
2. Home
3. Spacebar
4. Ctrl + V
5. End

this will probably print: $ ^[[1~ ^[[4~. So you have to add a mapping for these sequences to your inputrc (/etc/inputrc to be globally, or only for your user ~/.inputrc):

这可能会打印:$ ^[[1~ ^[[4~。因此,您必须将这些序列的映射添加到您的 inputrc(/etc/inputrc 是全局的,或仅适用于您的用户 ~/.inputrc):

"\e[1~": beginning-of-line
"\e[4~": end-of-line

回答by someone

In my case it was a problem with zsh in tmux (bash in tmux was ok). None of the other anwsers here worked for me.

在我的情况下,tmux 中的 zsh 有问题(tmux 中的 bash 没问题)。这里的其他答案都不适合我。

But adding this to .zshrcfixed it:

但是添加这个来.zshrc修复它:

bindkey "\E[1~" beginning-of-line
bindkey "\E[4~" end-of-line

Besides that I also have:

除此之外,我还有:

bindkey "\E[H" beginning-of-line
bindkey "\E[F" end-of-line
bindkey "\E[3~" delete-char

回答by ducklin5

So I don't have enough points to comment,so I'll say it here instead. I believe the preferred solution is using set -g default-terminal "screen-256color"in your ~/.tmux.conf. I actually had this problem a while ago and decided to go with sumanta's solution :

所以我没有足够的积分来评论,所以我会在这里说。我相信首选的解决方案是 set -g default-terminal "screen-256color"在您的~/.tmux.conf. 我实际上不久前遇到了这个问题,并决定采用 sumanta 的解决方案:

bind -n End send-key C-e
bind -n Home send-key C-a

However I had forgotten I left this in here and ended up having a similar issue with vim (home and end were copy pasting from registers) instead of zsh. In short bind DOES affect vim.

但是我忘记了我把它留在了这里,最终在 vim 上遇到了类似的问题(home 和 end 是从寄存器复制粘贴)而不是 zsh。总之绑定会影响vim。

回答by Max Robbertze

I wasted a lot of time trying all off the above. In the end I reverted to barbarism:

我浪费了很多时间尝试以上所有方法。最后我又回到了野蛮状态:

sudo apt purge tmux
sudo apt install tmux

fixed it for me.

为我修好了。

回答by André Werlang

From tmux FAQ:

来自tmux 常见问题解答

PLEASE NOTE: most display problems are due to incorrect TERM! Before reporting problems make SURE that TERM settings are correct inside and outside tmux.

Inside tmux TERM must be "screen", "tmux" or similar (such as "tmux-256color"). Don't bother reporting problems where it isn't!

Outside, it should match your terminal: particularly, use "rxvt" for rxvt and derivatives.

请注意:大多数显示问题是由于不正确的 TERM!在报告问题之前,请确保 TERM 设置在 tmux 内外都是正确的。

tmux 内的 TERM 必须是“screen”、“tmux”或类似的(例如“tmux-256color”)。不要费心报告不存在的问题!

在外面,它应该与您的终端匹配:特别是,对 rxvt 和衍生产品使用“rxvt”。

Add the following command to your ~/.tmux.conf:

将以下命令添加到您的~/.tmux.conf

set -g default-terminal tmux-256color

PS: any solution involving binding the keys explicitly is a hack, and as such bound to fail.

PS:任何涉及明确绑定密钥的解决方案都是一种黑客行为,因此必然会失败。