yiskw note

機械学習やプログラミングについて気まぐれで書きます

Starshipを使ってプロンプトをカスタマイズする


概要

Rust製のソフトウェアであるStarshipを用いて,プロンプトをカスタマイズしてみたので,
その使い方についてこちらにメモを残しておきます.

ターミナルでの作業の効率化については,他にも記事を書いておりますので,良ければご参照ください.

yiskw713.hatenablog.com

yiskw713.hatenablog.com

Starshipとは

starship.rs

公式によると,

The minimal, blazing-fast, and infinitely customizable prompt for any shell!

様々なOSで互換性があり,Rust製ということで高速かつ信頼性の高いプロンプトとなっているみたいです.
カスタマイズも容易に行うことができ,以下のように自分好みのプロンプトが簡単に作成できます.

f:id:yiskw713:20210620142408p:plain:w600

事前準備

今回はMacを想定して解説していきます.
その他のOSでのインストールは公式ドキュメントを参照してください.

事前準備として,Nerd Fontをインストールする必要があります.
今回はfont-hack-nerd-fontをインストールします.

brew tap homebrew/cask-fonts &&
brew install --cask font-hack-nerd-font

また,StarshipPowerline fontsを使用しているので,
こちらも同様にインストールしておきます.

git clone https://github.com/powerline/fonts.git --depth=1
cd fonts
./install.sh
cd ..
rm -rf fonts

インストールしたフォントを設定します.
Macのターミナルの場合は,Preferences > Profiles > Text > Font で,
名前にPowerlineとつくフォントを選択します.
自分はRoboto Mono for Powerlineを使用しました.

VSCodeなどでターミナルを使用する場合は,同様にフォントを追加します.
VSCodeの場合,左下歯車マークからSettingsにいき(もしくは⌘,),
fontと検索して,Editor: Font Family"Roboto Mono for Powerline"を追加します.

インストール

sh -c "$(curl -fsSL https://starship.rs/install.sh)"

もしくはHomebrewを使用して,

brew install starship

でインストールが可能です.
インストールができたら,~/.zshrcに以下を追加します.

# ~/.zshrc

eval "$(starship init zsh)"

プロンプトのカスタマイズ

Starshipではデフォルトで様々な機能が提供されています.

  • 各種言語の使用環境やバージョンの表示
  • コマンドの実行時間の表示
  • Gitのブランチや状態の表示

これらの設定を好きなように設定することができます.
設定は.toml形式で,~/.config/starship.tomlに記述します.

設定の詳細に関しては公式ドキュメントを参考にしてください.

自分は以下のように設定しました.

# https://starship.rs/config/
add_newline = true

# Replace the "❯" symbol in the prompt with "➜"
[character] # The name of the module we are configuring is "character"
error_symbol = "[✗](bold red)" 
success_symbol = "[](bold purple)" # The "success_symbol" segment is being set to "➜" with the color "bold green"

[aws] # display everything
format = 'on [$symbol($profile )(\($region\) )]($style)'
style = "bold blue"
[aws.region_aliases]
ap-southeast-2 = "au"
us-east-1 = "va"

[cmd_duration]
format = "[$duration]($style) " 
min_time = 1_000 # millisecond
show_notifications = true 
min_time_to_notify = 60_000 # millisecond

[directory]
truncation_length = 5
truncate_to_repo = false
truncation_symbol = "…/"

[docker_context]
format = "via [🐋 $context](blue bold)"

[python]
pyenv_version_name = true

Reference

関連記事

yiskw713.hatenablog.com

yiskw713.hatenablog.com