Mac OS X および Linux のターミナルでコマンド間に便利な区切り文字を追加する


非常に多くのコマンドを一度に実行するため、ターミナルでの作業が少し混乱する場合は、この小さなトリックを使用すると、各プロンプトの間に区切り文字が挿入され、最後に実行したいくつかのコマンドを簡単に確認できるようになります。

ブロガーの Emilis は、bash プロンプトが少し乱雑になりすぎていることに気づき、この調整を実装して、各コマンドの間に長い点線を挿入しました。また、実行した各コマンドを太字にして、簡単にスクロールバックして、実行した各コマンドとその出力の違いを確認できるようにしました。

Linux でこれを使用するには、次のコードをコピーするだけです (GitHub でも利用可能) という名前の新しいファイルに入れます。.bash_ps1ホームフォルダー内:

# Fill with minuses
# (this is recalculated every time the prompt is shown in function prompt_command):
fill="—- "
reset_style='\[\033[00m\]'
status_style=$reset_style'\[\033[0;90m\]' # gray color; use 0;37m for lighter color
prompt_style=$reset_style
command_style=$reset_style'\[\033[1;29m\]' # bold black
# Prompt variable:
PS1="$status_style"'$fill \t\n'"$prompt_style"'${debian_chroot:+($debian_chroot)}\u@\h:\w\$'"$command_style "
# Reset color for command output
# (this one is invoked every time before a command is executed):
trap 'echo -ne "\e[0m"' DEBUG
function prompt_command {
# create a $fill of all screen width minus the time string and a space:
let fillsize=${COLUMNS}-9
fill=""
while [ "$fillsize" -gt "0" ]
do
fill="-${fill}" # fill with underscores to work on
let fillsize=${fillsize}-1
done
# If this is an xterm set the title to user@host:dir
case "$TERM" in
xterm*|rxvt*)
bname=`basename "${PWD/$HOME/~}"`
echo -ne "\033]0;${bname}: ${USER}@${HOSTNAME}: ${PWD/$HOME/~}\007"
;;
*)
;;
esac
}
PROMPT_COMMAND=prompt_command

Mac を使用している場合は、代わりに次のコードをコピーします。

############################################
# Modified from emilis bash prompt script
# from https://github.com/emilis/emilis-config/blob/master/.bash_ps1
#
# Modified for Mac OS X by
# @corndogcomputer
###########################################
# Fill with minuses
# (this is recalculated every time the prompt is shown in function prompt_command):
fill="—- "
reset_style='\[\033[00m\]'
status_style=$reset_style'\[\033[0;90m\]' # gray color; use 0;37m for lighter color
prompt_style=$reset_style
command_style=$reset_style'\[\033[1;29m\]' # bold black
# Prompt variable:
PS1="$status_style"'$fill \t\n'"$prompt_style"'${debian_chroot:+($debian_chroot)}\u@\h:\w\$'"$command_style "
# Reset color for command output
# (this one is invoked every time before a command is executed):
trap 'echo -ne "\033[00m"' DEBUG
function prompt_command {
# create a $fill of all screen width minus the time string and a space:
let fillsize=${COLUMNS}-9
fill=""
while [ "$fillsize" -gt "0" ]
do
fill="-${fill}" # fill with underscores to work on
let fillsize=${fillsize}-1
done
# If this is an xterm set the title to user@host:dir
case "$TERM" in
xterm*|rxvt*)
bname=`basename "${PWD/$HOME/~}"`
echo -ne "\033]0;${bname}: ${USER}@${HOSTNAME}: ${PWD/$HOME/~}\007"
;;
*)
;;
esac
}
PROMPT_COMMAND=prompt_command

次に、ターミナルで次のコマンドを実行します。

nano ~/.bashrc

Linuxの場合、または

nano ~/.bash_profile

Mac OS X の場合。次のコードをそのファイルに貼り付け、Ctrl+X を押して保存し、Y を押して確認し、ターミナルを再起動します。

if [ -f "$HOME/.bash_ps1" ]; then
. "$HOME/.bash_ps1"
fi

新しい変更が有効になっていることが確認できるはずです。各コマンドの間に区切り文字が表示され、それらのコマンドは太字で表示されます (他のコマンドはすべて無地です)。続きを読むにはリンクをクリックしてください。

どうもありがとうございましたロブ・ジョンソンOS X バージョンの設定ファイルの微調整にご協力ください。

One Mans Bash プロンプトを少しカスタマイズして...|エミリス・ダンバウカス @ GitHub経由ハッカーニュース