m-chrzan.xyz
aboutsummaryrefslogtreecommitdiff
path: root/src/blog
diff options
context:
space:
mode:
authorMarcin Chrzanowski <marcin.j.chrzanowski@gmail.com>2020-10-06 00:41:34 +0200
committerMarcin Chrzanowski <marcin.j.chrzanowski@gmail.com>2020-10-06 00:41:34 +0200
commitd4f8b988a0af7c1429ce05ecfd058cefc9d184e8 (patch)
treee48d2ccd7c8e07407ad50c0e5d3a6f85fa9d603f /src/blog
parent3f2e2abf13983ac0ebc9fe75c869261e2723b904 (diff)
Publish vim keys in clis article
Diffstat (limited to 'src/blog')
-rw-r--r--src/blog/vim-keybindings-in-all-clis.html.erb71
1 files changed, 71 insertions, 0 deletions
diff --git a/src/blog/vim-keybindings-in-all-clis.html.erb b/src/blog/vim-keybindings-in-all-clis.html.erb
new file mode 100644
index 0000000..967079c
--- /dev/null
+++ b/src/blog/vim-keybindings-in-all-clis.html.erb
@@ -0,0 +1,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 &mdash; <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>