3 minute read Published:

ZSH: How Deep Am I

If you are on unix and you haven’t tried zsh, I would recommend it. If you use zsh and haven’t tried oh-my-zsh,  I would recommend it. I won’t go into too much detail on oh-my-zsh and how it helps manage your zsh configuration, but, it provides two features that make command line work more pleasant. 1) Plugins for integration with things like git, gulp, ruby, etc and 2) a number of different themes.

One of the plugins I use is Gradle. I noticed that it hadn’t been updated for a while because it was missing some switches from newer versions of Gradle, so I updated it. It was a trivial update and while I was there I noticed that the plugin lacked the ability to auto-complete the gradle init command. That is a more complex patch and required me to start learning zsh scripting.

Writing zsh and testing various functions is pretty straight forward-write a function in a file or directly on the command line. Updating configurations (which is what oh-my-zsh manages) is slightly harder to test. The best recommendation that I found, was to just open zsh in zsh.

Screenshot from 2015-12-02 20-00-07

I would open up zsh nested, let oh-my-zsh do its work with my changes and then test. Now I have a shell inside a shell, if I type exit then I’m back to my base shell. If I type exit again then whatever terminal I’m in will shut down. If I got distracted and forgot what level I was at in my nested zsh, and exited, then I had to started my terminal all over again. This is slightly annoying, so I added a little script to my .zshrc file to help me track where I’m at.

export ZSH_LEVEL
print -Pn \e]2;${ZSH_LEVEL}\a';
function zshexit_shelllevel() {
    let ';ZSH_LEVEL--';
    print -Pn '\e]2;${ZSH_LEVEL}\a'

Let me explain this real quick. Line 3 and 7 will allow us to change the the window title (that’s what the 2 is saying). zshexit_functions is a zsh builtin function that will get called when zsh is exited. So, when the .zshrc get source on start up it increments ZSH_LEVEL and when the zshexit_shelllevel gets called it decrements the ZSH_LEVEL, changing the window title every time.

It looks like this:


See that little “2” in the bottom right-hand corner.

I’m sure there are simpler/better ways to do this, but I got a kick out of learning some simple zsh builtins and display preferences, both of which are used heavily in oh-my-zsh.

Edit (March 19, 2016):

As expected, there is a built-in solution for this problem. Check out nath_schwarz solution on reddit.