Monday, October 8, 2012

Zsh rehash automatically

I am tired of installing a new program in the command-line and having to use the rehash command so zsh can recognize the new executable in my PATH.

Adding the following line to your .zshrc will refresh it immediately after installing something from pacman or yaourt.
TRAPUSR1() { rehash}; precmd() { [[ $history[$[ HISTCMD -1 ]] == *(pacman|yaourt)* ]] && killall -USR1 zsh }

The above code will also refresh for other terminals that are already open, so no need to close it or rehash it.

if you are using Debian or other, just change *(pacman|yaourt)* to your package manager, e.g: *(apt-get|aptitude)* or just one package manger: *apt-get*

3 comments:

  1. Nice one :)
    I use "add-zsh-hook precmd rehash_precmd" to avoid a potential conflict with an already defined precmd.

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. Unfortunately, you have to install a signal handler for USR1 into every ZSH process (e.g. trap USR1 in your /etc/zshenv) otherwise all zsh processes, including scripts, who don't trap USR1 will exit.

    My zshrc uses a different approach that should work everywhere: https://github.com/eatnumber1/dotfiles/commit/1237e2f880758dc9e819048f51a505a11698a5ce

    ReplyDelete