vi and emacs

vi editor is a small miracle of software engineering, a marvel, a unique, precious gem. Why it is so? Because it emerges from several elegant ideas put together in an classic old-school elegant simplicity.

This is an universal criteria - a magical thing emerges accidentally (evolves as a result of a long refinement and selection process) as a mixture of several elegant ideas and applied techniques. Lisp has been emerged this way.

In vi editor we have minimalism, we have uniformity and a familiar patterns.

Here we have a separation of concepts, two clearly separated modes - command mode and editing more - two states. You are either in a state of adding text, or in a state of executing commands on text you have. This is a natural concept.

We have uniform navigation - a notion of beginning of line, end of line, a whole line and words. That gives us a natural way to split text into separate pieces, distinct, logical chunks.

We also have a single-symbol commands, numerical modificators for them and generic commands for a repetition of previous action.

This is where the power comes from. Together all these ideas produce a magical miracle.

In command mode we could press a few keys to perform a logical, repeatable actions, according to with our current focus - the cursor position:

cw - change one word
c$ - change the rest of current line
cc - change the whole line

dw - delete a word
d$ - delete the rest of this line
dd - delete this line
5dd - delete 5 lines down from the current

These just a few simple examples of what uniformity gives us.

We have current location, the notion of a pieces of a line, and available actions.

What is the most important, we could discover patterns and learn them as we go.

The general pattern is this:

  • you are in command mode
  • your cursor indicates the current focus of your attention
  • you execute a command, parametrized by modificators - on which part of a line you want to perform intended action
  • as a result of your command you are entering editing mode to input a few characters
  • you are returning back into command mode by pressing Escape key, which indicates that you are done with this action.

Now you can repeat this action on the other place of text, just by placing cursor here and pressing . - a dot. This command will repeat the last action you have done.

What this remind us? It is a generalization of a predator's behavior. It is in a resting mode most of the time, observing. Then it performs a very quick, very precise action, almost without no unnecessary movements, and returns back to and idle, resting mode. This is beauty - minimalism and precision, without any redundancy.

Why it is so exciting? Because it is a natural pattern, and we learn it very quickly, by doing, to perform those actions almost unconsciously - as it supposed to be. Our mind designed to learn patterns by training and then filter them out from our attention.

This is why vi editor is such a miracle.


Emacs is a different story, but almost the same idea.

There is again a distinction between the two processes - entering text and entering commands to perform action within the text. In vi there are two separated modes, in emacs there is a notion of a key sequence, where a special key - modificator is involved.

There are few of such keys - Ctrl, Alt (or Meta) and Shift. (There also Winkeys (Super, etc) but first two is enough.)

So, by pressing Ctrl together with some other key we perform something different for just typing a text in. This is also natural pattern.

In emacs, usually, commands associated with Ctrl+X sequence, where X is some character, including, of course X itself.

For example, a Ctrl-a sequence interrupts our editing and moves cursor (our attention) to the beginning of line. Ctrl-e moves it to the end of line.

Of course, it is we, who decided to stop our current action and shift our attention to the beginning of the line first - we move our eyes and focus them on that area and then we are pressing appropriate keys to move the cursor there.

Again, when we learn these patterns we do them without explicit thinking. Our eyes move, our fingers press the keys. The command key-sequences is the way to not mess everything up.

But almost every editor, except vi, which is a small miracle, designed this way - to associate actions with key sequences. Why emacs is so special?

Because it is dynamic. We can bind and re-bind every sequence we wish using internal Lisp interpreter. Most of the actions available to us are implemented as Lisp functions which we could call by pressing a key-sequence associated with it. Moreover, we could change not just bindings between keys and functions, but also functions themselves, if we wish.

This is where the unlimited power of Emacs comes from.

These two emerged approaches are so remarkable, that we have both of them in a readline library - the implementation of a command-line editing functions, which is a part of good software.

The question which one is better is meaningless, as you can see. Which music instrument is better - a piano or a violin? What a nonsense!

see also

Last modified 4 years ago Last modified on Nov 14, 2017, 1:39:11 PM
Note: See TracWiki for help on using the wiki.