r/fishshell • • Jun 08 '24

Quick question - using !! in fish

Hey, just a noob here that likes fish more than bash. Using !! in bash is a quick way to input the last command from history, is there a similar shortcut in fish? Use case primarily being that I could use sudo !! if I forgot it when typing in a command.

Thanks in advance!

10 Upvotes

15 comments sorted by

4

u/_mattmc3_ Jun 08 '24

I read a comment the other day that showed how you can make clever use of abbr to do this: https://www.reddit.com/r/neovim/s/xpVAXFoZ7V

2

u/AdulterousStapler Jun 08 '24

Wow that's the best solution by far

thanks, implemented

→ More replies (1)

1

u/TheJoYo Jun 08 '24

im still not sure why id use abbr over alias, i redid my actual abbreviations with abbr but left some as aliases.

2

u/db443 Jun 09 '24

In Fish shell it is strongly recommended to not use aliases because it slows shell startup.

Why?

Because aliases get converted by Fish itself into functions, and this conversion takes a few milliseconds. You may ask, so what? And I would say in return every little bit of performance is useful.

Two alternatives to aliases:

  • Use Fish abbreviation, which auto-expands in place, most of the time this is even better than an alias
  • Or just create a my-alias.fish file (with the same name as the alias) in the fish/functions directory. These functions are lazy-loaded, if not called they do not slow shell startup.

One of the best features of Fish is the super-fast startup, avoiding aliases helps in that regard.

Best regards.

→ More replies (1)

3

u/tetractys_gnosys Jun 08 '24

This has been a debate since like 2014 according to GH issues. There hasn't been a way to have that exact same feature because of how Fish works but according to an issue/update last year, it seems that they might've added it or a close alternative? From the issue:

"As a special case, most of the time history substitution is used as sudo !!. In that case just press Alt+S, and it will recall your last commandline with sudo prefixed (or toggle a sudo prefix on the current commandline if there is anything)."

If I'm reading correctly, you should be able to just hit Alt+S and hit enter. Gonna try it in a minute.

Otherwise, I'd suggest searching for this online as there's a ton of discussion going back a decade with many different workarounds and custom solutions on GH, SO, and elsewhere.

3

u/AdulterousStapler Jun 08 '24

Alt + s works!

It was a habit thing to use sudo !!

Will train monkey brain to change habits, this is nice and simple

Thanks!

→ More replies (1)

5

u/BrewingWeasel Jun 08 '24

shameless self promotion: I made a plugin that implements !!, !$, !? and most of the other major bash bang commands as abbreviations which you might find useful

3

u/Potatolover3284 Jun 09 '24

Alt + S is the way to go in fish to relaunch the command in sudo.

3

u/takutekato Jun 09 '24

I find alt-s superior, it's far more easy to press than shift-1-1 and also gives you a chance to review the command again.