2

I perform a recursive search to find a command I executed few days back and I want to run it. However that command should run from the path from where I ran it previously from and not where I currently am.

Is it possible for zshto figure this out and change the command to the full path and run from my current path? or maybe fork a process switch to the original path run it and come back?

  • If zsh could record the PWD for every path, another use-case for it would be to filter the history search based on current working directory. – HRJ Nov 06 '14 at 11:08
  • Just found out that Oh-my-zsh has a plugin that records per-directory history! – HRJ Nov 06 '14 at 11:13

2 Answers2

2

The original directory where the command was executed is not recorded in the history.

You can set a hook to be executed when the name of the command you try to execute is not found: command_not_found_handler. But unless you have a good idea of what previous working at that time must have been, this isn't going to get you far.

Related reading: zsh history - full path; Can I have my shell history record how wildcards expanded?

0

In most cases, this would actually be an undesirable action. Often when I rerun command out is specifically to run them in my current context. Running a history command and having it act on something other than the current folder could even have unexpected and unwanted side effects.

I would suggest pursuing a different approach. First identify what command need to be specifically tin from a certain location, then ask yourself why they are like that and fix them using standard patterns.

  • If they need to load libraries out resources that are at paths relative to the executable, fix them so that they know where to find these using things like dirname and $0.

  • If they need data, consider passing them arguments that show where to find the data that are not relative so they can be so reused.

  • If the source of the issue isn't fixable by you, consider writing small shell functions or wrapper scripts that setup the environment for the executable independently of whatever factors vary on your system.

Caleb
  • 70,105