5

Suppose I do the following in zsh

cd ~
cd dir1
cd dir2
cd dir3
evince foo.pdf

zsh writes exactly the same in its history file.

Now my question is whether it is possible to have

cd ~
cd ~/dir1
cd ~/dir1/dir2
cd ~/dir1/dir2/dir3
evince ~/dir1/dir2/dir3/foo.pdf

in zsh history instead. I.e. that zsh remembers only full paths.

Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
student
  • 17,875
  • 31
  • 103
  • 169
  • Related (but different): [Can I have my shell history record how wildcards expanded?](http://unix.stackexchange.com/questions/12311/can-i-have-my-shell-history-record-how-wildcards-expanded) I don't think there's a way to change what's recorded in the history, but you can clumsily record extra stuff in parallel. – Gilles 'SO- stop being evil' May 07 '11 at 12:42
  • [Similar question in bash](http://unix.stackexchange.com/questions/36547/how-can-i-expand-a-relative-path-at-the-command-line-with-tab-completion) – Gilles 'SO- stop being evil' Apr 16 '12 at 23:28
  • 1
    A simpler and better alternative would be if zsh could record the PWD of every command in its history. So before executing a command from history it could prompt you if your current PWD dosesn't match the command's PWD – HRJ Nov 06 '14 at 11:06
  • 1
    Just found out that oh-my-zsh has a [plugin](https://github.com/robbyrussell/oh-my-zsh/tree/master/plugins/per-directory-history) for per-directory-history! – HRJ Nov 06 '14 at 11:14

1 Answers1

1

Not in a general way, because the shell has no way to know whether a given argument is a relative path or not. Of course it could use some heuristics (similar to the ones used for completion), but this might be too error-prone.

Adam Byrtek
  • 1,339
  • 9
  • 10
  • zsh history could log the full path before the command runs. The end user can then figure out what maps to a relative directory and what doesn't. – Alok Sep 21 '16 at 22:12
  • You could try arguments in a `$(realpath $arg)` subshell in a `zshaddhistory`-hook and if it fails you know its not a path. Add some initial regex validation and you're pretty close imo. – dza Nov 12 '22 at 12:08