ohmyzsh/plugins/per-directory-history
Codebuff Contributor 99486342e5 fix(per-directory-history): save to both history files and only push stack in directory mode
The addhistory hook previously had two bugs when inc_append_history
or share_history was not set:

1. Commands were only saved to one history file (HISTFILE or per-dir)
   depending on when fc -AI was called, resulting in lost history.

2. fc -p was called unconditionally after every command, corrupting
   the zsh history stack even in global mode. This caused the per-dir
   hook to push a new history frame on every command, breaking the
   global history state.

Fix: always write both history files via fc -AI before any mode
check, and only call fc -p (to push into per-directory history)
when actually in directory mode.
2026-05-16 09:00:35 +06:00
..
per-directory-history.plugin.zsh Update to latest per-directory-history 2013-02-28 10:28:08 -05:00
per-directory-history.zsh fix(per-directory-history): save to both history files and only push stack in directory mode 2026-05-16 09:00:35 +06:00
README.md docs(per-directory-history): add variable docs (#12844) 2024-12-15 23:40:09 +01:00

per-directory-history plugin

This plugin adds per-directory history for zsh, as well as a global history, and the ability to toggle between them with a keyboard shortcut. This is a bundle of the official plugin by @jimhester.

To use it, add per-directory-history to the plugins array in your zshrc file:

plugins=(... per-directory-history)

This is an implementation of per-directory history for zsh, some implementations of which exist in bash1,2. It also implements a toggle-history function to change from using the directory history to using the global history. In both cases the history is always saved to both the global history and the directory history, so the toggle state will not effect the saved histories. Being able to switch between global and directory histories on the fly is a novel feature.

Usage

The default mode is per directory history, interact with your history as normal.

Press ^G (the Control and G keys simultaneously) to toggle between local and global histories. If you would prefer a different shortcut to toggle set the PER_DIRECTORY_HISTORY_TOGGLE environment variable.

Configuration

  • HISTORY_BASE is a global variable that defines the base directory in which the directory histories are stored (default $HOME/.directory_history).
  • per-directory-history-toggle-history is the function to toggle between local and global histories.
  • PER_DIRECTORY_HISTORY_TOGGLE is the key binding used to run the toggle-history function above (default ^G)
  • PER_DIRECTORY_HISTORY_PRINT_MODE_CHANGE is a variable which toggles whether the current mode is printed to the screen following a mode change (default true)
  • HISTORY_START_WITH_GLOBAL is a global variable that defines how to start the plugin: global or local (default false)

History

The idea/inspiration for a per directory history is from Stewart MacArthur and Dieter, the implementation idea is from Bart Schaefer. The implementation is by Jim Hester in September 2012.