It’s been more than a year and a half now since I moved from Zsh to Fish, and I am still amazed by how smooth and simple it made my everyday workflow.
One little trick I’ve been taking with me from my Bash days is aliasing
_. There are a lot of commands I need to run via
sudo, and typing one
character instead of four makes it obviously quicker. But what if I already
started to write a command which needs proper privileges and forgotten about
Lukas Stabe fixed it for me, years
First, let’s add his
set -l prepend $argv
if test -z "$prepend"
echo "prepend_command needs one argument."
set -l cmd (commandline)
if test -z "$cmd"
commandline -r $history
set -l old_cursor (commandline -C)
commandline -C 0
commandline -i "$prepend "
commandline -C (math $old_cursor + (echo $prepend | wc -c))
Then, it’s only a matter of adding a key binding to
# Prepend sudo with C-s
bind \cs 'prepend_command sudo'
Now hitting Ctrl-s
anywhere in the command I am typing immediately
sudo at the beginning of the line.
prepend_command comes with the benefit of mimicking the
sudo plugin of Oh
Fish doesn’t support history
sometimes I just press Enter
before remembering to hit
, and that’s where I’d love some history expansion. I can hit
right after the malevolent Enter
, and have my
previous command with
sudo automatically prepended ready to go.
If you are accustomed to the Zsh key binding, you can have it in Fish too:
# Re-run previous command with sudo using ESC-ESC
bind \e\e 'prepend_command sudo'
Time savers, believe you me.