Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am happily using zsh since a while now, and I am quite satisfied with my history settings, which are:

# Write to history immediately
setopt inc_append_history
# History shared among terminals
setopt share_history
# Save extended info in history
setopt extended_history
# Ignore duplicates
setopt hist_ignoredups

But it happens often that I need to use specific commands inside some specific directories. For example, when I am in ~/my_project I usually invoke make target1 && ./run1, but when I am in ~/second_project I usually need make target2 && cat foobar | ./run2.

That is: different directories, but similar commands.

So, I usually cd ~/my_project and type make and then search backward in the history until I find what I need. But if it happen that I worked in second_project, when searching the history I will find some commands that I do not need.

So, my question: does a plugin/setting/something exist for zsh such that, when searching in history, commands invoked in the current directory are displayed first?

Ideally, every other matching history command will appear after those, optionally specifying a max number of priority elements.

I would try to write something like this by myself, but I still do not know how to write custom zsh plugins, how to handle history and so on.

share|improve this question
up vote 5 down vote accepted

There is a plugin that claims to do exactly what you are looking for, appropriately named per directory history plugin:


"Per-directory-history - tracks previous command history both per current directory and globally, with the ability to switch between them on the fly, bound to ctrl-g." says their wiki.

I did not test it myself (yet), but according to the comments it should work.

share|improve this answer
Nice, thanks! I will try it before accepting, if you don't mind :) – AkiRoss Apr 5 '13 at 17:07
Until I missed something, the plugin does not exactly what I need, in the sense that per directory and global histories are mutually switched using ctrl-g (and not by priority as I asked). Nevertheless, I still find it extremely useful and a good answer to my question, so, thanks :) – AkiRoss Apr 5 '13 at 17:20

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.