$ emrebener
home personal-projects vimgym

vimgym

technologies: python, neovim, lua, lazyvim, which-key.nvim, toml, tmux, pytest, uv repository: closed source
published: updated: type: pet

vimgym runs short Vim practice sessions inside my real Neovim, with my own config. It opens the editor on a small piece of text and a goal. I make the edit, the exercise ends by itself, and the next one loads. It remembers how each one went and brings back what’s fading, so techniques I nail disappear for weeks and the ones I fumble stay close.

It’s about 1,600 lines of standard-library Python and 1,000 lines of Lua, with 50 exercises. The hard part wasn’t the exercises. It was running someone’s real editor configuration, mine, over and over without leaving anything behind in it. It was built spec-first with Claude Code.

1. What a session looks like

One Neovim runs for the whole session. A strip at the top shows progress, how the last exercise went, and the current goal, with my text on the left and the target on the right. The screenshots here were taken in a stock Neovim with no config, which is what the test suite uses.

Practice session

The exercise ends the moment the text matches the target, and any way of getting there counts. Some exercises have no target: a word is highlighted and the goal is to put the cursor on its first character. F1 shows the taught solution as steps, each with a plain-words explanation, then resets the exercise so I can type it. F2 drops an exercise for good.

Solution steps

When Neovim closes, the terminal shows a summary of the session: the outcome of each exercise, the keys I typed and the time taken.

Session summary

It was built for one setup, Neovim with LazyVim on Linux, and one person. The design names its own failure condition as being “too much work”, so it’s meant to stay small: sessions of a few minutes, no streaks and no backlog.

2. Scheduling by outcome

Each finished exercise gets one of four outcomes, and the outcome alone decides when it returns.

OutcomeMeaningComes back
CleanDone in at most 1.5 times the keys of the taught solutionIn 14 days the first time, then after growing gaps of up to 180 days
LongDone, with more keys than thatIn 3 to 14 days
With solutionDone after pressing F1Tomorrow
DroppedRemoved with F2Never, until restored

The whole rule is one pure function:

def next_interval(p: Progress, outcome: str, today: date) -> int:
    """Days until the exercise comes back after an attempt with this outcome."""
    first = p.interval is None or p.last_attempt is None
    if outcome == CLEAN:
        if first:
            return FIRST_CLEAN
        days = (today - p.last_attempt).days
        return min(MAX_INTERVAL, max(p.interval, round(GROWTH * days)))
    if outcome == LONG:
        if first:
            return FIRST_LONG
        return min(LONG_MAX, max(LONG_MIN, p.interval // 2))
    if outcome == WITH_SOLUTION:
        return WITH_SOLUTION_DAYS
    raise ValueError(f"not an attempt outcome: {outcome!r}")

A clean attempt grows the gap from the time that actually passed, not from the scheduled interval, so an exercise I still get right after a long break jumps further out. A session then fills in order: exercises that are due (the ones I needed the solution for first), then ones I’ve never seen, then ones not due yet. Coming back after weeks away gives me the handful most worth doing and not a pile.

3. Running the real editor without leaving a trace

A session is my actual Neovim with my actual config, so that the habits transfer to the editor I use. That means every session could damage real state: the system clipboard, command and search history, saved editor sessions, the config directory. The rule is that none of those may be touched, and each protection exists because the failure happened or nearly did.

Neovim is only ever started through one function, which wraps the user’s config in guards on both sides:

def nvim_start(module: str, function: str) -> list[str]:
    """The arguments that protect the editor, put vimgym's runtime on 'runtimepath' and start
    `module` (with `-c`, after the config: lazy.nvim resets 'runtimepath' while it starts)."""
    return ["--cmd", EXIT_GUARD,
            "-c", PROTECT_FIRST,
            "-c", f"set rtp^={nvim_runtime()}",
            "-c", _START.format(module=module, function=function, status=FAILED_STATUS)]
  • An exit guard goes in before the config loads. If Neovim is killed while the config is still loading, the guard wipes every named buffer and stops the session-saving plugin before its save-on-exit hook can run.
  • The first command after the config clears the clipboard setting and stops the session saver, so a crash before vimgym’s own code runs leaves neither active.
  • The start fails closed. If vimgym’s module doesn’t load, Neovim writes the reason to a file, wipes its buffers and exits with a dedicated status instead of leaving me in a half-protected editor.
  • Protection is applied a second time later in startup, because LazyVim restores the system-clipboard setting at that point. Sessions and replays refuse to accept keys while a check reports that anything is still unprotected.

One detail took a real failure to find. Neovim is given a placeholder file argument so LazyVim’s dashboard stays away, and that file must not exist on disk. An existing file fires the event that loads the session-saving plugin before any vimgym code runs, and a test killed in that window once wrote junk into my saved sessions.

Two things any start of my Neovim does still happen: it appends to Neovim’s log files, and the plugin manager may run its periodic update check.

4. Counting keys under which-key

An outcome depends on how many keys I typed, so the count has to be right. Neovim’s on_key callback sees each key as typed, before mappings, which is the right source. The complication is which-key, the plugin that shows available key bindings after a prefix such as g or the leader. After a prefix it reads the following keys itself and then feeds them back as if typed, prefix and count included, so on_key sees them twice.

vimgym wraps the one which-key function that does the feeding and notes what it feeds, so the callback can pass over those copies:

local function on_key(_, typed)
  if not S.active or typed == nil or typed == '' then
    return
  end
  local mode = vim.api.nvim_get_mode().mode
  for _, token in ipairs(format.tokens(typed)) do
    if S.refed[1] == token then
      table.remove(S.refed, 1) -- which-key's copy of a key already counted when it was typed
    elseif is_counted(token, mode) then
      S.refed = {} -- a copy that never came must not swallow a later key
      S.started_at = S.started_at or vim.uv.hrtime() -- the timer starts at the first counted key
      S.keys[#S.keys + 1] = token
    end
  end
end

which-key itself is left working. Without it a slow leader key would time out, which isn’t how my editor behaves.

The hook reaches into the plugin’s internals, so a plugin update can break it. When it can’t be installed, the strip and the session summary say that key counts may be too high, and a set of tests that run against my real config is there to run after every plugin update.

5. Exercises as data, verified by replay

An exercise is one TOML file: a goal, the start text, the target or the spot to land on, and the taught solution as steps.

title = "Put the same text in front of several lines with :s"
label = "core"
kind = "edit"
goal = 'Turn the five retro topics into a list: put "- " in front of each.'
cursor = [3, 1]
# ... then the start text, the target text, and one solution table per step

Each solution step is a pair: the keys to type, such as V, and what they do in plain words, such as “start selecting whole lines: the current line is highlighted”. Each exercise teaches one technique, and the explanations are in plain words because I found compact key notation hard to read.

A taught solution that doesn’t work is worse than no exercise, so vimgym check replays every solution and fails if the result isn’t the target. It replays in headless Neovim under my full config, and for exercises labeled core it also replays in plain Vim with no config. A solution that only works under LazyVim can’t keep a core label.

The 50 exercises cover 13 topics, from line and word motions through text objects, visual mode, substitution and macros. Adding one is a Claude Code skill: I describe what tripped me up, and the skill writes the file and verifies it with the same check.

6. Tests that can’t reach the real editor

The same risk applies to the test suite, so its default is to be harmless. A fixture that applies to every test gives it a temporary state directory and a factory-default Neovim, which makes the suite safe to run at any time. Tests that start my own editor are marked and run only when an environment variable asks for them.

The end-to-end tests drive a real Neovim through tmux, on a tmux server of their own. Their environment is passed inside the command line itself. tmux ignores some variables given through its own option, and that’s how a test once started my real config by accident.

There are about 200 Python tests, plus Lua unit tests for the editor side that run under Neovim with no config. The Python side has no runtime dependencies beyond the standard library, and pytest is the only development dependency.