Is there a way to get the previous command (with its parameters) in Fish shell? I.e. not its exit code/status but the actual command given.
My use case is that I'd like to know the previous command in a later command when combining them with a logical AND operator, like e.g.
first_cmd params; and my_func
That is, I'd like to use the string "first_cmd params" in my Fish function my_func
.
The long story:
My actual use case is that I currently have a Fish function sr
(for "say ready") like this (running on macOS):
function sr if test $status -eq 0 say -v Samantha 'Operation finished'; else say -v Samantha 'Operation failed'; endend
I use that to get an audible notification about some long-running operation finishing or failing so that I can leave some commands running in a terminal, do other stuff while waiting them to finish, and then jump back to the terminal once they're ready. For example:
docker-compose build; and sr; and docker-compose up
This works, but I started thinking it would be even better if the notification mentioned the command that had finished. I.e. instead of saying "Operation finished" it would say "docker-compose build finished" in the above example.
I tried this with
say -v Samantha $history[1] 'succeeded';
and
say -v Samantha (history --max 1) 'succeeded';
(which I believe are equivalent) but that of course doesn't do what I want, as history
uses lines of commands and does not separate commands on the same line etc.
I guess one option could be to change my custom function to take the command as a parameter, like
sr "docker-compose build"
which would run the command and then say out the result. But if there's a way to get the previous command (as a string), that'd be a simpler change.
Other approaches to achieve what I want are of course welcome also.