Navigate your shell history with CTRL keys (#tilPost)

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…


This content originally appeared on Stefan Judis Web Development and was authored by Stefan Judis

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.


Reply to Stefan


This content originally appeared on Stefan Judis Web Development and was authored by Stefan Judis


Print Share Comment Cite Upload Translate Updates
APA

Stefan Judis | Sciencx (2024-11-03T15:00:00+00:00) Navigate your shell history with CTRL keys (#tilPost). Retrieved from https://www.scien.cx/2024/11/03/navigate-your-shell-history-with-ctrl-keys-tilpost/

MLA
" » Navigate your shell history with CTRL keys (#tilPost)." Stefan Judis | Sciencx - Sunday November 3, 2024, https://www.scien.cx/2024/11/03/navigate-your-shell-history-with-ctrl-keys-tilpost/
HARVARD
Stefan Judis | Sciencx Sunday November 3, 2024 » Navigate your shell history with CTRL keys (#tilPost)., viewed ,<https://www.scien.cx/2024/11/03/navigate-your-shell-history-with-ctrl-keys-tilpost/>
VANCOUVER
Stefan Judis | Sciencx - » Navigate your shell history with CTRL keys (#tilPost). [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/11/03/navigate-your-shell-history-with-ctrl-keys-tilpost/
CHICAGO
" » Navigate your shell history with CTRL keys (#tilPost)." Stefan Judis | Sciencx - Accessed . https://www.scien.cx/2024/11/03/navigate-your-shell-history-with-ctrl-keys-tilpost/
IEEE
" » Navigate your shell history with CTRL keys (#tilPost)." Stefan Judis | Sciencx [Online]. Available: https://www.scien.cx/2024/11/03/navigate-your-shell-history-with-ctrl-keys-tilpost/. [Accessed: ]
rf:citation
» Navigate your shell history with CTRL keys (#tilPost) | Stefan Judis | Sciencx | https://www.scien.cx/2024/11/03/navigate-your-shell-history-with-ctrl-keys-tilpost/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.