zsh tips: Auto completion & correction

When it comes to the amount of work to achieve a desired result, I’m kind of a lazy person. I like my workflows to be optimised, and I’m a fan of keyboard shortcuts. Everything in my shell (zsh, tmux), editor (vim, SublimeText) and operating system (Linux, OS X) needs to be keyboard optimised for fast access.

More optimised workflows means you get time for more important stuff, like enhancements of your environment, or spending time with your lovely ones. And on the other hand, if you do keyboard shortcuts right, people watching your screen get astonished by your hacking.

Auto completion

If you’re used to other shells – especially bash – you might want to say that auto completion is nothing new for you. If you’re working optimised, you might already use a more advanced system like bash-completion. So you expect no surprises here, right? I’ve to disappoint you, because zsh’s auto completion is kick-ass, and it will leave bash-completion in the dust!

Auto completion in zsh is done by hitting the tab  key (like most reasonable shells):

For example, if I type  ta<tab> , I get the following output:

The nice thing about zsh is, I can press tab  again, and then move with my cursor keys to select the right command.

But it will not only complete command, it will also complete arguments in a really nice way. Let’s say you want to create a tar ball, but you can’t remember the arguments for tar (shame on you). Type  tar -<tab> and you’ll get a really nice help view:

As you can see, you’ll not only get a cursor-selectable list of arguments, but you’ll get an inline argument description as well.

The thing I really like the most about auto completion, is the completion of directory names:

Of course there are more completions available, like usernames (e.g. usermod -a -G <tab>), hostnames (e.g.  ssh <tab>), function names, and many more.

Auto correction

When we talk about optimisation, we ultimately talk about speed. When it comes to shells, I like to type fast – sometimes too fast. It happens to all of us, we’re in hurry, hack something into the shell, hit the enter key, and then:

Happened to you as well, hasn’t it?

But there’s a really neat auto correction implementation in zsh, which (I think) is disabled by default. To enable auto correction, you’ve to set:

Now let’s say you’ve misspelled the command above again:

Because zsh is your friend, it already noticed your typo. Instead of running the command up against the wall, zsh will provide an auto correction with 4 different options:

  • n  which means no, don’t correct my command and execute it as it is
  • y  which means yes, correct my command
  • a  which means abort, don’t execute the command and give me an empty prompt
  • e  which means edit, don’t execute the command and let me edit it again

 

One Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.