A few useful standard readline bindings

December 30, 2016

One day, I'm not sure exactly when, I stumbled over the standard readline binding of M-. for 'yank-last-arg'. The description you can find in your local readline manpage may be a little abstract, so here's the simplified version: M-. inserts whatever was the last word on the previous command line into your current command line.

Did you perhaps type:

$ ls *some*[Cc]omplex*pattern

and now you want to grep through files matching that pattern? Easy; type 'grep thing M-.' and you'll wind up with 'grep thing *some*[Cc]omplex*pattern'.

(You can also use M-_, according to the manpage.)

This turns out to not be the only potentially useful standard readline binding that I wasn't aware of, so here's my current little collection.

  • M-C-y is 'yank first argument'. With an argument, both it and M-. are 'yank nth argument', but I'm not sure I'd ever do that instead of some form of line editing.

    (Providing numeric arguments is a bit awkward in readline and we're into the same 'counting words' problem territory that I have with Vi. It's mentally easier to reshape a command line than count out that I want the third, fourth, and sixth argument.)

  • C-_ or C-x C-u is incremental undo.
  • M-r reverts the line to its original state. This is probably mostly useful if I've accidentally modified a line in the history buffer and want to discard those changes to revert it to its pristine as-originally-typed state.

In Bash specifically (although not necessarily other things that use readline), you can force certain completions regardless of bad attempts to be clever. These are:

  • M-/ for filename completion
  • M-! for command name completion
  • M-$ for variable name completion
  • M-@ for hostname completion
  • M-~ for user name completion

All have C-x <char> versions that list all the possible completions. The keys for these are reasonably related to what you're forcing a completion for, so I have at least some chance of remembering them in the future.

(There are probably other readline bindings that are useful, and for that matter you can rebind things with a .inputrc. Advanced users of readline probably do all sorts of things there, especially with conditional constructs and variables and so on.)

Comments on this page:

By Icarus at 2016-12-30 12:48:22:

M-. is really really useful.

Two things not brought out in your article. First you can use numeric arguments so M-1 M-. will pull up the first argument of the previous command rather than the last one.

Second you can repeat it, so M-. M-. will bring up the last argument of the command before last (it will show you the last argument of the previous command after the first M-., then replace it when you type the second M-.)

You can combine this so M-2 M-. M-. M-. will bring up the second parameter of the third previous command.

yank-last-arg alone is enough to make me compile xterm or urxvt for systems where the terminal emulator makes it hard to send meta-characters (and typing ESC . ESC , Esc . is much harder than typing Alt . . .)

Written on 30 December 2016.
« One reason why Go's increment and decrement statements are good to have
Ubuntu's AppArmor system is reasonably and pleasantly non-obnoxious »

Page tools: View Source, View Normal, Add Comment.
Login: Password:
Atom Syndication: Recent Comments.

Last modified: Fri Dec 30 03:14:31 2016
This dinky wiki is brought to you by the Insane Hackers Guild, Python sub-branch.