Skip to content
Skillpedia

Claude Code Hooks: Automate Tasks Without Coding

Skillpedia team · updated 2026-12-01 · 11 min read

Short answer: Hooks in Claude Code are commands that run on their own at set moments. For example, right after Claude edits a file, or before it runs something in the terminal. Slash commands are shortcuts you type with a slash, like /clear or /init. Together, they let Claude Code handle repetitive tasks without you asking every time.

You may have been there: every time Claude finishes a task, you have to remember to ask it to format the file. Or you go make a coffee and only find out ten minutes later that it was stuck, waiting for your approval. Hooks solve this kind of problem. You set them up once, and the action happens every time, without relying on anyone's memory.

The good news is that you can do this without knowing how to code. Claude Code has a menu for creating hooks, and it can also write the configuration for you. If you haven't installed the tool yet, start with our Claude Code guide.

Table of contents

  1. What are hooks in Claude Code?
  2. Slash commands: the shortcuts you can already use
  3. Useful hook examples for beginners
  4. How to create your first hook step by step
  5. Hooks vs skills vs plugins: which one to use?
  6. Security precautions
  7. Skills that help with this
  8. Frequently asked questions

What are hooks in Claude Code?

Hook means "gancho" in Portuguese, which translates to "hook" in the sense of something you hang on. The idea is to attach an action to a specific moment in Claude's work. When that moment comes, the action runs automatically.

The difference from a regular instruction is important. If you write in your CLAUDE.md "always format the code after editing," Claude will try to follow it, but it may forget. A hook doesn't forget. It is a command on your computer that runs every time, the same way.

These are the most commonly used moments (called events):

Event When it triggers Example use
PreToolUse Before Claude uses a tool Block a dangerous command
PostToolUse After the tool has finished Format the edited file
Notification When Claude needs your attention Show an alert on screen
Stop When Claude finishes responding Play a sound
UserPromptSubmit When you send a message Add a fixed piece of information
SessionStart When a session begins Load a project reminder

Hooks are stored in configuration files called settings.json. There are three places:

  • ~/.claude/settings.json: applies to all your projects.
  • .claude/settings.json: applies only to that project and can be shared with the team.
  • .claude/settings.local.json: applies only to that project and only on your computer.

The full list of events and options is in the official Claude Code documentation.

Slash commands: the shortcuts you can already use

Before creating hooks, it's worth knowing slash commands. These are commands that start with a slash, and you type them directly in the conversation. Type just / and Claude Code shows the list of what's available.

Commands that come ready-made

  • /help: shows help and the available commands.
  • /init: analyzes your project and creates a starter CLAUDE.md.
  • /clear: clears the conversation and starts from scratch. Great for changing topics.
  • /compact: summarizes the conversation to free up space and save tokens.
  • /model: switches Claude's model.
  • /memory: opens the memory files for editing.
  • /mcp: shows the connected MCP servers.
  • /agents: manages subagents.
  • /hooks: opens the menu to create and view hooks.
  • /permissions: controls what Claude can do without asking.

The list changes with updates, so use /help to see the one for your version.

Creating your own commands

You can also create custom shortcuts. All it takes is a text file. Here's how it works:

  1. Create the folder .claude/commands/ inside the project (or ~/.claude/commands/ to use it everywhere).
  2. Create a file, for example revisar-texto.md.
  3. Write inside it the request you would make to Claude.

Example content:

Revise o arquivo $ARGUMENTS. Corrija erros de português,
deixe as frases mais curtas e não mude o sentido do texto.

Now, when you type /revisar-texto artigo.md, Claude receives that full request. $ARGUMENTS is replaced with whatever you wrote after the command. It's like having a saved prompt that you reuse with a shortcut. In the newer versions, skills can also be called with a slash, so check the documentation to see which format your version recommends.

Useful hook examples for beginners

These are simple hooks that make a difference day to day:

  1. Alert when Claude needs you. Uses the Notification event. You leave Claude working, go do something else, and get an on-screen notification when it asks for approval.
  2. Sound when the task finishes. Uses the Stop event. A beep tells you it's time to come back.
  3. Format the code after every edit. Uses PostToolUse with the editing tools. The file always comes out tidy, without you asking.
  4. Block dangerous commands. Uses PreToolUse. If Claude tries something like deleting a whole folder or force-pushing in git, the hook stops it first.
  5. Protect sensitive files. Also with PreToolUse. It stops Claude from editing files like .env, where passwords and keys are kept.
  6. Keep a log. A hook that records every command run in a file. Useful for knowing what happened later.

Notice that examples 4 and 5 work like a gatekeeper. When a PreToolUse hook ends with exit code 2, Claude Code cancels the action and shows the reason to Claude, which then tries another route.

5 new skills a week. 1 minute to read.

The most viewed skills of the week, explained simply. Free.

No spam. Unsubscribe in one click.

How to create your first hook step by step

Let's create the sound hook for when Claude finishes. It's simple and you can see it working right away.

Way 1: through the /hooks menu

  1. Open Claude Code in the terminal.
  2. Type /hooks and press Enter.
  3. Choose the Stop event.
  4. Add a new hook and paste the command that plays the sound (see below).
  5. Choose where to save it: only in this project, or for all projects.

Way 2: asking Claude itself

This is the easiest path for people who don't code. Write something like:

Create a hook in Claude Code that plays a sound when you finish responding. I'm on a Mac. Save it in my user settings.

Claude will write the configuration and ask for your permission to save it. Read what it shows before you accept.

Way 3: editing the file

If you prefer to see everything, open settings.json in an editor like VS Code and add:

{
  "hooks": {
    "Stop": [
      {
        "hooks": [
          {
            "type": "command",
            "command": "afplay /System/Library/Sounds/Glass.aiff"
          }
        ]
      }
    ]
  }
}

This afplay command works on a Mac. On Windows or Linux, the sound command is different, and the quickest way is to ask Claude for the version for your system.

An example with a filter

Some events accept a field called matcher, which says which tools the hook applies to. This example formats the file only when Claude uses the editing and writing tools:

{
  "hooks": {
    "PostToolUse": [
      {
        "matcher": "Edit|Write",
        "hooks": [
          {
            "type": "command",
            "command": "jq -r '.tool_input.file_path' | xargs npx prettier --write"
          }
        ]
      }
    ]
  }
}

It depends on two installed tools (jq and Prettier). If you don't know what they are, that's fine: ask Claude to check and install them.

Testing

After saving, ask Claude for something simple. When it finishes, the sound should play. If nothing happens, type /hooks to check whether the hook appears in the list. Some changes only take effect after you restart the session.

Hooks vs skills vs plugins: which one to use?

Claude Code has several ways to customize it, and they're easy to mix up. This summary helps:

Feature What it is Who decides when to use it Good for
CLAUDE.md Fixed project instructions Claude always reads it Rules and context
Slash command Saved prompt with a shortcut You, when typing Requests you repeat
Skill Folder with instructions and files Claude, when the task calls for it Teaching a way to do something
Hook Automatic command The system itself, always Making sure something happens
Plugin Package that bundles all of this You install it Sharing a ready-made kit

A practical rule:

  • If something must happen every time, without exception, use a hook.
  • If it's knowledge Claude needs for certain tasks, use a skill.
  • If it's a request you make every week, create a slash command.
  • If you want all of this packaged and ready to go, look for a plugin.

Plugins can come with hooks already set up. That's convenient, but it leads straight to the next topic.

Security precautions

Hooks run on your computer, with the same permissions you have. And they run without asking for confirmation. A poorly built hook can delete files or send data out. So:

  • Understand it before installing. Don't paste a hook you don't understand. Ask Claude to explain it line by line.
  • Look at the hooks in plugins. When you install a plugin, check whether it includes hooks and what they run. Prefer well-known authors. The post on skill marketplaces has tips for evaluating this.
  • Be careful with downloaded projects. A cloned project may come with a .claude/settings.json full of hooks. Open it and read it before using Claude Code in it.
  • Test in a test folder. Create an empty folder, set up the hook there, and check that it only does what it should.
  • Never put passwords in the command. The project's settings.json may end up shared with other people.
  • Use hooks in your favor. The same hooks that carry risk can also protect you, such as the ones that block destructive commands.

Anthropic itself stresses these precautions in its documentation. It's worth reading the security section before building more complex hooks.

Skills that help with this

If you don't want to write hooks from scratch, these skills from the directory already do much of the work:

  • git-guardrails-claude-code: sets up a safety hook that blocks risky git commands, such as forced pushes and resets, before they run. A great first hook for anyone working with code.
  • Hooks Automation: has your agent run automatic steps before and after the work, such as formatting code and saving session notes.
  • careful: teaches the agent to stop and check before destructive commands, such as deleting files. It pairs well with blocking hooks.
  • verification-before-completion: makes the agent run real checks before saying it's done. It helps avoid the famous "done" that wasn't really done.
  • terminal-recovery: when a command fails, helps the agent read the error and try a precise fix, instead of repeating the same thing.

If you want the agent to also work with files and programs on your computer more broadly, the Desktop Commander MCP server is an option. It gives the agent a lot of power, so the security precautions above matter even more.

Frequently asked questions

Do I need to know how to code to use hooks in Claude Code? No. You can use the /hooks menu or ask Claude itself to create the hook. The important thing is to read and understand what it will do before you approve it.

What's the difference between a hook and a slash command? A slash command only runs when you type the shortcut. A hook runs on its own, whenever its event happens, without you doing anything.

Do hooks work on Windows? Yes. What changes is the command inside the hook, which needs to match your system. Ask Claude for the right version for Windows, Mac, or Linux.

Do hooks use up my Claude limit? The hook itself is a command on your computer and doesn't use model tokens. But if it returns text for Claude to read, that text goes into the conversation. For details on plans, see the post on Claude Code pricing and check current prices on the official website.

How do I turn off a hook that's causing problems? Type /hooks, find the hook, and remove it. Another option is to delete the matching section in settings.json.

Read also

Free MCP

Give your AI 15,000+ skills, for free

Plug the Skillpedia MCP into Claude, Cursor, Codex or any AI. Before each task, it finds the right skill on its own.

  1. 01Get your free key
  2. 02Paste one line into your AI
  3. 03Ask for any task

Full guide on this topic: Claude Code: What It Is, What It Does, and How to Start (2026 Guide)

Search skills →

5 new skills a week. 1 minute to read.

The most viewed skills of the week, explained simply. Free.

No spam. Unsubscribe in one click.