ZSH Gem #9: zshexit() and other hook functions

Posted by | Comments (0) | Trackbacks (0)

Yesterday I told you about the chpwd() hook function. Today I want to go one step further and round up this topic. As you might have found out (or maybe you knew it already or at least guessed it), there are also other hook functions and today I want to present you some.

One of these magic functions or hook functions is zshexit() and it does exactly what the name suggests: it is executed automatically when the shell exists. But it is not executed when a subshell exists. You can check that easily by writing this into your .zshrc:

function zshexit() {
    echo "Hello World!"
}

When you now start another ZSH instance in your current shell and then type exit to leave it again, you should see the text Hello World printed to the screen. But when you run

(echo "Hello subshell")

Nothing except Hello subshell will appear. This is a bit like the .zlogout or .bash_logout files for login shells but a bit more generic.

How can we use this? It's again up to you. One thing is of course a farewell to the user, but that's not of much use (though a nice gesture). A better example would be to do some cleanup work. First of all you can clear the command line (this of course doesn't really work in most graphical terminal emulators, but often it's a good thing):

function zshexit() {
    clear
}

But you can also clean up temporary files, close open sockets, lock encrypted containers or whatever. I know, this is not a really new feature and it can be done with the logout files in nearly every shell. But as mentioned before: this way is more generic as it does not only work for login shells and you don't need extra files which clutter up your home directory. Therefore keep it in mind.

Another interesting hook function is precmd(). This function is executed before drawing the prompt, i.e. every time a new command line is inserted. When could this be of use? It could be a great addition to chpwd() when it comes to context specific tasks etc. (see yesterday's article for this). The downside of chpwd() is that it's only executed when the CWD changes. But if you need to do certain things based on other parameters, this isn't much of use. But you could use precmd(). For instance, you could change the prompt based on how much memory the system has, how much disk space is left, how many users are logged in and what not. With this function you can do all the stuff for which you don't have prompt escape sequences. Feel free to experiment. I've put a link to one gorgeous example of what is possible with precmd() below this article.

One more magic function is preexec(). This is run right after hitting enter on a command and before the command is executed. The entered command is passed to the function as the first parameter. What could this be for? I say it right at the beginning: there is no way to cancel a command, so we can't do any checks for accidental removal actions or so (execpt changing the working directory or renaming the files which are otherwise to be deleted so that the removal would fail, which both can become very cumbersome BTW). But what we could do is to ensure the command can be executed successfully. So, e.g. if there are any left over lock files we could delete them, if we are in the wrong directory, we could change it, if some permissions need to be set, we could alter them. I know, this is very abstract and there are actually very few cases where you'd really need this function, but it's there and I wanted to show it to you.

There are more hook functions, including periodic() (some kind of basic shell based cron mechanism) and zshaddhistory() (for working with the shell history). As for chpwd() it is possible to create an array $functionname_functions with names of custom functions for each hook function where functionname is the name of the main function (e.g. zshexit).

There are also some functions called trap functions which are very similar, but have no hook arrays. Take a look at the manual page I linked below for further information.

Read more about hook functions:

Trackbacks

No Trackbacks for this entry.

Comments

No comments have been submitted yet. Be the first!

Write a comment:

HTML-Tags will be converted to Entities.
Textile-formatting allowed
Standard emoticons like :-) and ;-) are converted to images.
Design and Code Copyright © 2010-2017 Janek Bevendorff Content on this site is published under the terms of the GNU Free Documentation License (GFDL). You may redistribute content only in compliance with these terms. tweetbackcheck