行被覆蓋而不是在同一行上繼續

行被覆蓋而不是在同一行上繼續

我的 bash 提示符號有問題。當我鍵入的命令超過螢幕寬度的長度時,它會在同一行上繼續並覆蓋我編寫的內容,然後在第 2 行上繼續。

這是我的 PS1:

PS1="\$(git_prompt) ${BIWhite}\W${Color_Off} → "

變數

BIWhite='\e[1;97m'
Color_Off='\e[0m'

然後是 git_prompt 的腳本:

git_prompt() {
local g="$(__gitdir)"
if [ -n "$g" ]; then
    local MINUTES_SINCE_LAST_COMMIT=`minutes_since_last_commit`
    if [ "$MINUTES_SINCE_LAST_COMMIT" -gt 30 ]; then
        local COLOR=${BRed}
    elif [ "$MINUTES_SINCE_LAST_COMMIT" -gt 10 ]; then
        local COLOR=${BIYellow}
    else
        local COLOR=${BGreen}
    fi
    local SINCE_LAST_COMMIT="${COLOR}$(minutes_since_last_commit)m${Color_Off}"
    # The __git_ps1 function inserts the current git branch where %s is
    local GIT_PROMPT=`__git_ps1 "(%s|${SINCE_LAST_COMMIT})"`
    echo ${GIT_PROMPT}
fi}

感謝所有幫助!謝謝!

答案1

不移動遊標的字元必須包含在 PS1 之間\[和之間\],否則 bash 會認為提示符比實際長度長。由於此變數的使用方式,您無法讓函數發出彩色文本,因為您無法正確轉義它。

http://mywiki.wooledge.org/BashFAQ/053以獲得更多解釋。

相關內容