From Electron Cloud
Jump to: navigation, search
(disclaimer, more problems)
 
(2 intermediate revisions by the same user not shown)
Line 1: Line 1:
*Problems:*
+
'''Problems:'''
- Hate it when I can't find a useful command which was last executed too long ago and has "expired" from bash history
+
* 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)
+
* 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)
+
* but, I still want a global time-ordered history too (at least approximately)
  
 
There is a partial solution [http://www.compbiome.com/2010/07/bash-per-directory-bash-history.html here] upon which this is based.
 
There is a partial solution [http://www.compbiome.com/2010/07/bash-per-directory-bash-history.html here] upon which this is based.
  
*Solution:*
+
'''Solution:'''
- redefine "cd" to save per-directory history inside the directory itself, and also append that to an ever-expanding ~/.bash_history
+
* 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
+
* 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 <code>history | grep</code> searches
+
* redefine "history" to get the global history so that I can do comprehensive <code>history | grep</code> searches
- add a "chistory" command to get CWD-specific history only
+
* 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):
 
If want to try it, stick this in your ~/.bashrc (and smoke it, or something):
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>
 +
 +
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?)