fish: sudo: !!: command not found

7,925

Solution 1

I haven't found a inbuilt replacement for !! in Fish however you can write a function that allows you to keep using !!

Taken from this answer https://superuser.com/a/719538/226822

function sudo --description "Replacement for Bash 'sudo !!' command to run last command using sudo."
    if test "$argv" = !!
    eval command sudo $history[1]
else
    command sudo $argv
    end
end

Solution 2

The !! syntax is part of the bash history substitution feature, which fish does not implement. See this link for a discussion. Please feel free to weigh in there on what you think fish ought to do.

The most efficient (in terms of keypresses) replacement for sudo !! is up-arrow to recall the last history item, ctrl-A to move to the beginning of the line, then type sudo.

Solution 3

@ridiculous_fish's answer is outdated. The quickest way to achieve the equivalent to sudo !! would be Ctrl + p/up-arrow (whichever you prefer), and then Alt+s to prefix the command with sudo.

Solution 4

If you use !! only in the context of sudo !!, you can define a keybinding to ^s (CTRL+s) which prepends sudo to your command:

function prepend_command
  set -l prepend $argv[1]
  if test -z "$prepend"
    echo "prepend_command needs one argument."
    return 1
  end

  set -l cmd (commandline)
  if test -z "$cmd"
    commandline -r $history[1]
  end

  set -l old_cursor (commandline -C)
  commandline -C 0
  commandline -i "$prepend "
  commandline -C (math $old_cursor + (echo $prepend | wc -c))
end

This allows to type in any command and prepend sudo while typing or like in your case as a substitution of sudo !!

See the Ahti's comment on the github discussion

Share:
7,925

Related videos on Youtube

VaTo
Author by

VaTo

Updated on September 18, 2022

Comments

  • VaTo
    VaTo over 1 year

    I have fish installed in my Linux Mint DE. I really like how fish makes things easier and it looks so pretty although I haven't find a correct answer about why I can't execute:

    sudo: !!: command not found
    

    At first I tried to escape the exclamation signs with sudo !! but didn't work either. Does someone know why is this failing?

  • VaTo
    VaTo over 8 years
    Ok thanks for the clarification. Although, in order to run sudo with the last command by going through the history and then ctrl+A is not exclusively from fish, that's basically how bash works.
  • casey
    casey over 8 years
    @SaulOrtega he didn't claim it was exclusively from fish, just that that method will work in fish, while !! will not.
  • Nathaniel
    Nathaniel about 8 years
    You have two options .config/fish/config.fish or if you want to keep things separate put them in individual files under .config/fish/functions/newfunction1.fish newfunction2.fish etc
  • ossbuntu
    ossbuntu over 6 years
    Or even better, use Ctrl+P to get the previous command and then Ctrl+A to go to the beginning of the line. To go all-in, use Ctrl/Alt + B (Backward) along with Ctrl/Alt + F (Forward) to replace the arrow keys entirely, Ctrl+E to replace Home and Ctrl+H along with Ctrl+W to replace Backspace. These shortcuts work in all shells and in vim's insert mode as well.
  • frederickjh
    frederickjh about 5 years
    @vato In this can you would save the code above to ~/.config/fish/functions/sudo.fish The name of the file should match the function inside.
  • rien333
    rien333 over 3 years
    No idea why this has been downvoted, but there is now an even quicker way to emulate sudo !! , namely by simply pressing Alt + s, which will get the last command from your history and prefix it with sudo.