Readline

Screwtapello edited this page Jun 19, 2018 · 3 revisions

GNU Readline is a library that prints a prompt and waits for you to type something and press Enter. It's most famously used by bash for command-line editing, but is also used by many other programs like the PostgreSQL command-line client and the Python interactive interpreter. Readline (as you might expect, being a GNU project) defaults to and extensively supports Emacs style keybindings, but also supports very extensive configuration for custom keybindings.

Note: If you want to use Real Kakoune to edit a command-line, you can bind a key to edit-and-execute-command (<c-x><c-e> in Emacs mode, not bound by default in Vi mode) which opens the current command in $EDITOR. For bash specifically, there's also the fc command.

Because Kakoune's editing model is very different from the ones Readline was designed to support, it's not possible to implement it exactly. However, if you stick the following into ~/.inputrc, you'll get something that feels at least a little familiar:

# Make Readline always default to vi-emulation, then we'll lay our Kakoune
# bindings on top of that.
set editing-mode vi
set keymap vi

# Kakoune's unusual select-and-move editing model means that most Kakoune
# keybindings correspond to a "select" and a "move" action in Readline terms.
# However, Readline does not support binding a key to a sequence of actions,
# only to a single action, or a sequence of other keys.
# Therefore, we bind weird, unused key-sequences to the individual actions we
# want to use, and make our Kakoune-style bindings invoke sequences of them.
# I chose <c-a><c-x> as the common prefix, because Kakoune does not map them
# to anything by default, let alone in sequence.

# Weird unlikely-to-be-used mappings for the functions we want to use
"\C-a\C-xm": set-mark
"\C-a\C-xh": backward-char
"\C-a\C-xl": forward-char
"\C-a\C-xw": vi-forward-word
"\C-a\C-xW": vi-forward-bigword
"\C-a\C-xb": vi-backward-word
"\C-a\C-xB": vi-backward-bigword
"\C-a\C-xe": vi-end-word
"\C-a\C-xE": vi-end-bigword
"\C-a\C-xf": vi-char-search
"\C-a\C-xt": vi-char-search
"\C-a\C-xF": vi-char-search
"\C-a\C-xT": vi-char-search
"\C-a\C-x0": beginning-of-line
"\C-a\C-x$": end-of-line
"\C-a\C-xd": kill-region
"\C-a\C-xy": copy-region-as-kill

# Kakoune keybindings defined as concatenations of the above functions
"h": "\C-a\C-xm\C-a\C-xh"
"H": "\C-a\C-xh"
"\M-h": "\C-a\C-xm\C-a\C-x0"
"\M-H": "\C-a\C-x0"
"gh": "\C-a\C-x0\C-a\C-xm"
"Gh": "\C-a\C-x0"
"GH": "\C-a\C-x0"
"l": "\C-a\C-xm\C-a\C-xl"
"L": "\C-a\C-xl"
"gl": "\C-a\C-x$\C-a\C-xm"
"Gl": "\C-a\C-x$"
"GL": "\C-a\C-x$"
"\M-l": "\C-a\C-xm\C-a\C-x$"
"\M-L": "\C-a\C-x$"
"w": "\C-a\C-xm\C-a\C-xw"
"W": "\C-a\C-xw"
"\M-w": "\C-a\C-xm\C-a\C-xW"
"\M-W": "\C-a\C-xW"
"b": "\C-a\C-xm\C-a\C-xb"
"B": "\C-a\C-xb"
"\M-b": "\C-a\C-xm\C-a\C-xB"
"\M-B": "\C-a\C-xB"
"e": "\C-a\C-xm\C-a\C-xe"
"E": "\C-a\C-xE"
"\M-e": "\C-a\C-xm\C-a\C-xE"
"\M-E": "\C-a\C-xE"
"f": "\C-a\C-xm\C-a\C-xf"
"F": "\C-a\C-xf"
"\M-f": "\C-a\C-xm\C-a\C-xF"
"\M-F": "\C-a\C-xF"
"t": "\C-a\C-xm\C-a\C-xt"
"T": "\C-a\C-xt"
"\M-t": "\C-a\C-xm\C-a\C-xT"
"\M-T": "\C-a\C-xT"
"x": "\C-a\C-x0\C-a\C-xm\C-a\C-x$"
";": "\C-a\C-xm"
"\M-;": exchange-point-and-mark
"d": kill-region
"gg": beginning-of-history
"gk": beginning-of-history
"gj": end-of-history
"ge": end-of-history 
"c": "\C-a\C-xdi"
"y": "\C-a\C-xy"
"P": "\C-a\C-xhp"

Known problems

  • We emulate Kakoune's selection system on top of Readline's Emacs-style regions. However, Emacs' convention is that the character under the cursor is not part of the region, while Kakoune says that the character under the cursor is part of the selection. There's probably no good way around that.
  • Readline's standard vi keybindings map f, t, F and T all to the same command, vi-char-search, which presumably decides what to do based on what key it was bound to. Surprisingly, Kakoune-style F and T bindings work, but <a-f> and <a-t> bindings do not.
  • Readline does not highlight the "region", what Kakoune would call the "selection", making this even harder to use than the bugginess would suggest.

See what mode you are in

set show-mode-in-prompt on
set vi-ins-mode-string \1\e[6 q\2
set vi-cmd-mode-string \1\e[2 q\2

From https://stackoverflow.com/a/42107711/165544

You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.