Yesterday I told you about how to use the full potential of the
cd command. Today I want to go one step further and show you how you can reduce the number of characters to type even more.
As I've already shown you several times throughout this series, ZSH is very capable of completing things when hitting the TAB key. Today I'll show you two more features concerning completion of abbreviated or incomplete pathnames.
The first feature I want to present you with enables you to complete incomplete parts of a filename. It can be activated with the option
COMPLETE_IN_WORD. You can set the option as usual with
setopt (as always, make sure the completion engine is loaded):
autoload -U compinit && compinit setopt complete_in_word
So now that it's enabled, how do we use this feature? Well, it's very simple. Imagine, you want to switch to the directory
/home/johndoe. You type:
% cd /hme/john
But before you finish the word
johndoe you realize that you didn't get the first part of the pathname right and forgot the o in home (maybe your keyboard is a bit older and some keys have become a little stiff). You could now hit the arrow keys to go back, fill in the missing o, jump back again to the end (either with the arrow keys or with the END key) and then write the rest of the name (or use the normal TAB completion). That's a relatively long procedure. But with
COMPLETE_IN_WORD you can save some time. Just use the arrow keys to go back until the cursor is on the
m right after the missing character and then hit TAB. ZSH will automatically complete
home and also fill in the rest of the name and set your cursor back to the end of it. So you end up with
/home/johndoe/ and the cursor after the last slash, ready to type in more words.
Quick and easy! Can't we always use something like this to save some characters? Of course we can and we don't even need
COMPLETE_IN_WORD for it. We just need to enable the completion engine (see above). Once this is done, we can use abbreviated path names where we want. For example to switch to
The TAB key completes the path and the only thing left to do is to press enter to execute the
cd command. As long as you specify the first (!) few characters of each directory level, ZSH will be able to complete the whole path.
But what does ZSH do when there is more than one possible match? First of all, ZSH evaluates the whole path which makes ambiguous paths very unlikely. For instance, if you enter /ho/jo/Images<TAB> and there is a directory
/home/johndoe and a directory
/home/johnwayne but no directory
/home/johnwayne/Images* then ZSH will automatically complete to
/home/johndoe/Images. If there is still more than one possibility, ZSH will complete up to the first ambiguous match and place the cursor at its position. Just enter a few more characters there and hit TAB again. ZSH will then continue the completion with the additional information it just got from you.
In my opinion, these two filename completion features make switching directories very fast and convenient. If you haven't already tried them out, now is the time. ;-)
Read more about filename completion: