In bash, history -p does history expansion on its argument; What is the alternative in zsh?
2 Answers
I'm not aware of any, but see the histverify option which changes the behaviour of history expansion so that when you press enter on a line that contains history operators, the expansion is performed in the editor buffer without running the command.
$ setopt histverify $ echo !!Enter $ echo setopt histverifyEnter setopt histverify
Also note that in zsh like in csh (where history expansion comes from in the late 70s) history modifiers (like :h, :r, :A...) can also be applied to parameter expansions ($var:h...).
And the whole history is accessible with the $history special associative array.
Keys of that associative arrays are the history numbers, and contrary to regular associative arrays where the order is undefined, $history[@] is sorted in descending order on the numerical value of the key.
So ${history:0:1} is the last history entry. So, you can use parameter expansion operators to get the same result as with history expansion operators. For instance: !$:h, could be ${${(z)history:0:1}[-1]:h}
In bash you can expand a command by doing this:
history -p !23 So if in 23 you have ls -l the, return will be:
ls -l In zsh i am able to reproduce a similar (but not identical) behavior by doing this:
history 23 23 | xargs -n1 | tail -n+2 - 1ITYM
history -p '!23'. Inzsh, just useprint -r -- $history[23]Stéphane Chazelas– Stéphane Chazelas2018-09-03 15:02:03 +00:00Commented Sep 3, 2018 at 15:02 - I tried but nothing happens =(. Try history -p !23 instead of history -p '!23'... for what is really that ? =(Luciano Andress Martini– Luciano Andress Martini2018-09-03 15:04:01 +00:00Commented Sep 3, 2018 at 15:04
- Thanks, but I need more sophisticated behavior; I wanted to run
history -p '^(#b)E(?)^E${(l:2::0:)$((match[1]+1))}'. I wrote that function usingfcin the end.HappyFace– HappyFace2018-09-04 17:49:41 +00:00Commented Sep 4, 2018 at 17:49 - I cant imagine the results you want to got. I just received "History Expansion Failed" (in bash). Can you post in you question what you have, and what exactly you want to get or do? Thank you. Maybe i have a easy solution!Luciano Andress Martini– Luciano Andress Martini2018-09-04 17:54:01 +00:00Commented Sep 4, 2018 at 17:54