Zsh History Control

Many comes to Zsh through the famous oh-my-zsh project, but fewer, including myself, aware of the power Zsh gives us out of box.

In this post I’d like to illustrate history control feature.

Up/Down Arrow Keys

The default behavior for arrow keys is to search in the history for a line beginning with the first word in the buffer.

So, when you type git, then arrow up key, it will navigate you through the last commands starting with git, but even if you type git status it still shows you all commands starting with git.

This is default behavior for up/down arrows in Zsh, which can be easily changed to something different, for example to make it search in the history for a line beginning with the current line up to the cursor add the following into your .zshrc file:

bindkey '\e[A' history-beginning-search-backward
bindkey '\e[B' history-beginning-search-forward

Search incrementally for a specified string

Another helpful Zsh feature:

  • Ctrl+R to search backward for a specified word, you’ll be prompted
  • Ctrl+S to search forward for a specified word, you’ll be prompted

Now go to your Zsh screen and press Ctrl+R, then enter any word, e.g. “help”, it will pick up and shows you the first command from the history containing the word “help”, press Ctrl+R again and it will show eldest command and so on, press Ctrl+S to search forward. When done just press Enter to execute the command.

Conclusion

That’s it, you can learn more about Zsh on it’s website and particularly about it’s History Control here.

Thank you!

Comments