update-search-year — Claude Code hook: current year in search queries
This hook appends the current year to WebSearch queries that lack one. Useful when you want fresh results instead of last year's pages.
npx claude-code-templates@latest --hook pre-tool/update-search-yearWhat update-search-year does
The hook intercepts WebSearch calls and inspects the query string. If there is no year in it and no words like "latest", "recent", "current", "new", "now" or "today", the current year gets appended. The search then runs with it.
Under the hood it is a single PreToolUse hook with the WebSearch matcher that runs python3 -c with a short script. The script reads JSON from stdin, pulls the query field, looks for a four-digit year with a regex, and returns the changed input through modifiedToolInput. Timeout is 5 seconds.
You need python3 on the machine. If the query already has a year or a temporal word, the hook leaves it alone — worth remembering when your phrasing itself filters out fresh pages.
What it helps with
- append the current year to a search query
- get fresh results instead of stale ones
- stop rewriting every WebSearch query by hand
- leave queries with a year or "latest" untouched
Example prompts for the agent
- search the web for what's new in React
- look up the latest changes to this API
- find the docs for this library
How to install update-search-year
1claude-code-templates
npx claude-code-templates@latest --hook pre-tool/update-search-year2Manually: put the files in place
.claude/settings.jsonThe hooks block: event (PreToolUse, PostToolUse, Stop…) → command. Personal ones go to ~/.claude/settings.json
Step by step, with commands for each platform: Claude Code hooks: what they are, where to configure them, how to block a risky command
Questions about update-search-year
How do I install update-search-year in Claude?
Run npx claude-code-templates@latest --hook pre-tool/update-search-year, then check .claude/settings.json.
What does update-search-year do?
It intercepts WebSearch calls and appends the current year to the query when no year is present.
When does the hook skip a query?
When the query already contains a year or one of the words "latest", "recent", "current", "new", "now", "today".
Source text
update-search-year.json · MIT
{
"description": "Automatically adds current year to WebSearch queries when no year is specified. This hook intercepts WebSearch tool usage and appends the current year to queries that don't already contain a year, ensuring search results are current and relevant.",
"hooks": {
"PreToolUse": [
{
"matcher": "WebSearch",
"hooks": [
{
"type": "command",
"command": "python3 -c \"import json, sys, re; from datetime import datetime; input_data = json.load(sys.stdin); tool_input = input_data.get('tool_input', {}); query = tool_input.get('query', ''); current_year = str(datetime.now().year); has_year = re.search(r'\\\\b20\\\\d{2}\\\\b', query); has_temporal = any(word in query.lower() for word in ['latest', 'recent', 'current', 'new', 'now', 'today']); should_add_year = not has_year and not has_temporal; modified_query = f'{query} {current_year}' if should_add_year else query; output = {'hookSpecificOutput': {'hookEventName': 'PreToolUse', 'modifiedToolInput': {'query': modified_query}}}; print(json.dumps(output)); sys.exit(0)\"",
"timeout": 5
}
]
}
]
}
}
© davila7 · MIT · davila7/claude-code-templates