概括

概括

概括

當我建立新的 tmux 會話時,我的提示符號會從預設的 bash 配置中提取,並且我必須手動執行自source ~/.bashrc訂提示符。

分析

我使用的是 RHEL 7 機器。不久前,我在 bash 更新後開始注意到這種行為,但直到現在才抽出時間來問這個問題(並且不確定這是從哪個更新開始發生的)。

例如,我將提示自訂為:

[user@hostname ~]$

每當我啟動一個新的 tmux 會話時,它都會使用 bash 預設值:

-sh-4.2$

快速運行source ~/.bashrc總是可以解決問題,但令人煩惱的是,每次我想修復一些小問題時都必須這樣做。關於如何讓 tmux 再次自動執行此操作有什麼想法嗎?

如果需要更多信息,我很樂意提供。

tmux.conf

作為參考,我tmux.conf在下面提供了我的文件,儘管它很難稱為自訂文件。

setw -g mode-keys vi

# reload tmux.conf
bind r source-file ~/.tmux.conf \; display-message " ✱ ~/.tmux.conf is reloaded"

答案1

這與 Bash 初始化檔有關。預設情況下,~/.bashrc用於互動式、非登入殼。它不會來自登入 shell。 Tmux 使用登入外殼預設情況下。因此,shell 由 tmux skip 啟動~/.bashrc

default-commandshell命令

預設為空字串,它指示 tmux 建立登入 shell使用選項的值default-shell

Bash 的初始化文件,

  1. 登入方式:
    1. /etc/profile
    2. ~/.bash_profile, ~/.bash_login, ~/.profile(只存在第一個)
  2. 互動的 未登入:
    1. /etc/bash.bashrc(某些 Linux;不在 Mac OS X 上)
    2. ~/.bashrc
  3. 非互動式:
    1. 原始檔在$BASH_ENV

解決方案

奇怪的互動式、非登入載入要求在其他情況下也會讓人感到困惑。這最佳解決方案~/.bashrc是改變as的載入要求僅限互動,這正是某些發行版(例如 Ubuntu)正在做的事情。

# write content below into ~/.profile, or ~/.bash_profile

# if running bash
if [ -n "$BASH_VERSION" ]; then
    # include .bashrc if it exists
    if [ -f "$HOME/.bashrc" ]; then
    . "$HOME/.bashrc"
    fi
fi

這應該是您想要的解決方案。我建議每個 Bash 用戶在設定檔中進行此設定。


更新:以上設定是從Ubuntu複製的。看來他們選擇.bashrc在登入 shell 中加載,無論它是否在互動式 shell 中。

如果您想偵測互動式 shell,請使用$PS1.

if [ -n "$BASH_VERSION" -a -n "$PS1" ]; then
    # include .bashrc if it exists
    if [ -f "$HOME/.bashrc" ]; then
    . "$HOME/.bashrc"
    fi
fi

參考

答案2

據我所知,預設情況下tmux運行登入 shell。當bash作為互動式登入 shell 呼叫時,它會查找~/.bash_profile~/.bash_login~/.profile。所以你必須放入source ~/.bashrc這些文件之一。

解決此問題的另一種方法是將以.tmux.conf下行放入您的文件中:

set-option -g default-shell "/bin/bash"

答案3

將以下內容新增至.tmux.conf

set-option -g default-shell "/bin/bash"

才不是產生期望的結果。

只有當添加source "$HOME/.bashrc"~/.bash_profile預期的結果時才能達到。

當開啟新視窗或窗格時,以及在分離和開啟新的 tmux 會話時,這將在活動的 tmux 會話上運作。

測試於:

VERSION="16.04.2 LTS (Xenial Xerus)"
tmux 2.1

答案4

$HOME/.tmux.conf使用以下內容修改您的文件:

set-option -g default-shell "/usr/bin/bash"
set-option -g default-command bash

那應該覆蓋它。當您這樣做時,為什麼不將點檔案放在$HOME/.config/tmux它所屬的不帶點的資料夾中。並且,將顏色輸出和重新載入鍵綁定新增至$HOME/.config/tmux/tmux.conf

set -g default-terminal "screen256color"

bind r source-file "${HOME}/.config/tmux/tmux.conf"

如果tmux -V< 3.1,請新增別名以$HOME/.bashrc從此位置載入設定檔:

alias tmux="tmux -f ${HOME}/.config/tmux/tmux.conf"

/etc/tmux.conf或將其設定為預設位置的系統範圍配置。

相關內容