Created
October 16, 2025 09:36
-
-
Save arathunku/b0e73dd90563d007510192734fde28e7 to your computer and use it in GitHub Desktop.
add bash commands executed via claude to atuin history
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env fish | |
| # Claude Code hook to track Bash commands in atuin | |
| # Add to .claude/settings.json PostToolUse hooks for Bash tool | |
| # "PostToolUse": [ | |
| # { | |
| # "hooks": [ | |
| # { | |
| # "type": "command", | |
| # "command": "claude-atuin-tracker" | |
| # } | |
| # ] | |
| # } | |
| # ] | |
| # I didn't find a good way to handle duration yet... not all started commands via PreToolUse | |
| # will have an end and may run in parallel. | |
| # input: | |
| # { | |
| # "session_id": "abc123", | |
| # "transcript_path": "/Users/.../.claude/projects/.../00893aaf-19fa-41d2-8238-13269b9b3ca0.jsonl", | |
| # "cwd": "/Users/...", | |
| # "hook_event_name": "PostToolUse", | |
| # "tool_name": "Write", | |
| # "tool_input": { | |
| # "file_path": "/path/to/file.txt", | |
| # "content": "file content" | |
| # }, | |
| # "tool_response": { | |
| # "filePath": "/path/to/file.txt", | |
| # "success": true | |
| # } | |
| # } | |
| set -xg ATUIN_HOST_USER "claude" | |
| set json (cat) | |
| if test -z "$json" | |
| exit 0 | |
| end | |
| if not echo "$json" | jq empty 2>/dev/null | |
| echo "Invalid JSON input" >&2 | |
| exit 1 | |
| end | |
| # Parse hook data | |
| set hook_event_name (echo "$json" | jq -r '.hook_event_name // ""') | |
| set tool_name (echo "$json" | jq -r '.tool_name // ""') | |
| # Only process PostToolUse for Bash tool | |
| if test "$hook_event_name" != "PostToolUse" -o "$tool_name" != "Bash" | |
| exit 0 | |
| end | |
| # Extract command details | |
| set command (echo "$json" | jq -r '.tool_input.command // ""') | |
| set cwd (echo "$json" | jq -r '.cwd // ""') | |
| set description (echo "$json" | jq -r '.tool_input.description // ""') | |
| # Get exit code from tool response success field | |
| set success (echo "$json" | jq -r '.tool_response.success // true') | |
| set exit_code 0 | |
| if test "$success" = "false" | |
| set exit_code 1 | |
| end | |
| if test -z "$command" | |
| exit 0 | |
| end | |
| # Change to the working directory where command was executed | |
| if test -n "$cwd" -a -d "$cwd" | |
| cd "$cwd" | |
| end | |
| # Track in atuin | |
| # Start the history entry and capture the ID | |
| set atuin_id (atuin history start -- $command 2>/dev/null | string trim) | |
| if test -z "$atuin_id" | |
| echo "Failed to start atuin history tracking" >&2 | |
| exit 0 | |
| end | |
| # End the history entry with exit code | |
| atuin history end --exit $exit_code -- $atuin_id 2>/dev/null |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment