Wandering Thoughts archives


A few useful standard readline bindings

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.)

unix/ReadlineUsefulBindings written at 03:14:31; Add Comment

Page tools: See As Normal.
Login: Password:
Atom Syndication: Recent Pages, Recent Comments.

This dinky wiki is brought to you by the Insane Hackers Guild, Python sub-branch.