Tmux 提示不遵循正常的 bash 提示 PS1 \W

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

Tmux prompt not following normal bash prompt PS1 \W

bashtmux

提问by Paige

I did some updating on my Mac and seem to have broken some of my settings. I have the following set in my .bash_profile

我在我的 Mac 上做了一些更新,似乎破坏了我的一些设置。我的 .bash_profile 中有以下设置

export PS1="\W $"

导出 PS1="\W $"

This is working in my normal bash session to show just the current directory instead of the whole path. However, when I switch into tmux, it again displays the whole path. Other changes to the PS1 in the bash profile such as color or other characters work fine and are reflected in tmux. I have emptied out my .tmux.conf to see if that was causing conflict but there was no change to this behavior.

这在我的普通 bash 会话中工作以仅显示当前目录而不是整个路径。但是,当我切换到 tmux 时,它再次显示整个路径。bash 配置文件中对 PS1 的其他更改(例如颜色或其他字符)工作正常,并反映在 tmux 中。我已经清空了我的 .tmux.conf 以查看是否会导致冲突,但这种行为没有改变。

I did create a new user on the system and tried the same PS1 and it worked perfectly in both a normal session and tmux.

我确实在系统上创建了一个新用户并尝试了相同的 PS1,它在正常会话和 tmux 中都运行良好。

I am mostly confused because I know I had it working and can't figure out what would have changed in the update. What files besides .bash_profile and .tmux.conf could be at play here? Is there a way to tell where tmux is pulling it's settings from?

我很困惑,因为我知道我可以正常工作并且无法弄清楚更新中会发生什么变化。除了 .bash_profile 和 .tmux.conf 之外,还有哪些文件可以在这里发挥作用?有没有办法告诉 tmux 从哪里提取它的设置?

Additional info: This behavior is the same in both iTerm2 and Terminal Tmux version 1.8 Mac OSX 10.9.1

附加信息:此行为在 iTerm2 和 Terminal Tmux 版本 1.8 Mac OSX 10.9.1 中相同

采纳答案by Coder-256

Add the following to your .tmux.conf:

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

new -n WindowName bash --login

You can replace WindowNamewith whatever you want the first window to be named. When bashis invoked this way, it sources to your .bash_profile, .bash_rc, .profile, etc. which is where you change your $PS1.

您可以将WindowName第一个窗口的名称替换为任何您想要的名称。当bash调用此方法,它可以提供你的.bash_profile.bash_rc.profile等这是你改变你的$PS1

回答by tmux-lover

This one works for me: In tmux/terminal:

这个对我有用:在 tmux/终端中:

tmux set-option -g default-command bash

Or simply put into ~/.tmux.conf:

或者简单地放入~/.tmux.conf

set-option -g default-command bash

回答by Samuel Lampa

I have the similar problem. I get the correct result if I always start tmux with the bash command applied, like so:

我有类似的问题。如果我总是在应用 bash 命令的情况下启动 tmux,我会得到正确的结果,如下所示:

tmux new bash

So, to simplify this, I just created an alias in my ~/.bash_aliases file, which I use all the time:

所以,为了简化这一点,我只是在我的 ~/.bash_aliases 文件中创建了一个别名,我一直在使用它:

alias tn='tmux new bash'

This does only work for the first window though. When creating new windows, you have to start bash again, by executing bashin the terminal.

不过,这只适用于第一个窗口。创建新窗口时,您必须通过bash在终端中执行来再次启动 bash 。

回答by nightm4re

Add the following in your ~/.tmux.conf

在您的 ~/.tmux.conf 中添加以下内容

set -g default-terminal "tmux-256color" 

From the beloved ArchWiki tmux

来自心爱的ArchWiki tmux

回答by srk

I had the same problem and after some research I have added the following command to my ~/.tmux.conf:

我遇到了同样的问题,经过一些研究,我将以下命令添加到我的~/.tmux.conf

set-option -g default-command "reattach-to-user-namespace -l /opt/local/bin/bash --login"

I am using OSX 10.9.5 with iTerm2 Build 2.0.0.20141103, bash 4.3.30(1)-release, tmux 1.9a. Bash and tmux are from macports.

我正在使用 OSX 10.9.5 和 iTerm2 Build 2.0.0.20141103,bash 4.3.30(1)-release,tmux 1.9a。Bash 和 tmux 来自 macports。

回答by ds-bos-msk

Add the following line to ~/.tmux.conf

将以下行添加到 ~/.tmux.conf

set -g default-terminal "screen-256color"

set -g default-terminal "screen-256color"

回答by RobM

Better to detect which terminals are known to the system's terminfodatabase via the infocmpcommand. I've got the following in my ~/.tmux.conf:

最好terminfo通过infocmp命令检测系统数据库已知哪些终端。我有以下内容~/.tmux.conf

# Last match wins
if-shell "infocmp xterm-256color" "set-option -g default-terminal xterm-256color"
if-shell "infocmp screen-256color" "set-option -g default-terminal screen-256color"
if-shell "infocmp tmux" "set-option -g default-terminal tmux"

If the infocmpshell-command succeeds, then the following set-optiontmux command is executed.

如果infocmpshell-command 成功,则执行以下set-optiontmux 命令。

回答by franbenz

When using tmux with byobu, you can try the byobu-promptcommand. It'will ask you whether you want a bash color prompt. After answering Yes, the prompt will change to the usual user@host.

当使用 tmux 和 byobu 时,您可以尝试使用该byobu-prompt命令。它会询问您是否需要 bash 颜色提示。回答 Yes 后,提示将更改为通常的 user@host。