LoginSignup
1
1

More than 3 years have passed since last update.

ターミナルとかのプロンプト表示を変更する

Last updated at Posted at 2021-02-24

ターミナルとかのプロンプトを短くしたい、そんな時のお話。肝はPS1の書き方が・・・ちと覚えられない・・・こと。

CentOS : Gitのブランチ名を表示する

  • 環境
    • CentOS Linux release 7.9.2009 (Core)
    • GNU bash, version 4.2.46(2)-release (x86_64-redhat-linux-gnu)
    • git-2.31.1

[ユーザー名@ホスト名 現在ディレクトリ]$になっているのを色付きで以下みたいに変えます。

変更後イメージ
ユーザー名@現在ディレクトリ (Gitのブランチ名)
$ 

うっかりミス防止にGitのブランチ名がプロンプトに表示したいのです。

git-prompt.shを探すときはlocateコマンドを使うと便利そうです。今回は持っていないのでやりませんでしたが。
参考 : locateコマンドについて詳しくまとめました 【Linuxコマンド集】

git-prompt.shを持っていない場合は、https://github.com/git/git/tree/master/contrib/completion からwgetできます。

# 1. 使っているシェルはBashです
$ echo $SHELL
/bin/bash

# 2. Gitをインストールした時に一緒に入っていたシェルを使います
$ ls -la /opt/git-2.31.1/contrib/completion/
total 128
drwxrwxr-x  2 root root  4096  3月 27 07:03 .
drwxrwxr-x 25 root root  4096  3月 27 07:03 ..
-rw-rw-r--  1 root root    14  3月 27 07:03 .gitattributes
-rw-rw-r--  1 root root 78497  3月 27 07:03 git-completion.bash # <<< これを使う
-rw-rw-r--  1 root root  4801  3月 27 07:03 git-completion.tcsh
-rw-rw-r--  1 root root  7117  3月 27 07:03 git-completion.zsh
-rw-rw-r--  1 root root 17778  3月 27 07:03 git-prompt.sh      # <<< これを使う

# 3. .bashrcにプロンプト文字をPS1で定義します
$ vi ~/.bashrc
# ------以下を追記------------------------------
source /opt/git-2.31.1/contrib/completion/git-prompt.sh
source /opt/git-2.31.1/contrib/completion/git-completion.bash
GIT_PS1_SHOWDIRTYSTATE=true
export PS1='\[\033[32m\]\u@\w\[\033[36m\]$(__git_ps1)\[\033[00m\]\n\$ '
# ------ここまで------------------------------

# 4. 反映する
$ source ~/.bashrc
PS1の文字 意味 参考サイト
\u 現在のユーザー名 6.9 Controlling the Prompt - Bash Reference Manual
\h ホスト名 6.9 Controlling the Prompt - Bash Reference Manual
\w 現在の作業ディレクトリ。$HOMEはチルダで短縮されます ($PROMPT_DIRTRIM変数を使用) 。 6.9 Controlling the Prompt - Bash Reference Manual
[ 印刷されない文字のシーケンスを開始します。これは端末制御シーケンスをプロンプトに埋め込むために使用できます。 6.9 Controlling the Prompt - Bash Reference Manual
] 印刷されない文字のシーケンスを終了します。 6.9 Controlling the Prompt - Bash Reference Manual
[\033[{色番号}m] 文字に色をつける。 BashのPS1についての忘備録 - HRR Co., Ltd.

Cloud9 : {ユーザ}を消す

# 1. 使っているシェルを確認する(違うシェルを使っている場合は次で変更するファイルが違うはず)
ec2-user:~/environment $ echo $SHELL
/bin/bash

# 2. PS1の内容を変更する
ec2-user:~/environment $ vi ~/.bashrc
---------変更内容-----------------------
変更前) PS1='\[\033[01;32m\]$(_cloud9_prompt_user)\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]$(__git_ps1 " (%s)" 2>/dev/null) $ '
変更後) PS1='\[\033[01;34m\]\w\[\033[00m\]$(__git_ps1 " (%s)" 2>/dev/null)\n$ '
------------------------------------------------------------------------

# 3. 反映する
ec2-user:~/environment $ source ~/.bashrc 

# 4. 完成!
~/environment
$ 

以下が緑の太字で内容をユーザ名を表示という意味になるのでそこを削除しました。
\[\033[01;32m\]$(_cloud9_prompt_user)\[\033[00m\]
以下が青の太字でディレクトリパス(ブランチ名)を表示するという意味になるので、その後ろに改行\n入れてみました。
\[\033[01;34m\]\w\[\033[00m\]$(__git_ps1 " (%s)" 2>/dev/null)

変更前
スクリーンショット 2021-02-24 18.30.28.png
変更後
スクリーンショット 2021-02-24 18.40.03.png

Git Bash : {ユーザー}@{ホスト} MINGW64'を消す

  1. /c/apps/Git/etc/profile.d/git-prompt.shをエディタで開く
  2. 14~17行目をコメントアウト(下記コード参照)して保存する
  3. Git Bashを再起動する
git-prompt.sh
#...省略...
    PS1='\[\033]0;$TITLEPREFIX:$PWD\007\]' # set window title
    PS1="$PS1"'\n'                 # new line
    # PS1="$PS1"'\[\033[32m\]'       # change to green << 行をコメントアウト
    # PS1="$PS1"'\u@\h '             # user@host<space> << 行をコメントアウト
    # PS1="$PS1"'\[\033[35m\]'       # change to purple << 行をコメントアウト
    # PS1="$PS1"'$MSYSTEM '          # show MSYSTEM << 行をコメントアウト
    PS1="$PS1"'\[\033[33m\]'       # change to brownish yellow
    PS1="$PS1"'\w'                 # current working directory
    if test -z "$WINELOADERNOEXEC"
#...省略...
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1