12

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?

2

5 Answers 5

14

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" = !!
        echo sudo $history[1]
        eval command sudo $history[1]
    else
        command sudo $argv
    end
end
3
  • 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
10

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.

5
  • 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
  • Most efficient way these days is Alt+s, to prefix the last (or current) command with the installed privilege escalator (sudo/doas/please).
    – Victor
    Jan 8 at 9:15
  • @ossbuntu, Ctrl+e would be a replacement for End, not Home.
    – Victor
    Jan 8 at 9:15
6

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

1
  • 1
    You don't have to recall the last command anymore, you can just do Alt+s immediately and it'll replace the empty command line with the last command, prefixed with sudo/doas/please.
    – Victor
    Jan 8 at 9:16
1

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

0

Simply press Alt+s and boom you're done in one step! The last command is freshly inserted right after the sudo command. Thanks DT!

1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .