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:

 tabs     tac      tail     tailf    take     tar      tasksel  taskset

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:

A  -- append to an archive
c  -- create a new archive
f  -- specify archive file or device
t  -- list archive contents
u  -- update archive
v  -- verbose output
x  -- extract files from an archive

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:

# while this is a lot of typing
cd ~/Documents/business/git/check42

# this is much more comfy
cd ~/D/b/g/42<tab>

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:

sl -la /tmp
command not found: sl

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:

ENABLE_CORRECTION="true"

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

sl -la /tmp
zsh: correct 'sl' to 'ls' [nyae]?

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

 

9 Comments

  • proessays

    I’m using Sublime text editor for my pro papers. Auto completion in the text is also checked with the script that I wrote several years ago. It’s a very respectable tool for English and French narratives.

  • moto x3m

    You’ll get an inline argument description as well.!

  • Films Jacket

    Pinpoint accuracy sir.. Wonderful, good going!

  • eventos bcn

    Great post! Thanks for sharing us.

  • TechToDown

    great post.Thank you. This is important for me

  • Craap Test

    Excellent blog you have got here… It’s difficult to find high-quality writing like yours these days. I also refer to our blog about Craap Test. It is exciting to read it and, use it. Thanks for the article.