LoginSignup
66
43

More than 5 years have passed since last update.

fishのプロンプトの右側をカスタマイズして、gitのブランチとstatusを表示させる

Posted at

やりたいこと

zshをやめて、fishを使い始めました。

zshを使っていたときに、下のような形でカレントディレクトリの右側にgitのブランチとstatusを出す設定をしていました。

[stagingに追加されていないファイルがある状態]
スクリーンショット 2017-06-07 13.16.31.png

[git addされている状態]
スクリーンショット 2017-06-07 13.13.29.png

[commitされている状態]
スクリーンショット 2017-06-07 13.16.22.png

便利に使っていたので、fishでも同じような感じにしたいと思いました。

また、fishのプロンプトはデフォルトでかわいいのがあるので、それを使いつつgitのブランチとstatusを右側に出すようにしたいと思いました。

ブラウザからプロンプトを設定する

$ fish_config

ブラウザが起動してぽちぽち設定できるので、好きなものを選びます。
わたしはこれにしました(水色が好き)。
スクリーンショット 2017-06-07 13.19.33.png

gitのブランチとstatusを表示する

fishの基本設定ファイルは~/.config/fish/config.fishです。
上記ファイルに書き込んでも設定は反映されますが、プロンプトの設定は関数に分けて~/.config/fish/functions/fish_prompt.fishに書くことが推奨されています。

fish_configコマンドを打ってブラウザからプロンプトを設定すると、以下のようなファイルが生成されて内容が書き込まれています。

~/.config/fish/functions/fish_prompt.fish
function fish_prompt
    test $SSH_TTY
    and printf (set_color red)$USER(set_color brwhite)'@'(set_color yellow)(prompt_hostname)' '
    test $USER = 'root'
    and echo (set_color red)"#"

    # Main
    echo -n (set_color cyan)(prompt_pwd) (set_color red)'❯'(set_color yellow)'❯'(set_color green)'❯ '
end

この二つのファイルを使って、gitのブランチとstatusの設定を追記してみます。

ちょっと古い記事ですが記号を使ったかわいい設定を公開してくださっている方がいたので、使うことにします。
Git Information in Fish Shell’s Prompt

~/.config/fish/config.fish
# Fish git prompt
set __fish_git_prompt_showdirtystate 'yes'
set __fish_git_prompt_showstashstate 'yes'
set __fish_git_prompt_showuntrackedfiles 'yes'
set __fish_git_prompt_showupstream 'yes'
set __fish_git_prompt_color_branch yellow
set __fish_git_prompt_color_upstream_ahead green
set __fish_git_prompt_color_upstream_behind red

# Status Chars
set __fish_git_prompt_char_dirtystate '⚡'
set __fish_git_prompt_char_stagedstate '→'
set __fish_git_prompt_char_untrackedfiles '☡'
set __fish_git_prompt_char_stashstate '↩'
set __fish_git_prompt_char_upstream_ahead '+'
set __fish_git_prompt_char_upstream_behind '-'
~/.config/fish/functions/fish_prompt.fish
function fish_prompt
    test $SSH_TTY
    and printf (set_color red)$USER(set_color brwhite)'@'(set_color yellow)(prompt_hostname)' '
    test $USER = 'root'
    and echo (set_color red)"#"

    # Main
    echo -n (set_color cyan)(prompt_pwd) (set_color red)'❯'(set_color yellow)'❯'(set_color green)'❯ '

    # Git
    set last_status $status
    printf '%s ' (__fish_git_prompt)
    set_color normal
end

これを保存して適用させると、以下のような表示になります。

[stagingに追加されていないファイルがある状態]
スクリーンショット 2017-07-26 11.11.47.png

[git addされている状態]
スクリーンショット 2017-07-26 11.11.56.png

[commitされているがpushされていない状態]
スクリーンショット 2017-07-26 11.13.23.png

[クリーンな状態]
スクリーンショット 2017-07-26 11.12.13.png

他にもステータスに応じてわかりやすくてかわいい記号が出るので、便利に使っています。

66
43
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
66
43