Thursday, 22 August 2013

Getting efficient with Readline

GNU Readline is a utility used by different programs to have a consistent command-line interface.
A few of these programs :
  • Bash (zsh does not use readline)
  • some Python interactive interpreters
  • rlwrap
By default, the end user interface of readline comes with really interesting keybindings. They are documented but the page layout is not really user-friendly so here's a summary :


TypeNameEmacs shortcutActionObjectDirectionEquivalent Windows/Gnome/KDE
Mode emacs-editing-mode^ Ctrl+eChangemodefrom vi to emacs (set -o emacs)
vi-editing-mode^ Ctrl+⎇ Alt+jChangemodefrom emacs to vi (set -o vi)
Move backward-char^ Ctrl+bMove the cursorone characterto the left
forward-char^ Ctrl+fMove the cursorone characterto the right
backward-word⎇ Alt+bMove the cursorone wordto the left^ Ctrl+
forward-word⎇ Alt+fMove the cursorone wordto the right^ Ctrl+
beginning-of-line^ Ctrl+aMove the cursorto the start of the line
end-of-line^ Ctrl+eMove the cursorto the end of the line
exchange-point-and-mark^ Ctrl+x ^ Ctrl+xMove the cursorto the start of the line/to original position
Swaptranspose-char^ Ctrl+tSwapthe characterbefore the cursor with current position
transpose-word⎇ Alt+tSwapthe wordbefore the cursor with current word
Searchcharacter-search^ Ctrl+]Searchthe characterand move to next occurence
character-search-backward^ Ctrl+⎇ Alt+]Searchthe wordand move to previous occurence
Casedowncase-word⎇ Alt+lLowercasethe wordfrom cursor
upcase-word⎇ Alt+uUppercasethe wordfrom cursor
capitalize-word⎇ Alt+cCapitalizethe wordfrom cursor
Killdelete-char^ Ctrl+dDeletethe character
unix-word-rubout^ Ctrl+wDeletethe wordleft until after the previous word boundary
backward-kill-word⎇ Alt+⌫ BackspaceDeletethe word
kill-word⎇ Alt+dDeletethe wordright until before the next word boundary
unix-line-discard^ Ctrl+uDeleteeverythingfrom the cursor to the start of the line
kill-line^ Ctrl+kDeleteeverythingfrom the cursor to the end of the line
Yankyank^ Ctrl+yYankthe kill ringat point
yank-pop⎇ Alt+yYankthe kill ringat point (going through older content)
Undoundo^ Ctrl+_ (or ^ Ctrl+x ^ Ctrl+u)Undolast change
revert-line⎇ Alt+rUndoall changesmade to this line
Completioncomplete↹ TabAttempt completion

possible-completions⎇ Alt+?List completions
insert-completions⎇ Alt+*Insert completions
History previous-history^ Ctrl+pMove in historyone lineup
next-history^ Ctrl+nMove in historyone linedown
beginning-of-history⎇ Alt+<Move in historyall linesto the top
end-of-history⎇ Alt+>Move in historyall linesto the bottom
reverse-search-history^ Ctrl+rIncremental searchbackward, going up the history
forward-search-history^ Ctrl+sIncremental searchforward, going down the history

^ Ctrl+jStopincremental search
abort^ Ctrl+gStopincremental searchand restore original line
non-incremental-reverse-search-history⎇ Alt+pNon-incremental search
backward, going up the history
non-incremental-forward-search-history⎇ Alt+nNon-incremental search
forward, going down the history
yank-nth-arg^ Ctrl+⎇ Alt+yYankfirst argumentof the previous command
yank-last-arg⎇ Alt+. or ⎇ Alt+_Yanklast argumentof the previous command
Refresh clear-screen^ Ctrl+lClearthe screen
redraw-current-lineNoneRefreshthe current line

⎇ Alt is also called "meta" prefix and written "M-" or "\M-".
^ Ctrl is also called "control" prefix and written "C-" or "\C-".

Loose convention is that Control operates on characters while Meta operates on words.

Numeric arguments can be passed to readline commands. Type meta digits (⎇ Alt+digit, digit can be - for negative arguments) and then the command. Once the first meta digit is typed, the reminder (if any) can be typed normally. For instance, ⎇ Alt+10^ Ctrl+d will delete the next 10 characters.
Numeric arguments can also be used to repeat normal inputs. For instance, ⎇ Alt+10k inserts "kkkkkkkkkk". If the input is a digit, ^ Ctrl+v can be used to insert it : ⎇ Alt+10^ Ctrl+v2 inserts "2222222222".

These bindings can be changed with a configuration file called inputrc.

Also, in Bash, the bind builtin command can be used to display (bind -P) or modify the readline key bindings. The read builtin command can be used to see the string corresponding to special key inputs. For instance, F5 gives "^[[15~" and then bind '"\e[15~":"top\C-m"' would map F5 to "top\n"

No comments:

Post a Comment