fish: sudo: !!: command not found
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 !!
Related videos on Youtube
VaTo
Updated on September 18, 2022Comments
-
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?
-
Angel Todorov over 8 yearsSee also superuser.com/q/719531/4714
-
-
VaTo over 8 yearsOk 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 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 about 8 yearsYou 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 over 6 yearsOr even better, use
Ctrl+P
to get the previous command and thenCtrl+A
to go to the beginning of the line. To go all-in, useCtrl/Alt + B
(Backward) along withCtrl/Alt + F
(Forward) to replace the arrow keys entirely,Ctrl+E
to replaceHome
andCtrl+H
along withCtrl+W
to replaceBackspace
. These shortcuts work in all shells and in vim's insert mode as well. -
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 over 3 yearsNo 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.