Are you a zsh user? Do you like to tweak your shell to the maxium in order to gain a bit of time here or there? You might be in the right place here.

This post will show you how to use keyboard shortcuts within zsh to run common commands.

git pull using a keyboard shortcut

In this example we want to run git pull with the shortcut ctrl-g+p (meaning ctrl-g - releasing g - pressing p ) as seen in the gif above.

We define our shortcuts in the zsh ressource file. It's normally called .zshrc and we find it within our home directory. I use visual studio code to edit the file:

code ~/.zshrc

Add the following content to the file:



# git pull (ctrl-g+p) function gitpull () { echo "git pull" ; git pull ; zle reset-prompt ; zle redisplay } zle -N gitpull bindkey '^gp' gitpull

Lets break it down line-by-line

A nice comment We define the function gitpull which we use for our keybinding later. The function does: Write out the command we send. (I like this over having an empty screen doing something) Runs git pull Resets our prompt. (This is useful, if we have git information in our prompt, as they are not updated if we don't use reset-prompt ) Redisplays our prompt We define a widget that points to the function We bind our key-combination to the widget

That's it. Let me know if it works for you. If there is some interest in this kind of stuff, I could follow up with an example of how to pass an argument to a keyboard shortcut. I for example use this option to create feature branches by doing:

typing the name of the feature branch --> ctrl-g+f

Bonus

Some more shortcut definition examples also using alt as the modifier to get you started:



'^g' # ctrl-g '^gp' # ctrl-g and then p (as shown in the example) '^[x' # alt-x '^[xc' # alt-x and then c

I would love to grow my network with other tech enthusiasts. Let's connect here or over on twitter! 👋 @langhard

Resources

A User's Guide to the Z-Shell

Update

Part 2 - Passing a parameter to a zsh keyboard-shortcut - is now online.