0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Starship + dotfiles セットアップガイド

Posted at

Starship + dotfiles セットアップガイド

macOS、WSL(bash/zsh両対応)で統一されたプロンプト環境を構築するためのガイドです。

概要

  • Starship: 高速で現代的なクロスプラットフォームプロンプト
  • dotfiles管理: 設定ファイルをGitで管理し、複数環境で同期
  • bash/zsh両対応: どちらのシェルでも同じ設定を使用可能

1. Starshipのインストール

macOS

# Homebrewでインストール(推奨)
brew install starship

# または直接インストール
curl -sS https://starship.rs/install.sh | sh

WSL(Ubuntu/Debian)

# 直接インストール
curl -sS https://starship.rs/install.sh | sh

# またはHomebrew in WSL
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
brew install starship

2. dotfiles管理の準備

ディレクトリ構造の作成

# ホームディレクトリに移動
cd ~

# dotfilesディレクトリ作成
mkdir -p dotfiles/.config

# 既存の設定ファイルをバックアップ
cp ~/.bashrc ~/.bashrc.backup 2>/dev/null || true
cp ~/.zshrc ~/.zshrc.backup 2>/dev/null || true

3. 設定ファイルの作成

~/.bashrc(~/dotfiles/.bashrc)

# ~/.bashrc または ~/dotfiles/.bashrc

# 基本設定
export EDITOR=vim
export LANG=ja_JP.UTF-8

# プラットフォーム判定
case $OSTYPE in
  darwin*)
    # macOS設定
    export CLICOLOR=1
    export LSCOLORS=ExFxBxDxCxegedabagacad
    
    # Homebrew PATH (Apple Silicon)
    if [[ -d /opt/homebrew/bin ]]; then
      export PATH="/opt/homebrew/bin:$PATH"
    fi
    
    # Homebrew PATH (Intel)
    if [[ -d /usr/local/bin ]]; then
      export PATH="/usr/local/bin:$PATH"
    fi
    ;;
  linux*)
    # Linux/WSL設定
    alias ls='ls --color=auto'
    alias grep='grep --color=auto'
    
    # WSL固有設定
    if grep -qi microsoft /proc/version 2>/dev/null; then
      # WSL環境
      export DISPLAY=$(ip route list default | awk '{print $3}'):0
    fi
    ;;
esac

# 共通エイリアス
alias ll='ls -alF'
alias la='ls -A'
alias l='ls -CF'
alias ..='cd ..'
alias ...='cd ../..'

# Git エイリアス
alias g='git'
alias gs='git status'
alias ga='git add'
alias gc='git commit'
alias gp='git push'
alias gl='git log --oneline'
alias gd='git diff'
alias gb='git branch'
alias gco='git checkout'

# Starship初期化
eval "$(starship init bash)"

# ローカル設定があれば読み込み
[[ -f ~/.bashrc.local ]] && source ~/.bashrc.local

~/.zshrc(~/dotfiles/.zshrc)

# ~/.zshrc または ~/dotfiles/.zshrc

# 基本設定
export EDITOR=vim
export LANG=ja_JP.UTF-8

# zsh設定
setopt AUTO_CD
setopt CORRECT
setopt SHARE_HISTORY
setopt HIST_IGNORE_DUPS
setopt HIST_IGNORE_ALL_DUPS
setopt HIST_SAVE_NO_DUPS
export HISTFILE=~/.zsh_history
export HISTSIZE=10000
export SAVEHIST=10000

# プラットフォーム判定
case $OSTYPE in
  darwin*)
    # macOS設定
    export CLICOLOR=1
    export LSCOLORS=ExFxBxDxCxegedabagacad
    
    # Homebrew PATH
    if [[ -d /opt/homebrew/bin ]]; then
      export PATH="/opt/homebrew/bin:$PATH"
    fi
    if [[ -d /usr/local/bin ]]; then
      export PATH="/usr/local/bin:$PATH"
    fi
    ;;
  linux*)
    # Linux/WSL設定
    alias ls='ls --color=auto'
    alias grep='grep --color=auto'
    
    # WSL固有設定
    if grep -qi microsoft /proc/version 2>/dev/null; then
      export DISPLAY=$(ip route list default | awk '{print $3}'):0
    fi
    ;;
esac

# 共通エイリアス
alias ll='ls -alF'
alias la='ls -A'
alias l='ls -CF'
alias ..='cd ..'
alias ...='cd ../..'

# Git エイリアス
alias g='git'
alias gs='git status'
alias ga='git add'
alias gc='git commit'
alias gp='git push'
alias gl='git log --oneline'
alias gd='git diff'
alias gb='git branch'
alias gco='git checkout'

# Starship初期化
eval "$(starship init zsh)"

# ローカル設定があれば読み込み
[[ -f ~/.zshrc.local ]] && source ~/.zshrc.local

~/.config/starship.toml(~/dotfiles/.config/starship.toml)

# ~/.config/starship.toml または ~/dotfiles/.config/starship.toml

# プロンプト全体の設定
format = """
$username\
$hostname\
$directory\
$git_branch\
$git_status\
$nodejs\
$python\
$rust\
$java\
$docker_context\
$kubernetes\
$cmd_duration\
$line_break\
$character"""

# 文字設定
[character]
success_symbol = "[➜](bold green)"
error_symbol = "[➜](bold red)"
vicmd_symbol = "[V](bold green)"

# ディレクトリ表示
[directory]
style = "cyan bold"
truncation_length = 3
truncation_symbol = "…/"
home_symbol = "🏠"

# Git ブランチ
[git_branch]
symbol = "🌱 "
style = "bold purple"
truncation_length = 20
truncation_symbol = "…"

# Git ステータス
[git_status]
style = "bold red"
ahead = "↑${count}"
diverged = "↕↑${ahead_count}↓${behind_count}"
behind = "↓${count}"
conflicted = "CONFLICT"
untracked = "UNTRACKED"
stashed = "STASHED"
modified = "MODIFIED"
staged = "STAGED(${count})"
renamed = "RENAMED"
deleted = "DELETED"

# ユーザー名
[username]
style_user = "yellow bold"
style_root = "red bold"
format = "[$user]($style) "
disabled = false
show_always = true

# ホスト名
[hostname]
ssh_only = false
format = "on [$hostname](bold blue) "
disabled = false

# 実行時間
[cmd_duration]
min_time = 2_000
format = "took [$duration](bold yellow)"

# プログラミング言語固有設定
[nodejs]
symbol = "⬢ "
style = "bold green"

[python]
symbol = "🐍 "
style = "bold yellow"

[rust]
symbol = "🦀 "
style = "bold red"

[java]
symbol = "☕ "
style = "bold red"

# Docker
[docker_context]
symbol = "🐳 "
style = "bold blue"

# Kubernetes
[kubernetes]
format = 'on [⛵ $context \($namespace\)](dimmed green) '
disabled = false

4. 設定の適用

シンボリックリンクの作成

# dotfilesディレクトリの設定を実際の場所にリンク
ln -sf ~/dotfiles/.bashrc ~/.bashrc
ln -sf ~/dotfiles/.zshrc ~/.zshrc
mkdir -p ~/.config
ln -sf ~/dotfiles/.config/starship.toml ~/.config/starship.toml

設定の読み込み

# 現在使用しているシェルに応じて実行
source ~/.bashrc   # bashの場合
# または
source ~/.zshrc    # zshの場合

5. Git管理の設定(推奨)

.gitignoreの作成

cat > ~/dotfiles/.gitignore << 'EOF'
# dotfiles用 .gitignore
.DS_Store
*.swp
*.swo
*~
.bashrc.local
.zshrc.local
EOF

Gitリポジトリの初期化

cd ~/dotfiles
git init
git add .
git commit -m "feat: 初期dotfiles設定 (Starship + cross-platform)"

# リモートリポジトリに登録(オプション)
# git remote add origin https://github.com/yourusername/dotfiles.git
# git push -u origin main

6. フォント設定(推奨)

Starshipのアイコンを正しく表示するためのNerd Fontインストール:

macOS

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

WSL

Windows Terminalの設定でNerd Fontを指定してください。

7. 動作確認

# プロンプトの変更確認
echo "プロンプトが変更されましたか?"

# Gitリポジトリでの動作確認
cd /path/to/any/git/repository
# Git情報(ブランチ名、変更状態など)が表示されることを確認

8. カスタマイズ

ローカル設定

環境固有の設定は ~/.bashrc.local または ~/.zshrc.local に記述すると、dotfilesリポジトリに影響せずに追加設定が可能です。

Starship設定のカスタマイズ

~/.config/starship.toml を編集することで、プロンプトの見た目や表示する情報をカスタマイズできます。

詳細は Starship公式ドキュメント を参照してください。

トラブルシューティング

よくある問題

  1. プロンプトが変わらない

    • source ~/.bashrc または source ~/.zshrc を実行
    • 新しいターミナルを開く
  2. アイコンが正しく表示されない

    • Nerd Fontがインストールされているか確認
    • ターミナルでNerd Fontが選択されているか確認
  3. エラーが表示される

    • Starshipが正しくインストールされているか確認
    • 設定ファイルのシンタックスエラーをチェック
  4. WSLで動作しない

    • WSL内でStarshipがインストールされているか確認
    • パスの設定を確認

デバッグコマンド

# Starshipのバージョン確認
starship --version

# 設定のテスト
starship config

# 詳細なデバッグ情報
starship explain

まとめ

この設定により、macOS、WSL環境でbash/zsh問わず統一されたプロンプト環境が構築できます。dotfiles管理によって設定の同期も簡単になります。

設定は個人の好みに応じてカスタマイズしてください!

0
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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?