Sunday, 6 May 2012

Nano, Vim and Emacs

I'm using my linux systems a lot at the moment, last night I was porting a bunch of my uses of the boost threading library over to the c++11 standard threading model/objects.

I did this, for the most part, in a GUI on my windows machines, but for the two linux boxes running my CGI (written in C++) back-bone for file publishing, I had to use the command line.

And I got thinking about better editors for the command line.

Now, I know about nano (use it all the time), I like its ease of use over Vim, it supports text colour highlighting and it is a very simple interface (anyone out there struggling with it?... The ^ (or hat) symbol means "Press CTRL".... So to output the file CTRL+O... which they show as ^O).

Anyway, I then got looking at the other command line editors available, Vi, of course is on most all *nux based systems, so I support at some point I better get around to looking at it.  Indeed in this month's (May 2012) Linux Format contains a tutorial by Jonathan Roberts, on how to use Vim... However, I find following his tutorial, or even the previous brief introduction, hard... Not because the information is not presented.  But that the presentation itself is not simple enough... Vi is a very alien working environment for the beginner, and many tutorials assume too much of the user to introduce them to it easily.

Of course, one then also hears that once one masters Vim they get into emacs... personally, I've never looked at emacs... I know, I know... How can I call myself a Software Engineer and not know Emacs?.... But the truth is, I was using systems, coding on them, long before emacs, long before IDE's in general.

But, going back in history, I remember learning to program, not just on the BASIC interpreters on Commodore machines, but also in Pascal on the ST, with its horrid editor - and then swapping to the nicer "EvereST" editor.  But then later on the PC, my first IDE was Borland Turbo Pascal... And I loved it.

I also see "Free Pascal" on Linux faithfully reproduces the IDE of that era...

I'd love to see that editor support C++ Syntax highlighting and maybe code completion...

But, I doubt it ever will... So in the mean time, I'm going to try and learn to use Vi, and feedback here how I get on, if at all...


  1. The difficult part of learning vi or Vim is understanding how its user interface and command language work and why anyone would want to use it instead of a mouse and pull-down menus. Several books that are quite good still seem to fail at this simple task.

    So, what makes Vim's interface different? There is a typing mode called insert mode and an editing mode called normal mode. Typing and editing are two distinct activities in Vim. In typing mode, the keys on the keyboard do what you would expect in any text editor: they produce text. But in editing mode--normal mode--they move the cursor and make changes to the text that is already there.

    Why have this complicated setup? Because it allows the user to keep their hands on the home row on the keyboard and to issue commands by touch typing without looking at the keyboard. This translates to speedier editing and better concentration.

    Vim's modes are a lot like Photoshop's or GIMP's toolbox tools. Each tool interprets the same mouse actions to produce different results. Clicking and dragging while the eraser tool is selected gives different results from clicking and dragging with the paintbrush tool selected. Each tool is, in effect, a different mode. Vim's modes change what happens when you press keys on the keyboard in the same way that GIMP's tools change what happens when you click and drag the mouse.

    In Vim, touch typing while in insert mode places new text into your buffer, but touch typing in normal mode moves the cursor and edits the text. Its command language consists of nouns, verbs, and modifiers that are combined with a simple consistent syntax:


    Modifiers are optional and consist of a number that specifies how many times to do a verb or how many nouns to do it to. Verbs are actions such as yank (copy), delete, or put (paste). Nouns are text objects such as characters, words, lines, sentences, or paragraphs. Nouns can be used alone for moving the cursor around in your buffer or with verbs to do something to the text while moving the cursor. Registers are sources or targets for commands. They are named clipboards internal to Vim that can be used to store text and are completely optional.

    Each verb and noun usually consists of a single character, and Vim runs these commands as soon as they are syntactically complete, without the need to press the enter key. For example: d4j deletes (d) 4 lines down (j). The books and other tutorials are great at explaining the hjkl movement commands. I won't go into that, but the power in deleting any number of lines by touch typing three or four characters should not be underestimated. This can be done in about one second.

    It takes no longer to delete four sentences or paragraphs. This is because the commands for doing so are just as succinct, but also because they follow the same syntax: d4) and d4}, respectively. Each of these commands can be rewritten as 4dj, 4d), and 4d}, and will produce the exact same results. The difference between them is more semantic than anything. d4w to delete 4 words is no different from 4dw to delete one word four times.

    Once you understand that Vim's commands always combine in a consistent manner, it is simple to learn new commands and to combine them with other commands that you already know.

    Compare this to editors that implement commands as items in pull-down menus. The commands may be easy to find, or not, and it may be easy to start using them without any training, but their limits are reached fairly quickly as more and more are created and as the tasks you want to perform become more complex. Eventually, menus become overcrowded and difficult to navigate. Sequentially typed text commands take no space. New actions can be built from existing commands by finding new ways to combine them, rather than by creating new menu items.

    These are the basics of Vim's user interface. There is more to it, but those concepts give new users the most trouble.

    1. This is the best vi introduction I have read. Addressing the conceptual difference make a lot of sense!
      Thank you.

  2. I can relate to not using a menu, I started to code on a C16... and later on a DOS editor in 1992/1994... I'm getting Vim down now, I'm used to "i" and "esc" and using '$' and 0 to move, as well as ":wq" etc etc... But I still feel Nano is easier to use between a GUI and Vim... CTRL+O to output, which is shown at the bottom is just as useful as Vim... more so as a novice.

  3. Perhaps for a novice. But how long does one remain a novice compared to how long one is an experienced user? And which is more useful to an experienced user: an editor with limited features or one with all the bells and whistles?