Friday, January 18, 2013

ZSH autocompletion for tmuxinator

I have been using robbyrussell's Oh My Zsh distribution for a few months now and I feel that it's a very good starting point for ZSH newbies. It's a reasonably good set of plugins, themes and configuration files that makes terminal users' life easier.

Yet another handy tool is tmuxinator which makes using/configuring/starting/managing tmux sessions a cinch. However I found that zsh doesn't have autocompletion for tmuxinator command built-in. And all it'd do when I press 'tab' is list out the files and directory in the current directory. It would be cool if I can make it autocomplete command-line options and project names. And that's what I did this morning.

Here is the piece of code you can put it in file called tmuxinator.plugin.zsh inside custom/plugins/tmuxinator/ directory.

_tmux_cmd() {
  # Constants
  local tmuxinator_configs_path
  tmuxinator_configs_path="$HOME/.tmuxinator"

  local a
  read -l a

  num_args=$(echo "$a" | awk '{print NF}')
  last_arg=$(echo "$a" | awk '{print $NF}')

  if [[ $num_args -le 1 ]]; then
    reply=(start open copy delete implode list doctor help version)
  else
    # whether to autocomplete project names
    if [[ -n "$(echo "start open copy delete" | grep -E "\<$last_arg\>")" ]]; then
      reply=(`ls "$tmuxinator_configs_path" | sed 's/\.yml$//' | sed -e :a -e '$!N; s/\n/ /; ta'`)
    fi
  fi
}

compctl -K _tmux_cmd tmuxinator

Here's how the final outcome looks...

When I enter 'tmuxinator ' and press tab, I'll get this

When it is 'tmuxinator open ' (or start or delete or copy), it'd give

Great!

1 comment:

Chun Yang said...

Thank you! This is exactly what i am looking for!