Z shell
Z shell (or Zsh) is another Unix shell, very similar to Bash but with more functionality and ease of use.
Installation
zsh
is installed on the Alliance clusters. It is now also the default shell on MacOS. For Windows users, you will probably have to use WSL. Here is a suggested setup (Unix shells such as Bash and Zsh are built for Unix systems and Windows is not a Unix system).
First launch
When you launch zsh
for the first time, a little program runs to help you create the Zsh config file .zshrc
and set some options.
You will probably want to add to this file the aliases, functions, and sourcing that you normally have in your .bashrc
file. For instance, in the modern utilities section, we added the following to our .bashrc
file:
[[ -s $EPREFIX/etc/profile.d/autojump.sh ]] && source $EPREFIX/etc/profile.d/autojump.sh
If you want to use autojump
in Zsh, you should add this to your .zshrc
file.
One thing that you do not have to copy over are the definitions of environment variables that you have defined in .bashrc
with export
because export
ensures that the variables get exported into any shell launched from Bash (and that is what we are doing when we launch Zsh). If you have defined environment variables in .bashrc
without exporting them and you want to use them in Zsh as well, you either need to export them in your .bashrc
file or copy the variable definitions over to your .zshrc
file.
Useful plugins
Out of the box, Zsh is nicer than Bash, but in addition, there are many additional niceties that have been built for it.
oh my zsh is a popular project and it contains many useful configurations and add-ons, but it also contain a lot of unnecessary things and is quite bloated.
I personally prefer to keep things simpler and install 3 great plugins:
The syntax highlighting plug-in is installed on the Alliance clusters. To install the other two, you only have to clone the repos or scp
the scripts themselves.
Clone the repos (you only need to do this once):
# create a directory to store the scripts
mkdir ~/.zsh_plugins
# autosuggestions
git clone https://github.com/zsh-users/zsh-autosuggestions.git ~/.zsh_plugins/zsh-autosuggestions
# history substring search
git clone https://github.com/zsh-users/zsh-history-substring-search.git ~/.zsh_plugins/zsh-history-substring-search
The you have to source the scripts (the one already installed on the Alliance clusters and the ones you just cloned under your own user).
You need to do this at each session, so you should add it to your .zshrc
file:
source $EPREFIX/usr/share/zsh/site-functions/zsh-syntax-highlighting.zsh
source ~/.zsh_plugins/zsh-history-substring-search/zsh-history-substring-search.zsh
source ~/.zsh_plugins/zsh-autosuggestions/zsh-autosuggestions.zsh
For the scripts to become active in your current Zsh session, you also need to run these lines in your terminal or exit Zsh (with Ctl + d
) and launch it back in (this sources the .zshrc
file again and will thus source the scripts).
Now paste the function we saw previously in zsh:
proc_kill() {
local pid
pid=$(ps -ef |
sed 1d |
fzf --cycle --reverse -i -e -m --bind "ctrl-o:toggle-all" \
--header "Tab: toggle, C-o: toggle-all" |
awk '{print $2}')
echo $pid | xargs kill -${1:-15}
}
and you will see the wonders of syntax highlighting in your shell inputs.
To use the history substring search, start typing some command then press Alt + p
or Alt + n
to cycle through all entries in your history that start with what you already typed.
Finally, the autosuggestion will suggestion commands based on your history. You can accept the whole command with Ctl + e
or accept a single word with Alt + f
.