(One)-minute geek news

Group talks, Laboratoire de Chimie, ENS de Lyon

Readline features: Killing and Yanking


Readline is a utility that reads your line when you type in a terminal. But most importantly, it offers editing capabilities while you are typing! Many features are available (by default, the commands are similar to those of Emacs), let us just focus on a very convenient group of such: Killing and Yanking.

Backward cutting

In the Emacs/Readline jargon, cutting is called killing with kill-ring push. Killing simply refers to deleting (a string), and that deleted text is pushed (i.e. saved) on top of a stack called the kill-ring (this is the equivalent of the usual clipboard, except that you can pile up multiple items).

The selected features (with default binding) are:

  • Ctrl-u (unix-line-discard): cut the beginning of the current line (from line beginning to cursor position).
  • Ctrl-w (unix-word-rubout): cut the beginning of the current word (from word beginning to cursor position). If the cursor is between two words, the previous word is cut.
  • Ctrl-y (yank): yank (i.e. paste) the top of the kill-ring at the cursor position. It is very much what we expect from a usual paste.
  • Bonus: ESC-y (yank-pop): Rotate the kill-ring, and yank the new top. In other words, cycle through your kill-ring for previously cutted strings. Note that this can only be used after a yank or yank-pop.
Beware that readline has a peculiar way to handle the kill-ring: if you keep cutting multiple items continuously they will be merged in the kill-ring.

Using the Ctrl-u/Ctrl-y combination you can start typing a command, then you realize you need to check something before continuing to type your command, so you hit Ctrl-u, you check what you have to, and resume your typing with Ctrl-y. Trust me, this is far more useful in daily-life than what it sounds like...

Forward deleting

Similarly to the previous features, the following features are deleting parts of your current command line. Instead of handling what is before the current cursor position, they deal with what is after it. However, they do not save/push to the kill-ring:

  • Ctrl-k (kill-line): delete the end of the current line (from cursor position to line ending). Useful for changing parameters of a previous command.
  • ESC-d (kill-word): delete the end of the current word (from cursor position to word ending). If the cursor is between two words, the next word is deleted.
Beware that those commands do not update the kill-ring. Your deletions will be lost. With no way to retrieve them, ever. Well... really?

Last word: undo

Remember that, thanks to readline again, you can always undo your changes while typing a new command!

To undo, simply hit Ctrl-_ (or the Emacs-inspired Ctrl-x Ctrl-u) and rejoice!

Unfortunately, I did not find a corresponding redo command, so be extra careful when undoing stuff, as they will likely vanish once undone... (the readline undo is incremental, and differs from the Emacs-style undo where you can undo your undo!)

References

man readline
man bash