Skip to content

refactor(zsh): 設定ファイルをディレクトリ構造で再編成#28

Merged
nyatinte merged 5 commits intomainfrom
claude/review-zsh-config-7wOHq
Apr 6, 2026
Merged

refactor(zsh): 設定ファイルをディレクトリ構造で再編成#28
nyatinte merged 5 commits intomainfrom
claude/review-zsh-config-7wOHq

Conversation

@nyatinte
Copy link
Copy Markdown
Owner

@nyatinte nyatinte commented Apr 6, 2026

概要

フラットに並んでいた zsh 設定ファイルを、責務ごとにディレクトリ分けして再編しました。

変更前

~/.zsh.d/
  path.zsh       # PATH定義
  dev-tools.zsh  # 開発ツール全般(責務が広すぎた)
  fzf.zsh        # fzf設定
  utils.zsh      # ユーティリティ関数

dev-tools.zsh が proto の起動、Docker 初期化、エディタ設定、パッケージマネージャ設定、Vite+ の読み込みなど、無関係な設定をすべて抱えていた。また path.zshdev-tools.zsh に PATH 追加が重複していた。

変更後

~/.zsh.d/
  core/
    path.zsh     # PATH 一元管理
    env.zsh      # 環境変数(EDITOR, NI_*, LG_CONFIG_FILE)
  tools/
    proto.zsh    # proto バージョンマネージャの起動
    docker.zsh   # Docker Desktop 初期化(任意)
    fzf.zsh      # fzf キーバインド・プレビュー設定
    vite-plus.zsh # Vite+ 環境変数(任意)
  functions/
    git.zsh      # gwn(git worktree ヘルパー)
    notify.zsh   # notify-after(コマンド完了通知)

機能差分

リファクタリングにあたり全項目を照合した結果、実質的な挙動の変化は 1点のみ です:

項目 変更前 変更後
vite-plus の読み込み . "$HOME/.vite-plus/env"(無条件) [[ -f ... ]] && source(存在確認あり)

変更前はファイルが存在しない環境でシェル起動がエラー終了するバグがありました。今回の変更で安全になっています。

その他の変更は機能的に同等です:

  • dev-tools.zsh にあった export PATH="$PROTO_HOME/tools/node/globals/bin:$PATH"core/path.zshtypeset -U path PATH + path 配列に同エントリが既にあったため、削除しても動作は変わりません
  • 環境変数の読み込み順が fpath 設定より前になりましたが、独立した変数のため影響なし

その他の改善点

  • functions/ はグロブ(*.zsh(N))で読み込むため、新しい関数ファイルを置くだけで自動認識されます
  • (N) glob 修飾子により、functions/ が空でもエラーになりません
  • .local.zsh の存在チェックを [ -f ] から [[ -f ]] に統一しました

claude added 2 commits April 6, 2026 13:06
…tories

- core/path.zsh: PATH management (consolidated from path.zsh + dev-tools.zsh)
- core/env.zsh: environment variables (EDITOR, NI_*, LG_CONFIG_FILE)
- tools/proto.zsh, docker.zsh, fzf.zsh, vite-plus.zsh: per-tool activation
- functions/git.zsh, notify.zsh: utility functions split by domain

https://claude.ai/code/session_01GkwneqBX3TWvXCVmAsAU33
…acket style

- Remove self-documenting comments from env.zsh, docker.zsh, vite-plus.zsh
- Use (N) glob qualifier in functions loop to handle empty directory safely
- Standardize to [[ ]] throughout (was mixing [ ] for .local.zsh)

https://claude.ai/code/session_01GkwneqBX3TWvXCVmAsAU33
@nyatinte nyatinte changed the title refactor(zsh): reorganize config into core/, tools/, functions/ directories refactor(zsh): 設定ファイルをディレクトリ構造で再編成 Apr 6, 2026
Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request refactors the Zsh configuration by modularizing settings into dedicated directories for core environment variables, tools, and functions. The .zshrc file has been updated to source these new locations, including a dynamic loop for loading functions. Feedback includes a suggestion to improve the notify-after function by displaying the full command string instead of just the first argument, and a recommendation to use a similar loop for sourcing tool configurations to enhance maintainability.

Comment thread home/dot_zsh.d/functions/notify.zsh Outdated
Comment on lines +5 to +9
if [ $exit_code -eq 0 ]; then
cmux notify --title "✓ Command Complete" --body "$1"
else
cmux notify --title "✗ Command Failed" --body "$1 (exit $exit_code)"
fi
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The notification currently only shows the first argument of the command (e.g., sleep instead of sleep 10). Using "$*" will provide the full command string in the notification body, making it much more informative for the user.

	if [ $exit_code -eq 0 ]; then
		cmux notify --title "✓ Command Complete" --body "$*"
	else
		cmux notify --title "✗ Command Failed" --body "$* (exit $exit_code)"
	fi

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ご指摘ありがとうございます。$1 だと sleep 10 の通知が sleep だけになってしまいますね。$* に修正しました。


Generated by Claude Code

Comment thread home/dot_zshrc
Comment on lines +12 to +15
source "${ZSH_CONFIG_DIR}/tools/proto.zsh"
source "${ZSH_CONFIG_DIR}/tools/docker.zsh"
source "${ZSH_CONFIG_DIR}/tools/fzf.zsh"
source "${ZSH_CONFIG_DIR}/tools/vite-plus.zsh"
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

To improve maintainability and consistency with how functions are loaded (lines 24-26), consider using a loop to source all files in the tools/ directory. This allows for adding or removing tool configurations without needing to manually update .zshrc each time.

for f in "${ZSH_CONFIG_DIR}/tools/"*.zsh(N); do
	source "$f"
done

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tools/ はロード順に意味があります(core/env.zsh で設定した変数を proto.zsh が参照するなど)。グロブによるアルファベット順読み込みでは順序が保証できないため、今回は明示的 source を維持します。


Generated by Claude Code

Ensures chezmoi apply cleans up old files from ~/.zsh.d/:
- remove_path.zsh      → deletes ~/.zsh.d/path.zsh
- remove_dev-tools.zsh → deletes ~/.zsh.d/dev-tools.zsh
- remove_fzf.zsh       → deletes ~/.zsh.d/fzf.zsh
- remove_utils.zsh     → deletes ~/.zsh.d/utils.zsh

https://claude.ai/code/session_01GkwneqBX3TWvXCVmAsAU33
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

zsh 設定ファイルを責務ごとにディレクトリ分割し、.zshrc から新構成を読み込むように整理するPRです(Vite+ の env 読み込みは存在チェック付きに変更)。

Changes:

  • ~/.zsh.dcore/, tools/, functions/ に再編し、.zshrc の読み込み順・対象を更新
  • PATH/環境変数を core/path.zsh, core/env.zsh に分離し、ツール設定を tools/ 配下へ移動
  • notify-afterfunctions/notify.zsh に分離し、関数ファイルをグロブで自動読み込み

Reviewed changes

Copilot reviewed 8 out of 14 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
home/dot_zshrc 新ディレクトリ構成に合わせて core/tools/functions を読み込むよう変更
home/dot_zsh.d/core/path.zsh PATH 管理を配列 + typeset -U で一元化
home/dot_zsh.d/core/env.zsh エディタ/ni/lazygit などの環境変数を分離
home/dot_zsh.d/tools/proto.zsh proto 有効化設定を分離
home/dot_zsh.d/tools/docker.zsh Docker init の任意読み込みを分離
home/dot_zsh.d/tools/vite-plus.zsh Vite+ env を存在チェック付きで読み込み
home/dot_zsh.d/tools/fzf.zsh fzf のキーバインド/ウィジェット設定を独立ファイル化
home/dot_zsh.d/functions/git.zsh notify-after を分離したため削除
home/dot_zsh.d/functions/notify.zsh notify-after を新規ファイルとして追加
home/dot_zsh.d/dev-tools.zsh 旧まとめファイルを削除

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread home/dot_zshrc
Comment on lines +24 to +26
for f in "${ZSH_CONFIG_DIR}/functions/"*.zsh(N); do
source "$f"
done
Copy link

Copilot AI Apr 6, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The functions autoload loop can behave unexpectedly when the glob matches nothing: in many shells (including zsh), for f in; do ... falls back to iterating over positional parameters. If functions/*.zsh(N) expands to empty (e.g., directory missing/empty), this could end up attempting to source arbitrary shell args. Consider guarding with an explicit existence check (e.g., build an array of matches and only loop when non-empty) or add a [[ -r $f ]] || continue inside the loop.

Suggested change
for f in "${ZSH_CONFIG_DIR}/functions/"*.zsh(N); do
source "$f"
done
function_files=("${ZSH_CONFIG_DIR}/functions/"*.zsh(N))
if (( ${#function_files[@]} )); then
for f in "${function_files[@]}"; do
source "$f"
done
fi

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

zsh の (N) glob 修飾子(Null glob)は、マッチするファイルが0件のとき展開結果を空にしてループをスキップします。for f in *.zsh(N) でマッチなしの場合にループ本体は実行されないため、配列化によるガードは不要と判断し現状を維持します。


Generated by Claude Code

Comment thread home/dot_zsh.d/functions/notify.zsh Outdated
notify-after() {
"$@"
local exit_code=$?
if [ $exit_code -eq 0 ]; then
Copy link

Copilot AI Apr 6, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use of single-bracket test with an unquoted variable is brittle in zsh ([ $exit_code -eq 0 ] can error if the variable is ever empty/non-numeric). Prefer zsh-native arithmetic ((( exit_code == 0 ))) or [[ $exit_code -eq 0 ]] for a more robust numeric check.

Suggested change
if [ $exit_code -eq 0 ]; then
if (( exit_code == 0 )); then

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ご指摘ありがとうございます。(( exit_code == 0 )) に修正しました。


Generated by Claude Code

@nyatinte
Copy link
Copy Markdown
Owner Author

nyatinte commented Apr 6, 2026

@copilot 機能のデグレない?

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 6, 2026

@copilot 機能のデグレない?

機能のデグレはありません。全項目を照合した結果:

  • core/path.zsh — 元の path.zsh と同一(typeset -U の重複排除で dev-tools.zsh 側の $PROTO_HOME/tools/node/globals/bin の再追加も吸収済み)
  • tools/proto.zsheval "$(proto activate zsh --no-shim)" + PROTO_AUTO_INSTALL=true と同一
  • tools/docker.zsh — Docker init スクリプトの条件ロードと同一
  • tools/fzf.zsh — fzf 設定・キーバインドはすべて同一
  • core/env.zshVISUAL, EDITOR, NI_*, LG_CONFIG_FILE すべて同一
  • functions/git.zshgwn 関数と同一

意図的な変更(改善)は2点:

  1. vite-plus. "$HOME/.vite-plus/env"(無条件)→ [[ -f ... ]] && source(存在確認付き)。元のコードはファイル不在時にシェル起動がエラー終了するバグがありました
  2. notify-after — 通知本文が $1(コマンド名のみ)→ $*(コマンド全体)に修正。こちらも元がバグでした

@nyatinte nyatinte merged commit 93d3e2e into main Apr 6, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants