Skip to content

Instantly share code, notes, and snippets.

@icholy
Last active January 9, 2022 19:19
Show Gist options
  • Star 23 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save icholy/5314423 to your computer and use it in GitHub Desktop.
Save icholy/5314423 to your computer and use it in GitHub Desktop.
Zsh Tab Completion for Golang

Zsh Tab Completion for Golang

This is pretty specific to my setup but the idea can be adapted to work with pretty much anything.

Go has a flag package which makes parsing command line arguments really easy.

package main

import "flag"
import "fmt"

var param1 = flag.String("param1", "default-value", "a description for param1")
var param2 = flag.Int64("param2", 123, "a description for param2")

func init() {
  flag.Parse()
}

func main() {
  fmt.Printf("param1: %s, param2 %d", *param1, *param2)
}

Zsh lets you define custom tab completion scripts. Here's one for the example program above. foo is what triggers the completion. So the program would have to be called foo and be somewhere in your PATH.

#compdef foo

_arguments -s \
  '-param1[a description for param1]' \
  '-param2[a description for param2]'

This is cool, but typing these out is a pain so let's automate it! The flag package has a VisitAll method which lets you iterate over all defined flags. Using that we can easily create the completion script.

func PrintCompletions(name string) {
  var cmpl []string
  flag.VisitAll(func(f *flag.Flag) {
    cmpl = append(
      cmpl, fmt.Sprintf("\t'-%s[%s]' \\\n", f.Name, f.Usage))
  })
  fmt.Printf("#compdef %s\n\n_arguments -s \\\n%s\n\n",
		name, strings.Join(cmpl, " "))
}

Here's a complete example go playground

package main

import (
  "flag"
  "fmt"
  "strings"
)

var (
  param1   = flag.String("param1", "foo", "this is param1, it's useless")
  param2   = flag.String("param2", "foo", "this is param2, even more useless")
  complete = flag.Bool("complete", true, "output zsh completions")
)

func init() {
  flag.Parse()
}

func PrintCompletions(name string) {
  var cmpl []string
  flag.VisitAll(func(f *flag.Flag) {
    cmpl = append(
      cmpl, fmt.Sprintf("\t'-%s[%s]' \\\n", f.Name, f.Usage))
  })
  fmt.Print(
    fmt.Sprintf("#compdef %s\n\n_arguments -s \\\n%s\n\n",
      name, strings.Join(cmpl, " ")),
  )
}

func main() {
  if *complete {
    PrintCompletions("main")
    return
  }
}

To use it, just pipe the output into a new completion script

$ myapp -complete > ~/.zsh/completion/_myapp

If you need more info about zsh completion scripts, check out this article.

@icholy
Copy link
Author

icholy commented Apr 5, 2013

Since this ended up being useless, here's a zsh completion for godoc https://gist.github.com/icholy/5320428

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment