Ooh. This one was interesting. This is a two part story.
1) So, zsh has a "vi-mode" built-in, and it turn out is not as useful in practice.
2) The bindkey command has an option -s that lets you map two key sequence.
Say I want to have a keymap, ctrl+x + s, to make zsh type sudo for me. This is how it goes.
bindkey -s'^xs''sudo'
The ^ thingy, means ctrl. That would work. But it can be better, because you can have the "special" sequence in the other side too. We can get fancy.
bindkey -s'^xs''^asudo ^e'
^a means ctrl+a, so that would get you to the beginning of the line and type sudo for you. Then, ^e will get your cursor to the end of the line. I would say that works fine 95% of the time. The other 5%, is when the original position of the cursor wasn't the end of the line. Can we do better? Yes, remember the "vi-mode"? That works too.
Behold.
bindkey -s'^xs''#^asudo ^x^vf#xi'
#: Its literally #, nothing special here.
^a: Go to the beginning of the line
sudo: You know it already.
^x^v: Enter vi-cmd mode. That is ctrl+x + ctrl+v. Not the most intuitive thing in the world.
f#: Remember, we are in "vi-mode." In here the character f is a command, a search command. What does it search? The next character we type. So, this will search for a # and get the cursor to that position.
x: Deletes the character under the cursor, which should be #.
i: Get us back to insert mode.
And there you have it. Now you can use ctrl+x + s to add sudo to a command you're currently writing, and it will take you back to the place you started (that is of course if you don't any extra # in your command already).
While on the subject of crazy keybindings I present these.
I'm a fan of Open Source and have a growing interest in serverless and edge computing. I'm not a big fan of spiders, but they're doing good work eating bugs. I also stream on Twitch.
Ooh. This one was interesting. This is a two part story.
1) So,
zsh
has a "vi-mode" built-in, and it turn out is not as useful in practice.2) The bindkey command has an option
-s
that lets you map two key sequence.Say I want to have a keymap,
ctrl+x
+s
, to makezsh
typesudo
for me. This is how it goes.The
^
thingy, meansctrl
. That would work. But it can be better, because you can have the "special" sequence in the other side too. We can get fancy.^a
meansctrl+a
, so that would get you to the beginning of the line and typesudo
for you. Then,^e
will get your cursor to the end of the line. I would say that works fine 95% of the time. The other 5%, is when the original position of the cursor wasn't the end of the line. Can we do better? Yes, remember the "vi-mode"? That works too.Behold.
#
: Its literally#
, nothing special here.^a
: Go to the beginning of the linesudo
: You know it already.^x^v
: Entervi-cmd
mode. That isctrl+x
+ctrl+v
. Not the most intuitive thing in the world.f#
: Remember, we are in "vi-mode." In here the characterf
is a command, a search command. What does it search? The next character we type. So, this will search for a#
and get the cursor to that position.x
: Deletes the character under the cursor, which should be#
.i
: Get us back to insert mode.And there you have it. Now you can use
ctrl+x
+s
to addsudo
to a command you're currently writing, and it will take you back to the place you started (that is of course if you don't any extra#
in your command already).While on the subject of crazy keybindings I present these.
You can guess what they do.
EDIT: By the way,
fish
users already have something like these. I believeAlt+p
will add| less
to your command. Don't remember what others they have.