What if you could use Emacs to edit the command line? Wouldn’t that be great? Well, of course, you can. Not only can but almost certainly do. I’m not talking about invoking Emacs from Bash with Ctrl+x Ctrl+e; I’m talking about readline
.
If you have any experience at all with the Bash shell you probably know that you can move to the beginning of the command line with Ctrl+a and to the end with Ctrl+e just like in Emacs. That’s the Readline library in action. It turns out, though, that the library is much richer than just those two commands.
If you look at the Readline man page or library manual, you’ll see that it actually supports an astounding number of Emacs commands, mostly bound to the familiar key sequences. You can transpose characters or words, move or delete characters or words, mark and operate on regions, and even record keyboard macros. And just like in Emacs, you can bind your own key sequences to the commands. You can even configure it to use Vim key sequences.
If you’d like to find out more about Readline and discover some of the things it can do, Sinclair Target over at Two-Bit History has a very nice post on the things you didn’t know about GNU readline. Go read it right now. You’ll be glad you did.
I’ve been using Bash for decades and have always made use of the Ctrl+a / Ctrl+e trick and a couple of the other movements but I didn’t realize how much of the Emacs (or Vim) command set is implemented. It truly is, as Target says, Emacs for a single line of text.