Search code examples
shellfish

Populate command line buffer with last command in case of error


I'm trying to implement a way to put back the previous command in case of an error in Fish. However the following always results in an empty command line.

function __process_last_cmd -e fish_postexec
  set -lx error $status
  set -lx cmd (commandline)
  if test $error -ne 0
    commandline $cmd
  end
 # history delete -C --exact $cmd
end

(There is also a secondary operation where the command is de-duped from history, which I've left in the code in case it's relevant.)


Solution

  • The fish_postexec event is documented to provide the command line to the function as the first argument, so use

    set -l cmd $argv[1]
    

    https://fishshell.com/docs/current/commands.html#function