head
& tail
in pure zsh
It is very common to use head
and tail
in shells. But as far as I can tell, they are not deployed as builtins for any shell and are instead separate binaries, which isn’t good for optimization. This is true for zsh as well. But, at least with zsh, parameter expansion can replace the need for them easily. For example, if you wanted to pick the first editor from a list of potential choices:
export EDITOR=$(whence -p nvim vim vi micro nano emacs | head -1)
You could easily replace head
with:
export EDITOR=${s$(whence -p nvim vim vi micro nano emacs)[(f)1]}
Where (f)
is the array subscript flag that, when used in combination with scalars (strings) instead of arrays, will break the scalar into lines. This will thus return the first line of the whence
builtin.
You could also replace whence
with a subscript into the $commands
array, but that’s another concept entirely.