Re-run past commands in their original context
A context-aware history extension for Bash & Zsh
ctxhist is more than just shell history. It records:
"When, where, and what you ran",
and allows you to re-run past commands in the exact directory they were originally executed.
⚠️ Depends onfzf– an interactive fuzzy finder for history and command selection.
Run a past command exactly where you ran it before.
In this example:
- You're currently in
/home/demo/work/project1 - You press
Ctrl-g Ctrl-rand select a previouscargo runcommand fromproject2viafzf - It auto-inserts:
cur=$PWD; cd /home/demo/work/project2 && cargo run; cd "$cur" - The command runs in
project2, then returns to the original directory - You press
Ctrl-g Ctrl-aand again select the samecargo runfromproject2 - It auto-inserts:
cd /home/demo/work/project2 && cargo run - The command runs and stays in
project2
| Feature | Traditional History (history / Ctrl-r) |
ctxhist |
|---|---|---|
| Command search | ✅ (with fzf) |
✅ (with fzf) |
| Records working directory | ❌ | ✅ (logged every time) |
| Auto directory switching | ❌ | ✅ (stay/restore modes) |
| Duplicate removal & limit | ❌ (requires manual config) | ✅ (built-in) |
- Logs
timestamp / directory / command - Fuzzy search with
fzfacross all or filtered directories - Stay/Restore modes for directory control
- Automatic duplicate removal and log trimming
- Insert commands directly using keybinds
| Mode | All History (global) | Subdirectory only |
|---|---|---|
| stay | Ctrl-g Ctrl-a |
Ctrl-o Ctrl-a |
| restore | Ctrl-g Ctrl-r |
Ctrl-o Ctrl-r |
Example:
Ctrl-g Ctrl-r→ Select via fzf → Temporarily cd & run → Return to original dir
Customizable via environment variables (e.g., CTXHIST_BINDKEY_STAY)
| Mode | Behavior | Example command |
|---|---|---|
stay |
Remain in the directory after run | cd ~/proj && docker up |
restore |
Return to original directory | cur=$PWD; cd ~/proj && docker up; cd "$cur" |
Location: $CTXHIST_LOG_FILE (default: ~/.config/ctxhist.log)
YYYY-MM-DD HH:MM:SS | /path/to/dir | command here
Currently supports Bash and Zsh (Oh My Zsh).
Other shells like Fish are not yet supported, but may be considered in the future.
sudo apt install fzf # on Ubuntu/Debian
brew install fzf # on macOSgit clone --depth 1 https://github.com/nakkiy/ctxhist.git ~/.ctxhistAdd the following to your .bashrc:
+ export CTXHIST_LOG_FILE="$HOME/.config/ctxhist.log"
+ export CTXHIST_MAX_LINES=10000
+ export CTXHIST_EXCLUDE_CMDS="cd clear ls"
+ export CTXHIST_BINDKEY_STAY='\C-g\C-a'
+ export CTXHIST_BINDKEY_RESTORE='\C-g\C-r'
+ export CTXHIST_BINDKEY_SUBDIR_STAY='\C-o\C-a'
+ export CTXHIST_BINDKEY_SUBDIR_RESTORE='\C-o\C-r'
+ source ~/.ctxhist/ctxhist.bashThen apply changes:
source ~/.bashrcsudo apt install fzf # on Ubuntu/Debian
brew install fzf # on macOSgit clone --depth 1 https://github.com/nakkiy/ctxhist.git ~/.oh-my-zsh/custom/plugins/ctxhistAdd the following to your .zshrc:
+ export CTXHIST_LOG_FILE="$HOME/.config/ctxhist.log"
+ export CTXHIST_MAX_LINES=10000
+ export CTXHIST_EXCLUDE_CMDS="cd clear ls"
+ export CTXHIST_BINDKEY_STAY='\C-g\C-a'
+ export CTXHIST_BINDKEY_RESTORE='\C-g\C-r'
+ export CTXHIST_BINDKEY_SUBDIR_STAY='\C-o\C-a'
+ export CTXHIST_BINDKEY_SUBDIR_RESTORE='\C-o\C-r'
# Enable the plugin
- plugins=(git)
+ plugins=(git ctxhist)Apply changes:
source ~/.zshrcrm ~/.config/ctxhist.log
rm -r ~/.ctxhistThen remove the corresponding lines from .bashrc and run:
- export CTXHIST_LOG_FILE="$HOME/.config/ctxhist.log"
- export CTXHIST_MAX_LINES=10000
- export CTXHIST_EXCLUDE_CMDS="cd clear ls"
- export CTXHIST_BINDKEY_STAY='\C-g\C-a'
- export CTXHIST_BINDKEY_RESTORE='\C-g\C-r'
- export CTXHIST_BINDKEY_SUBDIR_STAY='\C-o\C-a'
- export CTXHIST_BINDKEY_SUBDIR_RESTORE='\C-o\C-r'
- source ~/.ctxhist/ctxhist.bashsource ~/.bashrcrm ~/.config/ctxhist.log
rm -r ~/.oh-my-zsh/custom/plugins/ctxhistThen remove the corresponding lines from .zshrc and run:
- export CTXHIST_LOG_FILE="$HOME/.config/ctxhist.log"
- export CTXHIST_MAX_LINES=10000
- export CTXHIST_EXCLUDE_CMDS="cd clear ls"
- export CTXHIST_BINDKEY_STAY='\C-g\C-a'
- export CTXHIST_BINDKEY_RESTORE='\C-g\C-r'
- export CTXHIST_BINDKEY_SUBDIR_STAY='\C-o\C-a'
- export CTXHIST_BINDKEY_SUBDIR_RESTORE='\C-o\C-r'
# Enable the plugin
- plugins=(git ctxhist)
+ plugins=(git)source ~/.zshrcsudo apt remove fzf # on Ubuntu/Debian
brew uninstall fzf # on macOS| Variable Name | Description | Default Value |
|---|---|---|
CTXHIST_LOG_FILE |
Path to the log file | ~/.config/ctxhist.log |
CTXHIST_MAX_LINES |
Max number of lines to keep in log | 10000 |
CTXHIST_EXCLUDE_CMDS |
Commands to exclude (space-separated) | e.g., "cd clear ls" |
CTXHIST_BINDKEY_* |
Keybindings for each mode | \C-g\C-a, etc. |
If you have feedback, ideas, or run into any issues, feel free to open an issue!
- Share your impressions or thoughts
- Report bugs or unexpected behavior
- Suggest new features or improvements
English or Japanese — either is welcome!
- Zsh plugin support (✅ now supported via Oh My Zsh)
- Possibly support Fish shell if requested
