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?


4 Answers 4


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]
    command sudo $argv
  • Nice answer, However, where do I have to write it so I can make the function permanent?
    – VaTo
    May 11, 2016 at 17:19
  • 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
    – Nathaniel
    May 14, 2016 at 3:55
  • @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. Apr 23, 2019 at 8:51

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.

  • 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.
    – VaTo
    Oct 13, 2015 at 1:41
  • 4
    @SaulOrtega he didn't claim it was exclusively from fish, just that that method will work in fish, while !! will not.
    – casey
    Oct 13, 2015 at 3:01
  • 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.
    – ossbuntu
    Feb 3, 2018 at 15:06

@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.

  • 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.
    – rien333
    Sep 3, 2020 at 9:48

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

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

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.