In this level, you'll move the sorting into a script that Claude Code runs without asking each time. The script also keeps a ledger of every file it tidies.
Colour key: 2.1 Give It a Script 2.2 Scope the Tool 2.3 Keep a Ledger
Scroll and the sheet below builds the files one step at a time, pinning each step's notes. One scroll moves one step; the step buttons, the arrow keys or j and k do the same.
Where Level 1 Left It
Specimen tidy-imports/SKILL.mdWhere Level 1 Left It
Here's SKILL.md at the end of level 1. Claude still does the sorting itself, from the three lines at the bottom; step 2.1 replaces those with a script.
The files at the start
.claude/skills/tidy-imports/SKILL.md
---name: tidy-importsdescription: Sort the import statements at the top of a filearguments: [path]argument-hint: "[file]"---Tidy the imports in $path.Sort the import statements at the top of the filealphabetically, one per line. Leave everything belowthe imports exactly as it was.
Asked to reorder lines, Claude will now and then reflow one it was told to leave alone. A script sorts the same way every time, so move the sorting into scripts/sort-imports.sh inside the skill's folder and make it executable with chmod +x. In SKILL.md, call the script as ${CLAUDE_SKILL_DIR}/scripts/sort-imports.sh2.1a. Claude Code fills in the skill's own folder, so the call works whichever folder the session is in.
The files after step 2.1
.claude/skills/tidy-imports/SKILL.md
---name: tidy-importsdescription: Sort the import statements at the top of a filearguments: [path]argument-hint: "[file]"---Tidy the imports in $path.Run the script on the file, by exactly this path:```bash${CLAUDE_SKILL_DIR}/scripts/sort-imports.sh "$path"```It sorts the import block in place. Do not reorder anylines yourself or edit the file after it runs. If thescript exits non-zero, report its message and stop.
2.1a The script call takes the place of the three lines that told Claude how to sort.
2.1bDo not reorder any lines yourself keeps Claude's hands off the sorting: it leaves the file alone after the script runs, and a non-zero exit ends the run with the script's message.
#!/usr/bin/env bash# Sort the import lines at the top of a file, in place.set -euo pipefailexport LC_ALL=C # byte order, so every machine sorts the same wayfile="$1"# How many lines from the top start with "import "?count=$(awk '/^import /{n++; next} {exit} END{print n+0}' "$file")if [ "$count" -eq 0 ]; then echo "no imports at the top of $file" exit 0fi{ head -n "$count" "$file" | sort; tail -n +"$((count + 1))" "$file"; } > "$file.tmp"mv "$file.tmp" "$file"echo "sorted $count imports in $file"
Claude Code asks before every Bash call unless something has approved it. allowed-tools approves the calls it names 2.2a, for the rest of the turn that started the skill. Give it the same ${CLAUDE_SKILL_DIR} path as the body (with :* for whatever arguments follow) and it approves that one script and nothing else. Claude Code fills the variable in, in both places, so the rule and the command always match. Every other tool works as it did, under your usual permission settings.
The rule matches the start of the command, so the script call from 2.1a has to use exactly that path, with no bash or ./ in front.
The files after step 2.2
.claude/skills/tidy-imports/SKILL.md
---name: tidy-importsdescription: Sort the import statements at the top of a filearguments: [path]argument-hint: "[file]"allowed-tools: Bash(${CLAUDE_SKILL_DIR}/scripts/sort-imports.sh:*)---Tidy the imports in $path.Run the script on the file, by exactly this path:```bash${CLAUDE_SKILL_DIR}/scripts/sort-imports.sh "$path"```It sorts the import block in place. Do not reorder anylines yourself or edit the file after it runs. If thescript exits non-zero, report its message and stop.
2.2aallowed-tools approves Bash calls that start with this path until you send your next message, and the :* covers whatever arguments follow.
Next, add a ledger so a later run can tell what an earlier one touched 2.3a. The script appends one line per file 2.3b to .claude/tidy-imports.log, with the path and the number of imports it sorted. The script is already approved, so writing the ledger needs no extra permission.
.claude/tidy-imports.log after two runs
src/app.ts 3src/routes/home.ts 2
The files after step 2.3
.claude/skills/tidy-imports/SKILL.md
---name: tidy-importsdescription: Sort the import statements at the top of a filearguments: [path]argument-hint: "[file]"allowed-tools: Bash(${CLAUDE_SKILL_DIR}/scripts/sort-imports.sh:*)---Tidy the imports in $path.Read .claude/tidy-imports.log if it exists. If $path isalready listed there, say so before going on.Run the script on the file, by exactly this path:```bash${CLAUDE_SKILL_DIR}/scripts/sort-imports.sh "$path"```It sorts the import block in place. Do not reorder anylines yourself or edit the file after it runs. If thescript exits non-zero, report its message and stop.The script also writes the ledger line. Never write to.claude/tidy-imports.log yourself.
2.3aRead .claude/tidy-imports.log if it exists makes a second run on the same file say so before it starts.
2.3bThe script also writes the ledger line keeps the ledger's format the same on every run and needs no permission beyond 2.2a.
#!/usr/bin/env bash# Sort the import lines at the top of a file, in place.set -euo pipefailexport LC_ALL=C # byte order, so every machine sorts the same wayfile="$1"ledger=".claude/tidy-imports.log"# How many lines from the top start with "import "?count=$(awk '/^import /{n++; next} {exit} END{print n+0}' "$file")if [ "$count" -eq 0 ]; then echo "no imports at the top of $file" exit 0fi{ head -n "$count" "$file" | sort; tail -n +"$((count + 1))" "$file"; } > "$file.tmp"mv "$file.tmp" "$file"printf '%s\t%s\n' "$file" "$count" >> "$ledger"echo "sorted $count imports in $file"
The Real Thing
project-tag_version is a real skill in this repository: it tags a release after a merge to main. Two of its marked lines do what you just did in steps 2.1 and 2.2; the first is something tidy-imports gets in level 3 R.a.
skills/project-tag_version/SKILL.md
---name: "Project: Tag Version"description: "Tag the release after a merge to main, computing the next semver tag with svu"...disable-model-invocation: trueallowed-tools: ["Bash(svu:*)", "Bash(git:*)", "Bash(~/.claude/library/scripts/safe-version-next.sh:*)"]...---# Tag the release with `svu`...```bashTAG="$("$HOME"/.claude/library/scripts/safe-version-next.sh)" && git tag "$TAG" && git push origin "$TAG"```...
R.adisable-model-invocation: true means only a person can start this skill, by typing its name; Claude never starts it on its own. Level 3 covers when you'd want that.
R.ballowed-tools names safe-version-next.sh by its full path, as you did in step 2.2, alongside svu and git, which it uses to read the history and push the tag.
R.cThe TAG= line runs the script and tags whatever it prints, so Claude never works out the version number itself: the same idea as step 2.1.