From Electron Cloud
Jump to: navigation, search
(correct some problems I found)
Line 17: Line 17:
 
# per-directory history
 
# per-directory history
 
shopt -s histappend  # always append to history, don't replace
 
shopt -s histappend  # always append to history, don't replace
alias cd="cd_with_local_history"
+
alias cd='cd_with_local_history'
alias chistory="builtin history"
+
alias chistory='builtin history'
alias history="cat $HOME/.bash_history"
+
alias history='cat $HOME/.bash_history'
 +
export HISTFILE="$PWD/.bash_cwd_history"
 
function cd_with_local_history()
 
function cd_with_local_history()
 
{
 
{
export HISTFILE="$PWD/.bash_cwd_history"
+
        export HISTFILE="$PWD/.bash_cwd_history"
history -w # write the local history file  
+
        builtin history -w # write the local history file  
cat $HOME/.bash_history .bash_cwd_history | uniq > .bash_cat_history
+
        cat $HOME/.bash_history .bash_cwd_history | uniq > .bash_cat_history
mv .bash_cat_history $HOME/.bash_history
+
        mv .bash_cat_history $HOME/.bash_history
builtin cd "$@" # do actual cd
+
        builtin cd "$@" # do actual cd
export HISTFILE="$PWD/.bash_cwd_history"
+
        export HISTFILE="$PWD/.bash_cwd_history"
history -c # clear memory
+
        builtin history -c # clear memory
history -r #read from current histfile
+
        builtin history -r #read from current histfile
 
}
 
}
 
</pre>
 
</pre>

Revision as of 09:22, 14 March 2012

Problems:

  • Hate it when I can't find a useful command which was last executed too long ago and has "expired" from bash history
  • bash history is too cluttered with commands from all over the whole system; I want a per-project history (project == directory, usually)
  • but, I still want a global time-ordered history too (at least approximately)

There is a partial solution here upon which this is based.

Solution:

  • redefine "cd" to save per-directory history inside the directory itself, and also append that to an ever-expanding ~/.bash_history
  • replace in-memory history with the one from the CWD so that control-R searches don't find irrelevant commands
  • redefine "history" to get the global history so that I can do comprehensive history | grep searches
  • add a "chistory" command to get CWD-specific history only

If want to try it, stick this in your ~/.bashrc (and smoke it, or something):

# per-directory history
shopt -s histappend  # always append to history, don't replace
alias cd='cd_with_local_history'
alias chistory='builtin history'
alias history='cat $HOME/.bash_history'
export HISTFILE="$PWD/.bash_cwd_history"
function cd_with_local_history()
{
        export HISTFILE="$PWD/.bash_cwd_history"
        builtin history -w # write the local history file 
        cat $HOME/.bash_history .bash_cwd_history | uniq > .bash_cat_history
        mv .bash_cat_history $HOME/.bash_history
        builtin cd "$@" # do actual cd
        export HISTFILE="$PWD/.bash_cwd_history"
        builtin history -c # clear memory
        builtin history -r #read from current histfile
}