Tab completion after colon

so.nwalsh.com · 2026-08-20 · by Long Nguyen ·generic
Tab completion is great. But it doesn’t work by default after a colon. Unless you do this.

Tab completion after colon

Since long before my time, the Saxon products have used a colon to delimit command line arguments from their values. For example, you specify the source document `mydoc.xml` with the command line argument `-s:mydoc.xml`.

Because Saxon does it that way, and because I’d like to hold onto whatever tattered shreds of sanity I have left, I also do it that way in XML Calabash and NineML.

That’s fine, except that it breaks tab completion.

If you type `-s my<TAB>` on the command line, as a program argument, most shells will do completion of some sort on the `my` part. If the only file that begins with `my` in the directory is `mydoc.xml`, then tab completion will fill it in for you. (If more than one file begins with `my`, you might get the first one, or completion up to the point where they differ, or a menu, it depends and I don’t know off the top of my head on what.)

Unfortunately, if you type `-s:my<TAB>` on the command line, nothing happens.

I was recently asked if I could tweak the command line parsing on NineML so that tab completion would work. I probably could, but this was my impetus to finally investigate if completion after a colon was possible, and if so, how.

For the shell I use, zsh, the answer is remarkably simple. Just add these lines to your `~/.zshrc` file:

function aftercolon() {
  compset -P '*:'
  compset -S ':*'
  _default -r '\-\n\t /:' "$@"
}

autoload -Uz compinit compinit compdef aftercolon coffeepot saxon xmlcalabash

Replace “coffeepot”, “saxon”, and “xmlcalabash” with whatever shell scripts or aliases you use to run those commands (and add any others that you’d like to have completion-after-colon on).

I tried to work out how to do it with other shells. Anecdotal evidence suggersts that it’s possible bash, but I couldn’t work out exactly how. I’d be surprised if it isn’t also possible in other Unix shells. I haven’t a clue about PowerShell.

If anyone sends me the recipes, I’ll post em.

so.nwalsh.com · 4 images · 21 links

Comments (0)

No comments yet