Skip to content

Instantly share code, notes, and snippets.

@b-

b-/!!

Forked from cmlsharp/!!
Last active October 20, 2022 14:35
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save b-/981892a65837ab0a387e to your computer and use it in GitHub Desktop.
Save b-/981892a65837ab0a387e to your computer and use it in GitHub Desktop.
# Add this to your ~/.config/fish/config.fish
# Syntax:
# To just rerun your last command, simply type '!!'
# This forked version supports "sudo !!" via two functions.
function !!
eval $history[1] $argv
end function
function sudo
if test $argv
if test $argv = "!!"
eval /usr/bin/sudo $history[1]
else
eval /usr/bin/sudo $argv
end
end
end
@cmlsharp
Copy link

I like this! This works much better than my original function other than mine was slightly more flexible in that I could easily add more terms that were to be prepended to the previous command. However, this is much cleaner. Kudos

@tuxitop
Copy link

tuxitop commented Oct 26, 2015

testing for $argv[1] would be more specific, and it also fixes a bug.

@OJFord
Copy link

OJFord commented May 15, 2016

Would be a good idea to modify the command in history, replacing !! with the original command.

As it stands:

~> echo ''

~> !!

~> sudo !!
sudo: !!: command not found

A more realistic case when this might occur would be repeated execution of the last command - if you can only do it once then you'd be better off with up+return in your muscle memory than use this !! function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment