← Complete Projects

阅读中文版本

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.

beginner~4 hnodejsclisearchshell

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.

The project is deliberately sized so that every stage ships something you can run: the first phase produces a working command or endpoint, and each later phase adds one capability without forcing a rewrite of what came before. That property — always runnable, never half-migrated — is what makes the tutorial usable as a weekend build rather than a sketch that only works once the final commit lands.

The finished tool carries no hidden services and no hosted dependencies. Everything it needs lives in the repository and a machine with the stated runtime, so a reader can clone, install, run the tests, and trust that the behaviour they see locally is the behaviour the tutorial promised. Where a real deployment needs an external system, that seam is called out explicitly rather than smuggled in through a default.

It is written for a working developer who wants the whole shape of a real tool in one readable pass rather than a tour of a framework. Each phase states the decision it makes and why, then shows the code that encodes it, so the reader can stop after any phase with something that runs and resume later without relearning the whole design. The prose is deliberately honest about trade-offs: where a simpler approach would break at scale, that ceiling is named instead of hidden behind a benchmark.

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.

Set up the workspace before writing any logic. The table below pins every dependency to a version so the behaviour you observe matches the tutorial; floating versions are the most common reason a reader’s output diverges from the text. Install exactly what the table lists, then confirm the toolchain reports the expected version before continuing.

Two configuration choices deserve attention up front. First, the project keeps its runtime entry and its test entry as separate scripts so that running tests never starts a server or writes to a data directory. Second, all tunables are read from one place — a small config module or environment variables — so that nothing depends on the working directory a command happens to be launched from.

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.

The first hard decision is where the boundary between parsing and doing sits. Input arrives as raw text — argv, environment variables, a file on disk — and this phase converts it exactly once into a typed object that the rest of the program trusts. Confining that conversion to a single module means every later layer receives clean, already-validated input and never re-checks a string, which removes a whole class of bugs where two modules quietly disagree about what a flag or a field means. The second decision is failure visibility: invalid input must produce an actionable message and a non-zero exit rather than a stack trace surfacing from three layers down. Getting these two boundaries right early is what lets the remaining phases stay small.

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.

This phase defines the data model that every other module speaks. The choice of shape matters more than it looks: flat records are easy to serialize and diff, nested structures model relationships but complicate equality checks, and identifiers must be stable across edits so that references never silently rebind. The code here stays pure — functions take data and return data, with no clocks, no disk, and no printing — because pure functions are the only kind that can be tested exhaustively without a fixture harness. When the model is right the surrounding code almost writes itself; when it is wrong every later phase pays interest.

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.

The core operation is where the project delivers its value, and the temptation is to cram every option into one function. Resist it. The main path should read as a short, obvious sequence — load, transform, emit — with the branching behaviour factored into small decisions that can be tested on their own. This phase also fixes the error contract: which failures are fatal, which are recoverable, and how a partial result is reported. A tool that can explain what it did and why it stopped is far easier to trust in a pipeline than one that only signals success or silence.

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.

Integration is where the project stops being a toy. Input may arrive in a format an external system produced, output may need to match a contract another system consumes, and the two rarely line up without adjustment. This phase owns the adapters — the small translation functions between the outside world’s shape and the internal model — and it owns the boundary decisions about what to do when the two disagree: coerce, reject, or warn and continue. Keeping adapters thin and explicit means a change to an external format touches one file, and the internal model stays stable as the ecosystem around it moves.

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.

The user-facing surface determines whether the project feels finished. A command-line tool owes its caller predictable stdout, meaningful stderr, and honest exit codes; a service owes a stable request and response schema plus consistent status handling. This phase wires the internal modules to that surface and nothing else — it contains no business logic of its own, only translation between the outside contract and the internal calls. That separation is what lets the same core serve a CLI today and an HTTP endpoint tomorrow without duplication, and it is why the tests for this layer assert process-level behaviour rather than poking at internals.

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.

The final phase turns a working program into a dependable one. Tests are the visible part, but the discipline behind them is deeper: every claim the earlier phases made is pinned by an executable check, and every failure mode the tutorial mentioned has a test that reproduces it before the fix. Edge cases — empty input, malformed records, concurrent invocations, interrupted writes — are enumerated and asserted rather than assumed away. The payoff is a suite that fails loudly and precisely when someone changes a decision, which is exactly the signal a maintainer wants the moment they touch the code.

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.

Design trade-offs

Minimal dependencies over framework convenience. A framework would hand over plumbing for free, but it would also own the interesting decisions this project exists to teach. Keeping the dependency surface small means the lifecycle is visible end to end, the install is instant, and the tool runs anywhere the stated runtime does. The cost is owning the edge cases yourself — which is precisely why each of them gets a test.

Explicit contracts over clever conventions. Where a convention would infer behaviour from a filename or a folder, the project asks for a named export or a declared field instead. It reads as slightly more verbose and is far easier to review: a reader sees the intent on the line that owns it rather than reconstructing it from directory layout. Debugging also gets cheaper, because the failure points at the declaration instead of the convention that silently produced it.

Deterministic behaviour over convenience defaults. Anything that could vary between machines — time, ordering, encoding, locale — is injected or pinned rather than read from the ambient environment. That choice costs a little ceremony and buys reproducible output, which is the property that makes the output diffable in review and the tests trustworthy across the team’s machines.

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.

Tests are organised by layer so a failure points at the decision that broke. Pure unit tests cover the transformation logic without touching disk or network and run in milliseconds; integration tests exercise the real entry point against a temporary working directory and assert the observable contract; a small end-to-end tier proves the whole path produces the documented output. Keeping the fast tiers large and the slow tier thin is what lets the suite run on every save while still catching integration mistakes.

Every test asserts a specific promise from the phases above rather than merely covering lines. A passing suite therefore means the tool kept its word: filters compose, identifiers stay stable, failures exit non-zero with a readable message, and the output matches the format a downstream consumer expects. When someone changes a contract the matching test fails with an exact diff, turning a silent regression into a line in the pull request.

Running the suite in CI is the last line of defence, but the same command runs locally before every commit, which is what keeps the feedback loop short. A green run means the documented behaviour still holds; a red run names the exact promise that broke. Treat the suite as executable documentation and it stays current, because a stale assertion fails the moment the code and the text disagree.

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.

The structure leaves obvious seams for growth. Each capability that could be added — a new output format, an extra filter, an interactive mode — lands in one layer because the boundaries between parsing, logic, and presentation are already drawn. Extensions that would break the current design are equally visible: the moment a feature needs cross-process coordination or a database, the storage interface is where the change belongs, and its tests tell you immediately whether the surrounding code survives the swap.