From Electron Cloud
Jump to: navigation, search
(correct some problems I found)
(disclaimer, more problems)
 
Line 33: Line 33:
 
}
 
}
 
</pre>
 
</pre>
 +
 +
Disclaimer: I'm not responsible for your lost history!  Please make a backup of your ~/.bash_history first, just in case.
 +
 +
'''Known remaining problems:'''
 +
* ~/.bash_history will have accelerating growth because every time you cd out of a directory, it will concatenate the _entire_ CWD history into ~/.bash_history.  This could be corrected with another temp file and a marker to separate new history from old history, some more scripting etc.
 +
* If you exit from the shell it will only save the CWD history; the concatenation into ~/.bash_history only happens with the "cd" alias.  (Is there an "onExit" hook of some sort I could use?)

Latest revision as of 09:27, 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
}

Disclaimer: I'm not responsible for your lost history! Please make a backup of your ~/.bash_history first, just in case.

Known remaining problems:

  • ~/.bash_history will have accelerating growth because every time you cd out of a directory, it will concatenate the _entire_ CWD history into ~/.bash_history. This could be corrected with another temp file and a marker to separate new history from old history, some more scripting etc.
  • If you exit from the shell it will only save the CWD history; the concatenation into ~/.bash_history only happens with the "cd" alias. (Is there an "onExit" hook of some sort I could use?)