git stash for your prompt: end a message with /stash to keep it, /stash pop brings it back

git stash for your prompt. End a message with /stash to keep it on a stack instead of sending it:
refactor the auth middleware to use the new token cache /stash
/stash pop puts it back. Only the last word counts, so a message that mentions /stash is sent as written.
Turn on autoStash to also keep every draft you wipe without sending (Ctrl+U, select-all and delete, Ctrl+C). Short drafts (under 15 characters) and drafts you send are never kept that way.
A toast confirms each stash.
| Command | Does |
|---|---|
/stash or /stash list | Open the stack in a pane, with View (the whole draft), Pop and Drop per entry |
/stash pop [N] | Put draft N (default 0, the newest) back in the prompt box and remove it from the stack |
/stash apply [N] | Same, but keep it on the stack |
/stash drop [N] | Delete draft N |
/stash clear | Delete every stashed draft |
N can also be written stash@{N}.
/plugin install stash-stack --marketplace yyosifov/prompt-mods
| Setting | Default | Does |
|---|---|---|
autoStash | off | Also stashes a draft you wipe without sending |
showStatus | on | Shows stash: N · /stash in the status line while the stash holds anything |
To change one:
/plugin and pick stash-stack from your installed plugins.autoStash) or Show stash count (showStatus).The mod reloads with the new value at once; no restart is needed.
~/.claude/plugins/store/.hooks/register.tsx 200 lines1import type { EngineInterface, Register } from 'claude-code';
2import { atom, read, update } from 'claude-code';
3
4import type { Stashed } from '../types';
5import { parseArgs } from './args';
6import { push, stashRequest, vanished, wiped } from './capture';
7
8const PANE = 'stash-stack';
9const stack = atom({ plugin: 'stash-stack', key: 'stack' } as const, []);
10const viewing = atom({ plugin: 'stash-stack', key: 'viewing' } as const, null);
11
12// The stack is kept per project, across sessions, like git's.
13const storeKey = async ($: EngineInterface): Promise<string> => {
14 const root = await $.session.root();
15 return `stack:${root}`;
16};
17
18// Set from the `showStatus` option each time the module loads.
19let isStatusShown = true;
20let isAutoStash = false;
21
22const showCount = ($: EngineInterface, count: number): void => {
23 const isVisible = isStatusShown && count > 0;
24 $.ui.status(isVisible ? `stash: ${count} · /stash` : undefined);
25};
26
27const save = async ($: EngineInterface, next: Stashed[]): Promise<void> => {
28 await update($, stack, () => next);
29 await update($, viewing, () => null);
30 showCount($, next.length);
31 const key = await storeKey($);
32 await $.store.set(key, next);
33};
34
35const drop = async ($: EngineInterface, index: number): Promise<void> => {
36 const list = await read($, stack);
37 await save(
38 $,
39 list.filter((_, i) => i !== index),
40 );
41};
42
43const toggleView = async ($: EngineInterface, index: number): Promise<void> => {
44 await update($, viewing, (open) => (open === index ? null : index));
45};
46
47const preview = (text: string, width: number): string => {
48 const line = text.replace(/\s+/g, ' ').trim();
49 return line.length > width ? `${line.slice(0, width - 1)}…` : line;
50};
51
52// What the box held after the last edit we saw; a reload starts it over.
53let last = '';
54
55const keep = async ($: EngineInterface, text: string): Promise<void> => {
56 const at = await $.clock.now();
57 const list = await read($, stack);
58 await save($, push(list, { text, at }));
59 $.ui.toast(`Stashed: ${preview(text, 40)} · /stash pop`);
60};
61
62const fill = async ($: EngineInterface, entry: Stashed, index: number, isKept: boolean): Promise<void> => {
63 const { text: draft } = await $.prompt.read();
64 await $.prompt.fill({ text: entry.text, mode: draft.trim() === '' ? 'replace' : 'append' });
65 last = entry.text;
66 if (!isKept) {
67 await drop($, index);
68 }
69};
70
71const restore = async ($: EngineInterface, index: number, isKept: boolean): Promise<{ text: string }> => {
72 const list = await read($, stack);
73 const entry = list[index];
74 if (entry === undefined) {
75 const isEmpty = list.length === 0;
76 if (isEmpty) {
77 return { text: 'The stash is empty.' };
78 }
79
80 return { text: `No stash@{${index}}; /stash lists ${list.length}.` };
81 }
82
83 // The command's own run clears the box when it ends, so the fill waits for that.
84 $.clock.after(0, () => void fill($, entry, index, isKept));
85
86 return { text: `${isKept ? 'Applied' : 'Popped'} stash@{${index}}: ${preview(entry.text, 60)}` };
87};
88
89export const register: Register = (on, options) => {
90 isStatusShown = options.showStatus !== false;
91 isAutoStash = options.autoStash === true;
92
93 on('session.start', async ($, e, next) => {
94 await $.command.register({
95 name: 'stash',
96 description: 'Drafts you wiped without sending: list, pop, apply, drop',
97 argumentHint: '[list | pop [N] | apply [N] | drop [N] | clear]',
98 });
99 const key = await storeKey($);
100 const kept = await $.store.get(key);
101 const list = Array.isArray(kept) ? (kept as Stashed[]) : [];
102 await update($, stack, () => list);
103 // Also takes down a line a reload or an earlier version left behind.
104 showCount($, list.length);
105
106 return next(e);
107 });
108
109 on('prompt.edit', async ($, e, next) => {
110 if (!isAutoStash) {
111 return next(e);
112 }
113
114 const gone = vanished(last, e.text);
115 const box = await next(e);
116 const cleared = wiped(e.text, box.text);
117 last = box.text;
118
119 const draft = cleared ?? gone;
120 if (draft !== null) {
121 await keep($, draft);
122 }
123
124 return box;
125 });
126
127 // A sent draft is not a lost one.
128 on('prompt.submit', async ($, e, next) => {
129 last = '';
130 const draft = stashRequest(e.text);
131 if (draft === null) {
132 return next(e);
133 }
134
135 await keep($, draft);
136 return { drop: 'Stashed instead of sent · /stash pop brings it back' };
137 });
138
139 on('command.run', { command: 'stash' }, async ($, e) => {
140 const action = parseArgs(e.args);
141 switch (action.kind) {
142 case 'list':
143 await $.ui.open({ id: PANE, title: 'stash', focus: true });
144 return {};
145 case 'pop':
146 return restore($, action.index, false);
147 case 'apply':
148 return restore($, action.index, true);
149 case 'drop': {
150 const list = await read($, stack);
151 if (list[action.index] === undefined) {
152 return { text: `No stash@{${action.index}}.` };
153 }
154 await drop($, action.index);
155 return { text: `Dropped stash@{${action.index}}.` };
156 }
157 case 'clear':
158 await save($, []);
159 return { text: 'Stash cleared.' };
160 case 'help':
161 return { text: `Unknown: ${action.text}. Use /stash [list | pop [N] | apply [N] | drop [N] | clear].` };
162 }
163 });
164
165 on('ui.render', { component: 'Pane', requestId: PANE }, async ($, e) => {
166 const { Box, Text, Button } = $.ui.resolve(e);
167 const list = await read($, stack);
168 const open = await read($, viewing);
169 const width = Math.max(20, (e.viewport?.columns ?? 80) - 36);
170
171 return (
172 <Box flexDirection="column">
173 {list.length === 0 && <Text dimColor>Nothing stashed. End a message with /stash to keep it here.</Text>}
174 {list.map((entry, i) => {
175 const isOpen = open === i;
176
177 return (
178 <Box key={`s${i}`} flexDirection="column">
179 <Box>
180 <Text dimColor>{`stash@{${i}} `}</Text>
181 <Text>{preview(entry.text, width)} </Text>
182 <Button key={`view${i}`} label={isOpen ? 'Hide' : 'View'} onPress={() => void toggleView($, i)} />
183 <Text> </Text>
184 <Button key={`pop${i}`} label="Pop" onPress={() => void restore($, i, false)} />
185 <Text> </Text>
186 <Button key={`drop${i}`} label="Drop" onPress={() => void drop($, i)} />
187 </Box>
188 {isOpen && (
189 <Box key={`full${i}`} borderStyle="round" borderDimColor paddingX={1} marginBottom={1}>
190 <Text>{entry.text}</Text>
191 </Box>
192 )}
193 </Box>
194 );
195 })}
196 </Box>
197 );
198 });
199};
200hooks/args.ts 33 lines1export type StashAction =
2 | { kind: 'list' }
3 | { kind: 'pop'; index: number }
4 | { kind: 'apply'; index: number }
5 | { kind: 'drop'; index: number }
6 | { kind: 'clear' }
7 | { kind: 'help'; text: string };
8
9const index = (raw: string | undefined): number => {
10 const match = /^(?:stash@\{)?(\d+)\}?$/.exec(raw ?? '');
11 return match === null ? 0 : Number(match[1]);
12};
13
14export const parseArgs = (args: string): StashAction => {
15 const [verb = 'list', arg] = args.trim().split(/\s+/);
16 switch (verb.toLowerCase()) {
17 case '':
18 case 'list':
19 case 'ls':
20 return { kind: 'list' };
21 case 'pop':
22 return { kind: 'pop', index: index(arg) };
23 case 'apply':
24 return { kind: 'apply', index: index(arg) };
25 case 'drop':
26 return { kind: 'drop', index: index(arg) };
27 case 'clear':
28 return { kind: 'clear' };
29 default:
30 return { kind: 'help', text: verb };
31 }
32};
33hooks/capture.ts 47 lines1export const MIN_LENGTH = 15;
2export const MAX_ENTRIES = 50;
3
4// Only as the last word: a message that mentions the command, or quotes the mod's own
5// notice, is sent as written.
6const STASH_SUFFIX = /(^|\s)\/stash\s*$/;
7
8export const stashRequest = (text: string): string | null => {
9 const isRequest = STASH_SUFFIX.test(text);
10 if (!isRequest) {
11 return null;
12 }
13
14 const draft = text.replace(STASH_SUFFIX, '').trim();
15 return draft === '' ? null : draft;
16};
17
18const isWorthKeeping = (draft: string): boolean => draft.trim().length >= MIN_LENGTH;
19
20// The draft to keep when the box went from `before` to `after`: most of a
21// worthwhile draft wiped in one go (Ctrl+U, select-all + delete, a cut).
22export const wiped = (before: string, after: string): string | null =>
23 isWorthKeeping(before) && after.trim().length <= before.trim().length * 0.2 ? before : null;
24
25// The draft to keep when the box no longer holds what it last held, with no
26// edit of ours in between (Ctrl+S, Ctrl+C, a clear the editor did itself).
27// A slash command clears the box when it runs without passing prompt.submit,
28// so one that vanishes was run, not lost.
29export const vanished = (last: string, now: string): string | null => {
30 const isCommand = last.trimStart().startsWith('/');
31 if (isCommand) {
32 return null;
33 }
34
35 return isWorthKeeping(last) && now !== last && !now.includes(last.trim()) ? last : null;
36};
37
38export const push = <T extends { text: string }>(stack: readonly T[], entry: T): T[] => {
39 const isAlreadyNewest = stack[0]?.text === entry.text;
40 if (isAlreadyNewest) {
41 return [...stack];
42 }
43
44 const others = stack.filter((s) => s.text !== entry.text);
45 return [entry, ...others].slice(0, MAX_ENTRIES);
46};
47types/index.d.ts 12 lines1export type Stashed = { text: string; at: number };
2
3declare module 'claude-code' {
4 interface PluginState {
5 'stash-stack': {
6 // Newest first, as git numbers stash@{0}.
7 stack: Stashed[];
8 viewing: number | null;
9 };
10 }
11}
12