If you’ve used the terminal for any amount of time, you’ve probably run a command, only to realize you needed to include sudo. You could always hit the up arrow ꜛ and type sudo at the beginning of the line, but you many know that “!!” will repeat the last entry.
I recently noticed I wasn’t getting this functionality with my fish shell. At first I figured there was something wrong,
but if you consult the fish documentation however, you’ll see fish handles history a little differently than bash does.
You can access the fish documentation by typing “help” into a fish shell. We’ll take a look at how fish handles history later on,
first let’s get fish to implement “!!”.
To get “!!” to work we’ll add it into fish’s configuration file. To do that we’ll open it using nano.
nano ~/.config/fish/config.fish
Once we’re here, we can add the below function between the comment(# denotes a comment), and the “end” line. You can use SHIFT + INS to paste in nano.
function bang_bang
commandline -i (history -n 1)
end
bind !\! 'bang_bang'
Once finished you can press CTRL + o to save your changes. Then exit nano with CTRL + x. We can use source so we don’t have to reboot or start a new shell to test our new function.
source ~/.config/fish/config.fish
If you’re familiar with scripting, you’ll know what a shebang line is(#!/bin/bash). Bang refers to the exclamation mark. So in this case we can call the function bang_bang. We’ll use commandline -i to write to our terminal, and the command we want is obtained with history -n 1. Finally we map, or bind, the function to “!!”.
Now that we’ve restored the bash way of repeating a command, let’s learn some new tricks since we’re using a new shell. In fish, if you just want to run the last command again, but adding sudo, press ALT + s. Alternatively you can use ALT + ꜛ to go through the history.