Skip to content

Instantly share code, notes, and snippets.

@Luxcium
Forked from wess/folder-history.plugin.zsh
Created January 21, 2025 23:41
Show Gist options
  • Save Luxcium/2ba91c576361d3ae6b09ccce4f12dcf2 to your computer and use it in GitHub Desktop.
Save Luxcium/2ba91c576361d3ae6b09ccce4f12dcf2 to your computer and use it in GitHub Desktop.
OhMyZsh plugin for local/folder based history
# Enable Zsh options for history
setopt EXTENDED_HISTORY
setopt INC_APPEND_HISTORY
setopt SHARE_HISTORY
setopt HIST_FIND_NO_DUPS
# Clear local history and load a fresh session for the current directory
function load_local_history() {
if [[ -f .zsh_cmd_history ]]; then
# Clear in-memory history and reload only local history
fc -p .zsh_cmd_history
else
# Create a fresh local history file
: > .zsh_cmd_history
fc -p .zsh_cmd_history
fi
}
# Save current session's history to local file
function save_local_history() {
if [[ -n $PWD ]]; then
fc -W .zsh_cmd_history
fi
}
# Add hooks for loading and saving local history
autoload -Uz add-zsh-hook
add-zsh-hook chpwd load_local_history # Load local history when changing directories
add-zsh-hook precmd save_local_history # Save local history before each prompt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment