'How do I enable "!!" in fish?
I often used $ sudo !! while using bash. After switching to fish, I noticed this no longer works. Due to search engines not supporting search for special characters, this has been particularly difficult to figure out on my own. I presume I just need to add/change one line in the config, but I don't know what that would be.
Solution 1:[1]
Oldish question, but I'm surprised that there's been no mention of fish shell's "pre-pend sudo" keybinding Alt+s.
With the default, out-of-the-box keybindings, sudo !! can be accomplished with either:
- Alt+. followed by Alt+s
- or ? followed by Alt+s, but that does move your finger a bit far from the "home row" for those that care (and I do).
The first key-chord repeats the previous command, then Alt+s prepends sudo to it.
Edit: With fish 3.2 and later, one of my favorite new features is that you don't even need to retrieve the previous line via Alt+.. Just Alt+s with no other text typed at the prompt will result in the prompt being automatically populated with sudo + the last command typed.
Solution 2:[2]
There are some ways of implementing this using shell hacks listed at https://github.com/fish-shell/fish-shell/wiki/Bash-Style-Command-Substitution-and-Chaining-(!!-!%24-&&-%7C%7C)
For example:
function bind_bang
switch (commandline -t)
case "!"
commandline -t $history[1]; commandline -f repaint
case "*"
commandline -i !
end
end
function bind_dollar
switch (commandline -t)
case "!"
commandline -t ""
commandline -f history-token-search-backward
case "*"
commandline -i '$'
end
end
function fish_user_key_bindings
bind ! bind_bang
bind '$' bind_dollar
end
funcsave bind_bang bind_dollar fish_user_key_bindings
Solution 3:[3]
The easiest way is to install the Fisher package manager; and then install the package !!, e.g.:
$ fisher install oh-my-fish/plugin-bang-bang
p.s.
Not only do you get back the use of !! (last command used) but also !$ (last command arguments.)
You could also use the plugin through the older installer omf.
Why reinvent the wheel when you have an easy to use a package manager like Fisher.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Zanchey |
| Solution 3 | Jorge Bucaran |
