Published at
Updated at
Reading time
2min
This post is part of my Today I learned series in which I share all my web development learnings.

I'm terrible at optimizing my keyboard productivity. Occassionally, I try to save some keystrokes beyond the shortcuts I use every day... but learning and memorizing new key combinations is just very hard.

Today, I discovered a neat little trick that will hopefully make it into my default strokes.

You probably know you can access all your previously run commands with the arrow keys (โ†‘ / โ†“). This functionality works in all shell environments I've used so far. There's nothing wrong with skipping through your history this way, except that your hands have to move away from their primary keyboard position (asdf / jkl;) to reach for the arrows.

Today I learned, that most shells in their default configuration (I tested bash, zsh and zsh with oh-my-zsh) allow you to do the same with ctrl+p (p for "previous") and ctrl+n (n for "next").

Example showing how to navigate the shell history with โ†‘ / โ†“ and ctrl+p / ctrl+n

Pro tip: I have CAPSLOCK mapped to ctrl to avoid moving my hands.

I've been trying these new combinations for a day now, and not reaching for the arrow keys feels great!

Bonus tip: fuzzy search your shell history

While looking into this topic I also realized that my shell (Oh My Zsh) provides a feature that isn't part of the default zsh config.

Instead of navigating the entire shell history via the arrow keys, Oh My Zsh adds a fuzzy search for the string I already typed. And I'm using this feature every single day!

For example, if I'm looking for a specific ls command, I can type ls and then navigate all history entries starting with ls. No history grepping, no manual searching; my shell just does it for me.

To enable the fuzzy search functionality for the ctrl mappings, you can add your own custom key bindings using history-beginning-search-backward or history-beginning-search-forward.

And if you're using "Oh My Zsh" you can rely on up-line-or-beginning-search and down-line-or-beginning-search. These "Oh My Zsh" functions also add convenience cursor handling.

# enable fuzzy backward search for navigating the command
# history with ctrl-p and ctrl-n

# without oh-my-zsh
bindkey '^P' history-beginning-search-backward
bindkey '^N' history-beginning-search-forward

# with oh-my-zsh
bindkey '^P' up-line-or-beginning-search
bindkey '^N' down-line-or-beginning-search

And here's my added configuration in action.

Example showing how to fuzzy search throw the history with โ†‘ / โ†“ and ctrl+p / ctrl+n

It's a tiny thing, but I hope that I'll move my hands less and move these juicy ctrl combinations into my muscle memory.

If you enjoyed this article...

Join 5.4k readers and learn something new every week with Web Weekly.

Web Weekly โ€” Your friendly Web Dev newsletter
Reply to this post and share your thoughts via good old email.
Stefan standing in the park in front of a green background

About Stefan Judis

Frontend nerd with over ten years of experience, freelance dev, "Today I Learned" blogger, conference speaker, and Open Source maintainer.

Related Topics

Related Articles