Birbla

Login
    Writing simple tab-completions for Bash and Zsh (mill-build.org)
    235 points by lihaoyi - 23 hours ago

  • I wrote this, hope everyone finds it as interesting reading this as I did figuring this out for the first time!
    by lihaoyi - 22 hours ago
  • Isn't there a standard flag which programs can implement to avoid writing this bash script?

    Ideally this could all be part of a library such as argparse for typical cases, right?

    by oezi - 22 hours ago
  • Do many people do bash on osx or zsh on Linux, and would this make much of a difference?
    by bravesoul2 - 22 hours ago
  • Here's another tutorial for creating zsh completers using the built-in functions: https://github.com/vapniks/zsh-completions/blob/master/zsh-c...
    by vcdimension - 22 hours ago
  • Shell syntax is the exact reason why we've needed LLMs in the first place.
    by wiseowise - 22 hours ago
  • With fish, if the program you're interested in hasn't betrayed the decades-old tradition of shipping man pages, it's often as simple as running `fish_update_completions`.

    It parses all man pages on your system and generates completion files for you. By default, they go into ~/.cache/fish/generated_completions/*

    If the man page was written poorly/is missing, you can always write your own completion (and hopefully send it upstream). fish uses such a simple format that I don't think there's any need for tutorials save the official doc:

    https://fishshell.com/docs/current/completions.html

    For example, here's an excerpt from curl

      complete --command curl --short-option 'L' --long-option 'location' --description 'Follow redirects' 
      complete --command curl --short-option 'O' --long-option 'remote-name' --description 'Write output to file named as remote file'
    by homebrewer - 22 hours ago
  • Why doesn't someone (not me) just build a basic DSL and a transpiler that does this?
    by camdroidw - 22 hours ago
  • I feel that the ergonomics of bash completion took a hit as the configurations got “smarter” and “helpfully” started blocking file or directory name completion if it thinks it wouldn’t be appropriate to have a file name at the current cursor position. Instead of blocking, the default should always be to fall back to filename completion.

    Sometimes I’m close to disabling/uninstalling all completion scripts out of irritation as decades of muscle memory are frustrated by this behavior.

    It’s like that bad/annoying UX with text fields where the UI is constantly fighting against you in order prevent you from producing “illegal” intermediate input - e.g. let me paste the clipboard here goddammit - I know what I’m doing - I’ll correct it.

    by derriz - 22 hours ago
  • Here's zsh snippet I've came up with for my own simple functions. I'm using it as a base for other completions. In this example, function `set-java-home zulu-21` sets JAVA_HOME to `~/apps/java/zulu-21`. Here's `_set-java-home`:

        #compdef set-java-home
        
        local -a versions=(~/apps/java/*(:t))
        _describe 'version' versions
    
    So basically almost a one-liner (but couldn't do it really one-liner, unfortunately).
    by vbezhenar - 21 hours ago
  • JSON fields autocomplete right in bash/zsh: https://fx.wtf/install#autocomplete
    by medv - 20 hours ago
  • I wish tcsh would get more love.
    by xenophonf - 20 hours ago
  • I’m not familiar with `_gnu_generic`, but it sounds like a handy shortcut for basic completions without writing a full script. Does it work with commands that only have `--help` but no man pages?
    by harimurti - 20 hours ago
  • Basic completion in ksh is as easy as defining an array. From https://man.openbsd.org/ksh :

    Custom completions may be configured by creating an array named ‘complete_command’, optionally suffixed with an argument number to complete only for a single argument. So defining an array named ‘complete_kill’ provides possible completions for any argument to the kill(1) command, but ‘complete_kill_1’ only completes the first argument. For example, the following command makes ksh offer a selection of signal names for the first argument to kill(1):

        set -A complete_kill_1 -- -9 -HUP -INFO -KILL -TERM
    by sebtron - 19 hours ago
  • I did something similar to this for tab-completing server names for use with ssh. I went a step further and allowed pattern matching based on the server being qa/prod and on locale. so for instance you could type `ssh co prod <tab>` and it would tab-complete / suggest any servers that were production and located in the Denver datacenter (co is the state abbrev for Colorado, for non-US readers).

    Unfortunately my work doesn't allow me to share code, but essentially I remapped ssh to a bash script that maintains an environment variable containing the args (you must do this because each <tab> press is an independent invocation of the script. Then you run into persistence problems, so I added a call to compute elapsed seconds so that it flushes the state variable after a 10s timeout).

    The bash script then forwards the args to a python script that reads a JSON file and figures out which params (such as 'co' or 'qa') map to which hostnames. It also matches against partial hostnames, so when you see this after tab

    qa-server-co1 qa-server-co2 pr-server-co3

    you only need to add '3' to the list of args to narrow it down to 1 match, then hit <enter> to ssh to that host.

    by gosub100 - 14 hours ago
  • I've started using jdx's usage[1] for my clis. It integrates neatly into clap, and can be used stand alone in scripts. It can generate completions, argparse, manpages, and more

    I'm still on the fence if replacing the argparse blocks in my fish scripts is worth the hassle, but against things like old school optparse, it's far better

    [1]: https://usage.jdx.dev/

    by paradox460 - 14 hours ago
  • Since auto text completion is the primary task of an LLM, I have seen GitHub copilot do that very well on both bash and zsh within vscode terminal!
    by vismit2000 - 4 hours ago

© 2025 Birbla.com, a Hacker News reader · Content · Terms · Privacy · Support