1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
title: Vim Keybindings in All CLIs
date: October 5, 2020 20:52
---
<p>
This post already lives as part of my
<a href='<%= path_to "blog_cheatsheets" %>'>cheatsheets</a>, but I think this is
one of those things that's worth putting out there.
</p>
<p>
<code>readline</code> is a library that can be used to add keyboard shortcuts to
various CLI programs. If you're a command line user on Linux or Mac, you're
probably already using it even if you don't know it — <code>bash</code>
keyboard shortcuts come from <code>readline</code>.
</p>
<p>
By default, <code>readline</code> uses Emacs-like bindings. If you were ever
confused by the weird key combinations in <code>bash</code>, this is where they
come from.
</p>
<p>
<code>readline</code> can be configured via an <code>~/.inputrc</code> config
file in your home directory.
</p>
<p>
There are several other libraries that provide similar functionality that are
used by certain CLI programs. <code>libedit</code> is a popular alternative with
a more permissive ("less free") license. Many Haskell projects use
<code>haskeline</code>, created specifically for <code>ghc</code>.
</p>
<h3>Configuring readline</h3>
<p>
All of the above libraries can be configured with config files in your home
directory. Here's what to put in your dotfiles to use vi keybindings by default:
<pre>
# ~/.inputrc
set editing-mode vi
# ~/.editrc
bind -v
# ~/.haskeline
editMode: Vi
</pre>
</p>
<p>
One other useful tip: if you want to just quickly switch <code>bash</code> to vi
keys, perhaps on an account that's temporary and you won't be putting effort
into customizing it, you can do so by running <code>set -o vi</code>.
</p>
<h3>Where this doesn't work</h3>
<p>
The above configs cover some of my most used CLI tools, like <code>bash</code>,
REPLs for Ruby, Python, Haskell. My biggest pain is that I haven't found a
satisfactory way of getting vi keybindings in a Node.js shell.
</p>
<p>
In some cases you can hack your way around a CLI not using any of the
<code>readline</code>-like libraries using a tool called <code>rlwrap</code>.
Unfortunately, it's not a silver bullet. I remember it working fine enough for
the OCaml REPL, but it doesn't play well with Node.
</p>
|