
私は、git ディレクトリにいるかどうか、ディレクトリがクリーンかどうか、変更をコミットする必要があるかどうかなど、より関連性の高い情報をターミナルに表示するために、カスタム PS1 を使用しています。ただし、コマンドを矢印で選択しているときに、ターミナルの行の一部が消えてしまうことがあります。
@ ~/tests/testing [tests] > grunt
# up arrow, down arrow
@ ~/tests/testing [t
本質的には、 がests] >
切り取られ、 だけが残ります[t
。
この PS1 構成で回線の一部が切断される特別な理由があるのでしょうか?
追加情報は次のとおりです:
私の TERM 環境変数は ですxterm-256color
。私の は次のとおりです.bash_profile
:
red='\033[0;31m'
yellow='\033[0;32m'
orange='\033[0;33m'
blue='\033[0;34m'
pink='\033[0;35m'
NC='\033[0m'
function is_git {
if git rev-parse --is-inside-work-tree 2>/dev/null; then
return 1
else
return 0
fi
}
function stuff {
if [ $(is_git) ]; then
git_dir="$(git rev-parse --git-dir 2>/dev/null)"
if [ -z "$(ls -A ${git_dir}/refs/heads )" ]; then
echo -en " [${orange}init${NC}]"
return
fi
echo -n " ["
if [ $(git status --porcelain 2>/dev/null| wc -l | tr -d ' ') -ne 0 ]; then
echo -en "${red}"
else
echo -en "${blue}"
fi
echo -en "$(git rev-parse --abbrev-ref HEAD)${NC}]"
fi
}
export PS1="@ \w\[\$(stuff)\]\[\$(tput sgr0)\] > "
答え1
@i_am_root の\[
and をand の\]
定義内に入れるという提案は良いものです。しかし、red
これ、bash は 内の および のみを処理し\[
、によって に含まれるテキスト内の および は処理しません\]
。したがって、などの内部ではおよびの代わりにand (またはand )を使用します。PS1
PS1
$()
\001
\002
\x01
\x02
red
\[
\]
注: 1人あたりこの答え、エスケープ コードのみが および 内に存在する必要があります\001
。\002
ユーザーに表示されるテキストは および の外側に配置する必要があります。\001
そう\002
することで、bash はそれが画面上でスペースを占有していることを認識し、再描画時にそれを考慮できるようになります。
答え2
Bash のカラー コード、文字のエスケープ、割り当てなどはすぐに混乱してしまいます。
echo
変数に追加してコマンドを置き換えるこのコード サンプルを試してくださいPS1
。
red='\[\033[0;31m\]'
yellow='\[\033[0;32m\]'
orange='\[\033[0;33m\]'
blue='\[\033[0;34m\]'
pink='\[\033[0;35m\]'
NC='\[\033[0m\]'
export PS1="@ \w"
function is_git {
if git rev-parse --is-inside-work-tree 2>/dev/null; then
return 1
else
return 0
fi
}
function stuff {
if [ $(is_git) ]; then
git_dir="$(git rev-parse --git-dir 2>/dev/null)"
if [ -z "$(ls -A ${git_dir}/refs/heads )" ]; then
PS1="${PS1} [${orange}init${NC}]"
return
fi
PS1="$PS1 ["
if [ $(git status --porcelain 2>/dev/null| wc -l | tr -d ' ') -ne 0 ]; then
PS1="${PS1}${red}"
else
PS1="${PS1}${blue}"
fi
PS1="${PS1}$(git rev-parse --abbrev-ref HEAD)${NC}]"
fi
}
stuff
PS1="${PS1}$(tput sgr0) > "