in ✏️ Articles · terminal tip software

cd subdirectory alias with completions in zsh

For quite some time, I’ve had a helper function on my ZSH config to quickly be able to cd to specific directories. It looked something like this:

function cdc() {
  cd "$HOME/Code/$1"

So simply typing cdc or even cdc hacdias/ would work perfectly. But there was a problem: I was seeing myself just typing cdc, pressing enter, and then “cding” to the right directory. This mostly happened because there was no autocomplete with this function.

Yesterday I decided to investigate how to enable autocomplete for this function and it’s actually pretty straightforward. To do so, you need to implement a definition function. It is standard to call it _{name of alias}. Hence, in our case, it would be _cdc.

function cdc() {
  cd "$HOME/Code/$1"

function _cdc() {
  ((CURRENT == 2)) &&
  _files -/ -W "$HOME/Code"

compdef _cdc cdc

On the completion definition function _cdc we use ((CURRENT == 2)) to make sure we only give definitions for the directory part of the command. _files is a builtin for auto completing paths.

Now, if you restart your shell, you can just type cdc and then press tab. You’ll now see the completion suggestions!

Bonus code

If you have more than one cd subdirectory alias like me, instead of manually defining the functions and their completions for all aliases, you can create an associative array and a simple for loop:

typeset -A cd_aliases

for k in "${(@k)cd_aliases}"; do
  function $k() {
    cd "$cd_aliases[$0]/$1"

  function _$k() {
    ((CURRENT == 2)) && _files -/ -W $cd_aliases[${0:1}]

  compdef _$k $k

I hope this can be as useful for you as it was for me!

Interactions (0)