Hacker Newsnew | past | comments | ask | show | jobs | submitlogin
ZSH Completions How To (github.com/zsh-users)
89 points by ossusermivami on Dec 4, 2021 | hide | past | favorite | 20 comments


I hate how complex it to write a decent zsh completion. It is not something you can realistic do in a weekend.

You can implement a simple one without much effort. But doing anything a bit more interesting need to dig into zle, and there is only a few resources available online.


Fish has sane defaults. I don’t think using shell scripts is a great idea. The creator of oil shell pops up pretty often, he has some default scripts that might be useful for some. Also I should add you can use it interactively in bash. https://wiki.archlinux.org/title/fish#Setting_fish_as_intera...


If you want to play around with writing more 'interesting' completions, check out Fig (https://github.com/withfig/autocomplete).

You can write completions declaratively and use TypeScript to generate dynamic suggestions.

Disclosure: I'm one of the founders.


Looks neat.

I've though about using tree-sitter DSL to generate zsh completion.

Your project seems to be a better candidate for this. Tree-sitter DSL, with some extra annotations, could be used to generate the specification. With the added bonus of being a format that can provide syntax highlight on text editors (in commands in sh files).

If I not mistaken, tree-sitter also uses TS.

I only gave a quick look at fig (only use HN on mobile). Will have a better look when at the desktop.


I have an edge-case for zsh completion that I'd love to know if there's a way to implement.

I've declared a function `k` which tests if I'm currently on a `kubie` [0] shell. If it isn't, it runs `kubie ctx`; if it is, it runs `kubectl $@`.

I never write kubectl directly but rather run this `k` function. This way I'm always in the context I want and don't get surprises since I'm forgetful.

I'd like for this function to complete like it was kubectl itself.

So far I've tried:

    compdef k=kubectl
    compdef _kubectl k
    compdef '_bash_complete -o default -F __start_kubectl' k
    
The last one is from the below, although I don't really understand what it's doing:

    $  grep kubectl ~/.zcompdump
    'kubectl' '_bash_complete -o default -F __start_kubectl'
    autoload -Uz _bat _brew _git _j _kubectl \
[0] https://github.com/sbstp/kubie


I’ve spend hours trying to get it to work as well as fish, but even the installed configs I miss one thing or another and autocomplete is still missing features. It’s a mess and really slow.

Without color, and trying to get NSH working with autocomplete (with bash autocomplete) it still didn’t work in NSH. I if it wasn’t for fish I’d still be using GUI, and if it was this and bash, I would have never felt comfortable configurating stuff I didn’t understand.


Why not generate them from the manpages [1] or from the output of the --help option [2]

[1] https://github.com/nevesnunes/sh-manpage-completions

[2] https://github.com/RobSis/zsh-completion-generator


it's not always ideal, only you have a very intelligent parser understanding the context of the manpage.. i.e: "-u: read a http url" the parser would need to understand to propose an https:// (and perhaps the hosts on system) prefix after hitting the TAB key after the -u flag. if not it would do just the completion of switches and argument which is maybe good enough but not great...

(it's a simple example that doesn't add a lot of usefulness, but there is other usecase like the -n of kubectl, it is very useful to have the completion of kubectl namespaces in there after -n)


Love zsh + OhMyZsh. But one thing I repeatedly wish it had but maybe I just need to read the manual: when I type the start of a command, says “python”, I want to search for a previous command that starts with “python” and contains the string “data2134”. I tried Ctl-R but that doesn’t seem to be it.


I have this with fzf + bash, and assume it would work in zsh too.


Fzf is what you're looking for.


I’ve been loving FIG terminal! The auto complete is lovely.


tl;dr: download fish. autocompletion works right out of the box.


I really, really don't like completions as a feature in virtually any shell. I much prefer reverse search.


These things are not mutually exclusive, right? I use both to great effect.


Were you thinking of autosuggestions? (completions are command-specific assistance you get when you hit Tab)


Have you tried fish? You can partially complete the query and use the arrow buttons, and the search is fuzzy if you use the previous commands search. Typing Sudo for instance and arrow will show my previous sudod commands.


Yup, its great. Problem is, the syntax is slightly different from Bash. So whenever I use a shell script or use some kind of commands where the differences are incompatible, I tend to switch to Bash. And then my history doesn't add up with the one from Fish.

For fuzzy search I bound ctrl+r to fzf (you can use Rust's sk too).


https://wiki.archlinux.org/title/fish#Setting_fish_as_intera... you can use it as an interactive shell in bash as well. Fzf can also use Rusty fd. ;) https://github.com/sharkdp/fd


You can have both.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: