Last active
January 23, 2025 01:39
-
-
Save wess/9aec93162363ba7b15d92537a2212e89 to your computer and use it in GitHub Desktop.
OhMyZsh plugin for local/folder based history
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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