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
❤️ Let's connect!
I would love to grow my network with other tech enthusiasts. Let's connect here or over on twitter! 👋 @langhard
Resources
Update
Part 2 - Passing a parameter to a zsh keyboard-shortcut - is now online.
Top comments (7)
But why define functions & shortcuts if there is a great git plugin for zsh? It's built around aliases, the commands are pretty short and mnemonic, and you can just add any flags you want after typing in the command, i.e. ga -u which is git add -u
Sure, aliases and plugins work as well, if not better.
I just wanted to show how to handle keybindings in zsh here. Git was just an example. Keybindings come in handy for more terminal-based commands imo. Like clearing the screen (which is often bound to
ctrl-l
) and stuff alike.Fair enough, I misunderstood the topic I guess
I am sharing some of my zsh widgets:
in my "autoloaded" folder I also have this function:
The keybinding to open it automatically is:
bindkey -s '^o' 'vif^M'
, so it will run when I press Ctrl-o.Some useful aliases:
I have started using zsh about a couple of weeks ago. I was searching for a dark-theme friendly shell and features like this one.
I can't wait for the post with the "how to pass arguments" example!
Thanks for the time you took on writing this post.
I am happy to hear it is of use for you. Part 2 is now online.
Nice and very useful. Thank you for posting this.
Some comments may only be visible to logged-in visitors. Sign in to view all comments.