Build a Searchable Command History CLI
A tool that parses your shell history file, indexes commands by frequency and recency, and ranks fuzzy matches so the command you want is the first result.
Project goal
Build a CLI that parses a shell history file, deduplicates commands while keeping recency, ranks fuzzy and substring matches, and prints the best candidates to rerun.
Final features
- Parse history files from bash and zsh formats tolerating timestamps.
- Deduplicate commands while tracking the most recent occurrence.
- Rank matches by recency, frequency, and match quality.
- Support fuzzy subsequence matching in addition to substrings.
- Print the ranked command so it can be captured and rerun.
Architecture
A parser reads a history file into command records; a deduplicator collapses repeats while tracking recency and frequency; a scorer ranks candidates by a weighted combination of match type, frequency, and recency; and a CLI prints the top result. Scoring is a pure function over records and a query.
Prerequisites
- Node.js 20 LTS or later
- Familiarity with shell history formats
- Basic understanding of ranking and scoring
Dependencies
- node >=20 — File system access and the CLI entry point
- vitest ^2.1.0 — Parser and scorer tests
Directory structure
hist-search/
├── src/
│ ├── parse.js
│ ├── dedupe.js
│ ├── score.js
│ └── cli.js
├── test/
│ ├── parse.test.js
│ └── score.test.js
└── package.json
Phases
1. History parser
Read a history file and parse both timestamped and plain command lines into records.
Files: src/parse.js
export function parseHistory(text) {
const lines = text.split(/\r?\n/);
const records = [];
let pendingTime = null;
for (const line of lines) {
if (line.startsWith(': ')) {
const parts = line.slice(2).split(':');
pendingTime = Number(parts[0]) * 1000;
if (parts[2]) records.push({ command: parts[2].trim(), at: pendingTime });
} else if (line.trim()) {
records.push({ command: line.trim(), at: pendingTime });
pendingTime = null;
}
}
return records.filter((r) => r.command && !r.command.startsWith('#'));
}Expected behavior: A zsh extended line with a timestamp produces one record with that time, and a plain bash line produces one record with a null time.
2. Deduplication
Collapse repeated commands into one record, tracking occurrence count and the most recent time.
Files: src/dedupe.js
export function dedupe(records) {
const map = new Map();
records.forEach((record, index) => {
const key = record.command;
const existing = map.get(key);
if (existing) {
existing.count += 1;
if ((record.at ?? 0) >= (existing.lastUsed ?? 0)) existing.lastUsed = record.at ?? existing.lastUsed;
existing.lastIndex = index;
} else {
map.set(key, { command: key, count: 1, lastUsed: record.at ?? null, lastIndex: index });
}
});
return [...map.values()];
}Expected behavior: Three uses of one command become a single record with count three, and the latest timestamp wins.
3. Fuzzy matching
Implement subsequence matching so a query of scattered letters can still match a command.
Files: src/fuzzy.js
export function isSubsequence(query, text) {
let i = 0;
for (const ch of text) {
if (ch === query[i]) i++;
if (i === query.length) return true;
}
return i === query.length;
}
export function matchKind(query, text) {
if (text === query) return 'exact';
if (text.startsWith(query)) return 'prefix';
if (text.includes(query)) return 'substring';
if (isSubsequence(query, text)) return 'fuzzy';
return 'none';
}Expected behavior: A scattered query like gco matches git checkout --orphan as a subsequence, and an unrelated query returns none.
4. Scorer
Combine match kind, frequency, and recency into one score and rank the candidates.
Files: src/score.js
import { matchKind } from './fuzzy.js';
const BASE = { exact: 100, prefix: 60, substring: 40, fuzzy: 15, none: 0 };
export function score(record, query, now = Date.now()) {
const kind = matchKind(query.toLowerCase(), record.command.toLowerCase());
if (kind === 'none') return null;
const frequency = Math.min(20, record.count * 2);
const ageDays = record.lastUsed ? (now - record.lastUsed) / 86_400_000 : 30;
const recency = Math.max(0, 10 - ageDays);
return { command: record.command, kind, score: BASE[kind] + frequency + recency };
}
export function rank(records, query, now = Date.now()) {
return records.map((r) => score(r, query, now)).filter(Boolean)
.sort((a, b) => b.score - a.score || a.command.localeCompare(b.command));
}Expected behavior: An exact match outranks a prefix and a prefix outranks a substring; a command with no match yields null and is filtered out.
5. CLI
Read the history file, deduplicate, rank against the query, and print the top results or just the best.
Files: src/cli.js
import { readFile } from 'node:fs/promises';
import { parseHistory } from './parse.js';
import { dedupe } from './dedupe.js';
import { rank } from './score.js';
const args = parse(process.argv.slice(2));
const path = process.env.HISTFILE ?? args.file;
if (!path) { console.error('usage: hist <query> [--file history] [--top 1]'); process.exit(1); }
const records = dedupe(parseHistory(await readFile(path, 'utf8')));
const results = rank(records, args.query ?? '', Date.now());
const top = Number(args.top ?? 10);
if (args.best) {
if (!results.length) { console.error('no match'); process.exit(1); }
console.log(results[0].command);
} else {
for (const r of results.slice(0, top)) console.log(String(Math.round(r.score)).padStart(3) + ' ' + r.command);
}Expected behavior: Best mode prints exactly the top command for script capture, and list mode prints ranked lines with scores.
6. Tests and hardening
Cover parsing, deduplication, and ranking, and fix any failure the tests reveal.
Files: test/score.test.js
import { rank } from '../src/score.js';
const now = Date.parse('2026-10-22T00:00:00Z');
const records = [
{ command: 'git checkout main', count: 1, lastUsed: now },
{ command: 'git commit -m x', count: 1, lastUsed: now },
{ command: 'grep -r checkout .', count: 3, lastUsed: now },
];
test('prefix outranks substring and fuzzy', () => {
const ranked = rank(records, 'git ch', now);
expect(ranked[0].command).toBe('git checkout main');
});
test('exact match wins outright', () => {
const ranked = rank(records, 'git commit -m x', now);
expect(ranked[0].kind).toBe('exact');
});Expected behavior: The ranking suite passes with the prefix case first and the exact case scoring highest, and no-match commands are excluded.
Testing
Run npx vitest. Parser tests cover zsh extended history lines with timestamps and plain bash lines. Dedupe tests assert that repeats collapse into one record with the latest timestamp and an accurate count. Scorer tests assert that an exact match outranks a prefix and that a fuzzy-only match outranks nothing at all.
Troubleshooting
Nothing matches a command you know is in history
Check the query against the fuzzy path and confirm the history file is the one your shell writes; a different HISTFILE is the usual reason the tool sees an incomplete history.
Common commands drown out specific ones
Weight match kind above raw frequency and cap the frequency term; otherwise the most-run command always wins regardless of the query.
Duplicate entries inflate frequency counts
Deduplicate before scoring and count occurrences on the collapsed record; counting raw lines double-counts the same command.
Deployment
Bind the best-match mode to a shell key binding that inserts the result at the prompt, and keep the history file path configurable for different shells. The tool only reads history, so it is safe to run frequently.
Known limitations
- It reads a snapshot of the history file and does not see the current session until the shell flushes it.
- Timestamps are missing from plain bash history, so recency falls back to last-seen order.
Project overview
Your shell history already contains the command you want; the problem is recall. This tool parses the history file, deduplicates repeats, and ranks candidates so the most likely command is first.
The scorer combines match quality with recency and frequency, which is what makes the ranking feel right: a command run many times recently beats one that merely appears in the file.
Scoring weights exact and prefix matches above fuzzy subsequences and folds in recency and frequency:
export function score(record, query, now) {
const cmd = record.command.toLowerCase();
const q = query.toLowerCase();
let base = 0;
if (cmd === q) base = 100;
else if (cmd.startsWith(q)) base = 60;
else if (cmd.includes(q)) base = 40;
else if (isSubsequence(q, cmd)) base = 15;
else return 0;
const frequency = Math.min(20, record.count * 2);
const ageDays = (now - record.lastUsed) / 86_400_000;
const recency = Math.max(0, 10 - ageDays);
return base + frequency + recency;
}
Because the score is a plain number, the ranking is deterministic and easy to assert in a test rather than a black box.
Setup
Zero runtime dependencies; the whole tool is parsing and arithmetic over a text file.
| Dependency | Version | Why it is here |
|---|---|---|
| node | >=20 | File system access and the CLI entry point |
| vitest | ^2.1.0 | Parser and scorer tests |
Create the project and install:
mkdir hist-search && cd hist-search
npm init -y
npm install -D vitest@^2.1.0
Configure the CLI and tests in package.json:
{
"name": "hist-search",
"type": "module",
"scripts": {
"hist": "node src/cli.js",
"test": "vitest run"
}
}
The history file path comes from an environment variable so tests use a small fixture instead of your real shell history.
Phase 1: History parser
Read a history file and parse both timestamped and plain command lines into records. The scope is chosen so the project is genuinely finishable, yet every phase has a real decision to make rather than busywork.
export function parseHistory(text) {
const lines = text.split(/\r?\n/);
const records = [];
let pendingTime = null;
for (const line of lines) {
if (line.startsWith(': ')) {
const parts = line.slice(2).split(':');
pendingTime = Number(parts[0]) * 1000;
if (parts[2]) records.push({ command: parts[2].trim(), at: pendingTime });
} else if (line.trim()) {
records.push({ command: line.trim(), at: pendingTime });
pendingTime = null;
}
}
return records.filter((r) => r.command && !r.command.startsWith('#'));
}
Acceptance for this phase: A zsh extended line with a timestamp produces one record with that time, and a plain bash line produces one record with a null time.
Phase 2: Deduplication
Collapse repeated commands into one record, tracking occurrence count and the most recent time. The scope is chosen so the project is genuinely finishable, yet every phase has a real decision to make rather than busywork.
export function dedupe(records) {
const map = new Map();
records.forEach((record, index) => {
const key = record.command;
const existing = map.get(key);
if (existing) {
existing.count += 1;
if ((record.at ?? 0) >= (existing.lastUsed ?? 0)) existing.lastUsed = record.at ?? existing.lastUsed;
existing.lastIndex = index;
} else {
map.set(key, { command: key, count: 1, lastUsed: record.at ?? null, lastIndex: index });
}
});
return [...map.values()];
}
Acceptance for this phase: Three uses of one command become a single record with count three, and the latest timestamp wins.
Phase 3: Fuzzy matching
Implement subsequence matching so a query of scattered letters can still match a command. The scope is chosen so the project is genuinely finishable, yet every phase has a real decision to make rather than busywork.
export function isSubsequence(query, text) {
let i = 0;
for (const ch of text) {
if (ch === query[i]) i++;
if (i === query.length) return true;
}
return i === query.length;
}
export function matchKind(query, text) {
if (text === query) return 'exact';
if (text.startsWith(query)) return 'prefix';
if (text.includes(query)) return 'substring';
if (isSubsequence(query, text)) return 'fuzzy';
return 'none';
}
Acceptance for this phase: A scattered query like gco matches git checkout –orphan as a subsequence, and an unrelated query returns none.
Phase 4: Scorer
Combine match kind, frequency, and recency into one score and rank the candidates. The scope is chosen so the project is genuinely finishable, yet every phase has a real decision to make rather than busywork.
import { matchKind } from './fuzzy.js';
const BASE = { exact: 100, prefix: 60, substring: 40, fuzzy: 15, none: 0 };
export function score(record, query, now = Date.now()) {
const kind = matchKind(query.toLowerCase(), record.command.toLowerCase());
if (kind === 'none') return null;
const frequency = Math.min(20, record.count * 2);
const ageDays = record.lastUsed ? (now - record.lastUsed) / 86_400_000 : 30;
const recency = Math.max(0, 10 - ageDays);
return { command: record.command, kind, score: BASE[kind] + frequency + recency };
}
export function rank(records, query, now = Date.now()) {
return records.map((r) => score(r, query, now)).filter(Boolean)
.sort((a, b) => b.score - a.score || a.command.localeCompare(b.command));
}
Acceptance for this phase: An exact match outranks a prefix and a prefix outranks a substring; a command with no match yields null and is filtered out.
Phase 5: CLI
Read the history file, deduplicate, rank against the query, and print the top results or just the best. The scope is chosen so the project is genuinely finishable, yet every phase has a real decision to make rather than busywork.
import { readFile } from 'node:fs/promises';
import { parseHistory } from './parse.js';
import { dedupe } from './dedupe.js';
import { rank } from './score.js';
const args = parse(process.argv.slice(2));
const path = process.env.HISTFILE ?? args.file;
if (!path) { console.error('usage: hist <query> [--file history] [--top 1]'); process.exit(1); }
const records = dedupe(parseHistory(await readFile(path, 'utf8')));
const results = rank(records, args.query ?? '', Date.now());
const top = Number(args.top ?? 10);
if (args.best) {
if (!results.length) { console.error('no match'); process.exit(1); }
console.log(results[0].command);
} else {
for (const r of results.slice(0, top)) console.log(String(Math.round(r.score)).padStart(3) + ' ' + r.command);
}
Acceptance for this phase: Best mode prints exactly the top command for script capture, and list mode prints ranked lines with scores.
Phase 6: Tests and hardening
Cover parsing, deduplication, and ranking, and fix any failure the tests reveal. The scope is chosen so the project is genuinely finishable, yet every phase has a real decision to make rather than busywork.
import { rank } from '../src/score.js';
const now = Date.parse('2026-10-22T00:00:00Z');
const records = [
{ command: 'git checkout main', count: 1, lastUsed: now },
{ command: 'git commit -m x', count: 1, lastUsed: now },
{ command: 'grep -r checkout .', count: 3, lastUsed: now },
];
test('prefix outranks substring and fuzzy', () => {
const ranked = rank(records, 'git ch', now);
expect(ranked[0].command).toBe('git checkout main');
});
test('exact match wins outright', () => {
const ranked = rank(records, 'git commit -m x', now);
expect(ranked[0].kind).toBe('exact');
});
Acceptance for this phase: The ranking suite passes with the prefix case first and the exact case scoring highest, and no-match commands are excluded.
Testing strategy
Run npx vitest. Parser tests cover zsh extended history lines with timestamps and plain bash lines. Dedupe tests assert that repeats collapse into one record with the latest timestamp and an accurate count. Scorer tests assert that an exact match outranks a prefix and that a fuzzy-only match outranks nothing at all.
The ranking test is the point of the tool: a search that returns the right command second is little better than not finding it, so ordering is asserted explicitly.
Extending the project
Frequency by directory, per-host history, and integration with the shell key binding are additions around the parser and scorer. Because scoring is pure, a new signal is one more term rather than a redesign.