1-minute implementation
Copy this into Claude Code and tell it to set this up for you. It’ll install Hammerspoon, create the auto-close script and write the config.
Once Claude Code is done, this is all that’s left for you:
- Grant Hammerspoon Accessibility permissions when macOS prompts you (System Settings > Privacy & Security > Accessibility > enable Hammerspoon)
- If you want, set Hammerspoon to launch at login from its menu bar icon
Now type <instructions> and watch </instructions> appear on the next line, with your cursor between the tags.
5-minute manual method
If you use Claude Code a lot, you’ve probably noticed that XML-structured prompts get much better results. Wrap what you want in tags like <instructions>, <context>, <example> and <constraints> and Claude parses complex prompts more reliably, especially when you’re juggling several pieces of context in one message.
But typing closing tags in a terminal is tedious. There’s no auto-complete, no Emmet and no IDE shortcuts, so you type <instructions> and then you type </instructions> by hand. Nest a few tags or use several in one prompt and it adds up fast. It’s a small thing, but it nudges you towards lazier, unstructured prompts.
So I built a little keystroke-level automation for it. You type an opening XML tag, press >, and it inserts the closing tag on the next line and puts your cursor between them. It works in any app on macOS, including Claude Code, iTerm2 and VS Code’s terminal. And once the friction is gone you end up reaching for XML tags by default.
How it works
It runs on Hammerspoon, a free and open-source macOS automation tool that can intercept keystrokes at the OS level. A Lua script watches what you type. When it sees you finish an opening XML tag with >, it simulates typing the matching closing tag on the next line.
You type: <instructions>
It becomes:
<instructions>
| <-- cursor here
</instructions>
What you need
- macOS (Hammerspoon is macOS-only)
- Homebrew installed
Step 1: Install Hammerspoon
brew install --cask hammerspoon
Then launch it:
open -a Hammerspoon
macOS will ask for Accessibility permissions. Go to System Settings > Privacy & Security > Accessibility and enable Hammerspoon. You may need to relaunch Hammerspoon after that.
Step 2: Create the auto-close script
Create the file ~/.hammerspoon/xml_autoclose.lua with this in it:
-- XML Auto-Close Tag
-- Typing <tagname> auto-inserts </tagname> on the next line.
-- Toggle with Cmd+Shift+X.
local M = {}
local buffer = ""
local enabled = true
local isInserting = false
local maxBufferSize = 200
-- Toggle hotkey
hs.hotkey.bind({"cmd", "shift"}, "x", function()
enabled = not enabled
hs.alert.show("XML Auto-Close: " .. (enabled and "ON" or "OFF"))
end)
-- Keystroke watcher
M.watcher = hs.eventtap.new({hs.eventtap.event.types.keyDown}, function(event)
if not enabled or isInserting then return false end
local keyCode = event:getKeyCode()
local flags = event:getFlags()
-- Ignore keyboard shortcuts
if flags.cmd or flags.ctrl then return false end
-- Backspace: trim buffer
if keyCode == 51 then
if #buffer > 0 then
buffer = buffer:sub(1, -2)
end
return false
end
-- Enter/Return: clear buffer
if keyCode == 36 then
buffer = ""
return false
end
local char = event:getCharacters()
if not char or #char == 0 or #char > 1 then return false end
buffer = buffer .. char
if #buffer > maxBufferSize then
buffer = buffer:sub(-maxBufferSize)
end
if char == ">" then
-- Match <tagname> (hyphens, underscores, colons, dots allowed)
local tag = buffer:match("<([%a_][%w%-_:.]*)>$")
-- Also match tags with attributes: <tag attr="val">
if not tag then
tag = buffer:match("<([%a_][%w%-_:.]*)%s[^>]*>$")
end
-- Ignore self-closing tags like <br/>
if tag and buffer:match("/>$") then
tag = nil
end
if tag then
buffer = ""
isInserting = true
hs.timer.doAfter(0.03, function()
-- Insert newline (Shift+Enter for Claude Code)
hs.eventtap.keyStroke({"shift"}, "return", 0)
hs.timer.doAfter(0.03, function()
hs.eventtap.keyStrokes("</" .. tag .. ">")
-- Move cursor between the tags
hs.timer.doAfter(0.05, function()
hs.eventtap.keyStroke({}, "up", 0)
hs.timer.doAfter(0.02, function()
hs.eventtap.keyStroke({"cmd"}, "right", 0)
hs.timer.doAfter(0.02, function()
isInserting = false
end)
end)
end)
end)
end)
end
end
return false
end)
M.watcher:start()
return M
If your newline key is different
The script uses Shift+Enter for newlines, which is a common Claude Code setup. If yours uses a different key, update this line:
hs.eventtap.keyStroke({"shift"}, "return", 0)
For Option+Enter:
hs.eventtap.keyStroke({"alt"}, "return", 0)
You can check yours in ~/.claude/keybindings.json. If you haven’t customised it, try both and see which one gives you a newline in your Claude Code input.
Step 3: Create the init file
Create ~/.hammerspoon/init.lua:
-- Hammerspoon config
require("xml_autoclose")
-- Auto-reload config on save
hs.pathwatcher.new(os.getenv("HOME") .. "/.hammerspoon/", function(files)
hs.reload()
end):start()
hs.alert.show("Hammerspoon loaded")
If you’ve already got an init.lua, add require("xml_autoclose") to it.
Step 4: Test it
Open your terminal and type <test>. As soon as you press >, </test> should appear on the next line with your cursor between the two tags.
Cmd+Shift+X toggles it on and off, and an alert tells you which state it’s in.
What it handles
- Simple tags:
<instructions>inserts</instructions> - Hyphenated tags:
<system-prompt>inserts</system-prompt> - Namespaced tags:
<xml:tag>inserts</xml:tag> - Tags with attributes:
<div class="foo">inserts</div> - Self-closing tags like
<br/>get ignored, so no closing tag there - Closing tags like
</div>get ignored too, so you never get a double close
Why bother with XML tags?
Claude models are trained to understand XML-structured input. So when you wrap parts of your prompt in semantic tags, Claude gets explicit cues about which bits are context, which are instructions and which are constraints.
Instead of:
Here's some context about my project. It uses React and TypeScript.
Now here's what I want you to do. Refactor the auth module.
Make sure to keep backwards compatibility.
Try:
<context>
My project uses React and TypeScript.
</context>
<instructions>
Refactor the auth module.
</instructions>
<constraints>
Maintain backwards compatibility.
</constraints>
The second one’s easier for you to read, and Claude parses it more reliably. It helps even more once prompts get complex, like when you’ve got nested context, several constraints, examples with expected outputs or persona instructions.
Some useful tags:
<instructions>for what you want Claude to do<context>for background information<constraints>for rules and boundaries<example>for input and output examples<system-prompt>for persona or behaviour instructions<code>for code snippets you want it to refer to<output-format>for how you want the response structured
Tips
- Launch at login. Click the Hammerspoon menu bar icon, then Preferences, then “Launch Hammerspoon at login.”
- Works everywhere. You’ll get this in any text input on macOS, so VS Code, Slack and Notes too.
- Auto-reload. The init file has a path watcher, so Hammerspoon picks up anything you save in
~/.hammerspoon/and you don’t have to relaunch. - Quick disable. If it gets in the way, like when you’re typing HTML in a code editor, press Cmd+Shift+X to toggle it off.
- Customise the toggle. If Cmd+Shift+X clashes with another shortcut, edit the
hs.hotkey.bindline inxml_autoclose.lua.