jimhester / per-directory-history

Per directory history for zsh, as well as global history, and the ability to toggle between them with ^G.
zlib License
260 stars 36 forks source link

Return to the directory that ran the command? #28

Closed biocyberman closed 5 years ago

biocyberman commented 5 years ago

I ran commands and jumped everywhere. Now I use ctrl - g and ctrl - r to search back history. When I find a command, I want to jump to the correct directory to run that command. For example:

cd $HOME
mkdir testdir
cd testdir
mkdir -p testsubdir
cd $HOME

At this point, I enable search global and search for "testsubdir" If I run mkdir -p testsubdir again under $HOME, it will create testsubdir under $HOME. This is not what I want. I want to know that I ran the command under testdir to work correctly. How do I find that out?

piegamesde commented 5 years ago

You could hack in this functionality for cd, but I do not think this is possible at a larger scale.

biocyberman commented 5 years ago

If my memory serves me correctly, this plugin mirror directory structure under ~/.per_directory_history or something like that. So in the example, I will have ~/.per_directory_history/$HOME/testdir/testsubdir`. Can we make use of this fact and print the directory for the command?

If that is not the case, somehow I have to record ZSH History with $PWD in addition to the command itself to make this work. Should there be a hack on how HISTORY format at ZSH side?

biocyberman commented 5 years ago

Thanks to this answer on stack overflow, I modified this function:

function _per-directory-history-addhistory() {
  # respect hist_ignore_space
  if [[ -o hist_ignore_space ]] && [[ "$1" == \ * ]]; then
      true
  else
      print -Sr -- "${1%%$'\n'}"
      fc -p $_per_directory_history_directory
  fi
}

To this version:

function _per-directory-history-addhistory() {
  # respect hist_ignore_space
  if [[ -o hist_ignore_space ]] && [[ "$1" == \ * ]]; then
      true
  else
      print -Sr -- "${1%%$'\n'} ### ${PWD}"
      fc -p $_per_directory_history_directory
  fi
}

And it works perfectly the way I want :)