I'm trying to learn how to write completion functions and initially I want to avoid a proliferation of separate files for separate customizations (I already have enough and I don't want more).

Is there a way to write the entire customization logic in your .zshrc file directly?

BTW here's an example I want to play with but again it uses a separate file called _hello.

#compdef hello

_arguments "1: :(World)"

compdef is a command on its own, so you can easily achieve your goal with

compdef '_arguments "1: :(World)"' hello

Here I'm using the first form of compdef:

compdef [ -ane ] function name ... [ -{p|P} pattern ... [ -N name ...]]

where function can be the function to call or alternatively "a string containing almost any shell code".

The full documentation of compdef is way too complex to quote here. The official documentation is here, but you really have to read the whole chapter on the completion system to fully understand it.

  • I also just read that you can define functionas anywhere and just reference them with the compdef command as you have said. – Sridhar Sarnobat Nov 12 '15 at 21:20
  • Yes of course, as I quoted above the most basic usage is compdef function name. – 4ae1e1 Nov 12 '15 at 21:39
  • I've played with this a bit (for docker completion since I don't like what other projects offer) and I keep crashing my Macbook. So I've given up for now. My god, trying to learn ZSH autocompletion is almost as hard as trying to learn how an operating system kernel works. – Sridhar Sarnobat Nov 13 '15 at 0:01
  • You could get errors in your shell, but it's impossible to crash your computer (I assume you mean kernel panic) that way under normal conditions. Even a fork bomb can't result in kernel panic. You probably need to go to your local Apple Store to get your MacBook checked, since constant crashing could be a sign of some sort of hardware failure. – 4ae1e1 Nov 13 '15 at 11:28
  • Thanks for the feedback. Maybe I should experiment using my Ubuntu server. I was worried about crashing that too but from the sound of it it's my oldish Mac OS X – Sridhar Sarnobat Nov 13 '15 at 19:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.