Command expansion

An kind of expansion which zsh provides and which one may miss in other shells is command expansion. In zsh, an expression of the form =name is replaced by the absolute path of the command name, provided that it can be found by searching $PATH. This allows, for example, editing executable scripts without knowing their absolute location.

Of course one can always use expressions of the kind

vi $(which script)
but this rather tedious, especially if a command contains several such expressions.

As a substitue for command expansion I wrote a function with the minimalistic name p. It assumes that the parameter list constitutes a complete command. First, every parameter representing a simple name is looked up using $PATH, and if found, replaced by its absolute path. Then the function executes the modified command using eval.

The file contains an auxiliary function _p_complete and a complete command for the bash completion mechanism. Their effect is that any parameter representing the name of an executable file will be changed into an absolute path by pressing the tab key.


  1. Editing a script myscript which is reachable via $PATH:
    p vi myscript
  2. Copying the script myscript as myscript1 into the working directory:
    p cp -a myscript myscript1
  3. Copying prog as prog1 into the directory containing prog:
    p cp prog $(p dirname prog)/prog1
Created 2011-08-11 by mopcoge