1UP your Zsh abilities by autoloading your own functions (2024)

lukeojones

Posted on

1UP your Zsh abilities by autoloading your own functions (2) 1UP your Zsh abilities by autoloading your own functions (3) 1UP your Zsh abilities by autoloading your own functions (4) 1UP your Zsh abilities by autoloading your own functions (5) 1UP your Zsh abilities by autoloading your own functions (6)

#bash #tutorial #beginners

Zsh is quickly becoming the shell of choice for software developers and, if you want to maximise your productivity at the CLI, you should know how to create and autoload your own Zsh functions.

1UP your Zsh abilities by autoloading your own functions (7)

Even novice CLI users will have added the occasional alias in order to avoid repeatedly typing out a verbose command. Whilst aliases are great, they aren’t well suited to the more complicated, logical code snippets that you may want to run on a regular basis.

In this whistle-stop tour, I’ll show you how to create basic Zsh functions and integrate them with Zsh’s autoloading system so they’re available during interactive shell sessions.

Creating our function

After reading this article, I’m sure you’ll be inspired to write lots and lots of functions but, before you get carried away, it’s good practice to create a directory to store them all. I usually create a hidden subdirectory in my home directory called .zshfn

By convention, each function is stored in a file with the same name; for the purposes of keeping this guide snappy, I’m going to create a very simple clock function that just displays the current time and so I’ll create a file called clock (no file extension) to put my function code in.

mkdir ~/.zshfntouch $_/clock

Now edit the clock file and add the following snippet:

# Initialisation Codeecho "Initialising Clock"# Display simple clock every secondclock () { while [ true ] do echo "==========" date +"%r"; echo "==========" sleep 1; clear; done}# Clear the three lines from the terminalclear() { for i in {1..3}; do tput cuu1 tput el done}clock

We’ll come back to the function later — for now just save the clock file.

Configuring our .zshrc file

In order to make our function accessible whenever we startup a new terminal, we need to make a couple of modifications to the .zshrc file.

First we need to ensure we add our .zshfn function directory to the Zsh file search path (a.k.a fpath) so that Zsh knows it should look in our new directory for functions. As you might expect, the fpath is traversed from left-to-right like other unix path variables.

Secondly, we need to tell Zsh about the clock function we just created — this is achieved via the autoload command. The autoload command essentially instructs Zsh to register a function (called clock) using the contents of the clock file that is located in a directory somewhere along the fpath.

# Bottom of .zshrc filefpath=( ~/.zshfn "${fpath[@]}" )autoload -Uz clock

As you add more functions, you’ll find it becomes cumbersome to append each one to the end of the autoload command and so (if you want to be super cool) you can choose to autoload all files in your .zshfn folder using the following alternative:

# Bottom of .zshrc filefpath=( ~/.zshfn "${fpath[@]}" )autoload -Uz $fpath[1]/*(.:t)

If you’re wondering what the -Uz options do, they simply ensure that autoloading will do the right thing regardless of what other options you may have configured (i.e. enforces disabled alias-expansion and zsh style autoloading).

Running our function

After saving our clock function and the .zshrc file we are now ready to test the function out. In your terminal begin typing clock — you should be able to tab-complete the function and if you have syntax highlighting turned on you should see that kick in too.

1UP your Zsh abilities by autoloading your own functions (8)

Hit return and you should see the following output:

1UP your Zsh abilities by autoloading your own functions (9)

Remember that as far as Zsh is concerned, before running the clock function, the entire contents of the clock file are used as the function definition. This is why the first time you run clock, you see the initialisation code being executed.

Kill the function (Ctrl-c) and run the function again — you should see the following output the second time around:

1UP your Zsh abilities by autoloading your own functions (10)

Notice that in the second execution, the initialisation code does not run— this is because the first time we ran the function, the clock function was redefined to use the actual clock() function defined in our file. This can be a really nice pattern to use if you have some sort of one-off initialisation you need to accomplish.

At this point you might be wondering why the clock() method is even needed? Well actually, it isn’t. You could replace the clock file with the following contents and it would work just fine.

echo "Initialising Clock"while [ true ]do echo "==========" date +"%r"; echo "==========" sleep 1; for i in {1..3}; do tput cuu1 tput el donedone

If you now execute this version of the clock function twice in a row you’ll see that, because the clock function is not redefined, the initialisation code runs both times.

1UP your Zsh abilities by autoloading your own functions (11)

Which approach you use is largely down to your use-case and stylistic preferences. I tend to prefer the first option (i.e. encapsulate my clock code in a clock() function block) but in many cases it is completely superfluous.

I hope you learned something from this quick write-up — there is a lot more to Zsh functions than covered here and if you want more detailed information, I’d recommend you check out this resource.

In any case, functions are a great way to optimise your CLI experience and it’s certainly worthwhile knowing how to use them 😎👍

1UP your Zsh abilities by autoloading your own functions (2024)
Top Articles
Latest Posts
Article information

Author: Rob Wisoky

Last Updated:

Views: 6714

Rating: 4.8 / 5 (68 voted)

Reviews: 83% of readers found this page helpful

Author information

Name: Rob Wisoky

Birthday: 1994-09-30

Address: 5789 Michel Vista, West Domenic, OR 80464-9452

Phone: +97313824072371

Job: Education Orchestrator

Hobby: Lockpicking, Crocheting, Baton twirling, Video gaming, Jogging, Whittling, Model building

Introduction: My name is Rob Wisoky, I am a smiling, helpful, encouraging, zealous, energetic, faithful, fantastic person who loves writing and wants to share my knowledge and understanding with you.