Go home

Using Vi

During my switch to OpenBSD, I also switched from using the vis editor (not to be confused with vis(1)) to vi(1). The primary difference between vis and vi is that vis is extensible. vis's full Lua API allows you to extend the editor however you want, writing custom commands, integrating it with other programs, et cetera. In vi, you get what you're given, and you suck it up and use it. That's not to say that vi is an inflexible editor, and most of functionality that I implemented myself via lua in vis are available right in the editor. How, you ask? By using not just vi, but by using the whole UNIX environment, and the tools provided by vi to access it! The most common function I use is "! ". By hitting the exclamation mark, followed by a motion, and then a command, the given command is executed with the stdin being the contents of the motion, and the content of the motion is replaced by stdout. For example, if you wanted to sort the next 10 lines in-place, you can type "!10j sort", or to reflow the current paragraph, you can type "!} fmt". Using this, many in-line text modifications are possible. A more well known way of interacting with the system in vi-like editors is the ":!" command. This command doesn't do any replacing, but allows you to easily access the shell. For example, to take the word count of the current file, you could do ":!wc -w %". "%" is replaced by the current filename.

Vi also does certain other things differently than vim, or vim-inspired editors, such as macros, and the lack of visual* modes. Macros are done via creating a named buffer, with the contents of the macro, and then executing it with the "@" key. It is a bit less convenient than recording macros with "q", however there is much less magic happening behind the scenes. Do you know how your macros are recorded and stored in vim?

Vi certainly has a learning curve, however like most learning curves, the result is worth it. At times I do miss the plugin infrastructure and endless flexibility of editors like vim and vis, but anything that I can do in those heavy editors, I can do in vi. For comparison, a dynamically linked vim binary is about 30MB. Vi on the other hand is 384k. That alone makes the trade-off worth making for me

This page was inspired by this post by june.

Last updated on 2021-08-10.