11

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?

Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
VaTo
  • 3,071
  • 3
  • 18
  • 47

5 Answers5

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
Nathaniel
  • 430
  • 4
  • 9
  • Nice answer, However, where do I have to write it so I can make the function permanent? – VaTo May 11 '16 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 '16 at 03: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. – frederickjh Apr 23 '19 at 08: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.

ridiculous_fish
  • 1,661
  • 10
  • 15
  • 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 '15 at 01: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 '15 at 03:01
  • Or even better, use `Ctrl+P` to get the **p**revious command and then `Ctrl+A` to go to the beginning of the line. To go all-in, use `Ctrl/Alt + B` (**B**ackward) along with `Ctrl/Alt + F` (**F**orward) 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 03 '18 at 15:06
4

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

rien333
  • 613
  • 4
  • 15
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

orzechow
  • 326
  • 2
  • 6
-1

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!

AdminBee
  • 21,637
  • 21
  • 47
  • 71
Nubco
  • 1