Mastering Your Terminal Editors
There are times, when I feel that it makes sense not to be rational especially where your productivity is concerned. However, I have to admit that a life without humour is boring.
I am thinking of theeditor wars between Emacs and Vi (and certainly its successor, Vim). It is considered to be one of the longest Holy War that characterises the hacker culture.
I tend to take the view that those who were engaged in such passionate debates were doing so out of the need to entertain themselves and take a humorous view of life in general. Along the way, one sees how system administrators, programmers and other developers get attached to their tools and environments in a way that a musician does to their instruments.
In this post, I am going to discuss editing on the terminal: what should you do when you come across an editor you aren’t used to?
This is especially true with Vim and Emacs: most distributions come with vi installed as the default editor. The reason has to do with vi being light on resources, and so suitable for a basic installation.
The basics of a text editor
What is the first thing you should look for in a text editor? Is it its interface? Its market share or manual?
We all agree that it does not make sense to need a manual before one can be productive with a chosen application. If an application isn’t so obvious in how it can be used, then it may be a time-waster.
Unfortunately, this is the state with terminal editors: if you are coming from a graphical user interface world, you expect things to work in a particular way. This is the beauty of Windows, it seems to have standardised the way how its applications ought to work:
You launch a program from the desktop or the start menu;
You start typing;
CTRL-Sto save a file to disk;
You open an already existing file with
All these keystrokes are found in almost every program you come across such that there is no need to learn a new set of rules for each program. This trend seems to have been adopted by the Linux desktop managers as well.
In that same respect, the basics of any text editor—meaning what one looks for in a text editor—is that one should be able to:
- Open it;
- Close it;
- Write text;
- Select text;
- Search and replace.
Looking at Default text editors
By default, we have vi and nano as the terminal text editors. While emacs had been around for this long, you have to install it before you can use it.
Using Nano and vi
To accomplish the things we stated above with both nano and vi, you have to take into account that these are console text editors, meaning that they are optimised for working with text. they are not graphical. So they have a set of commands you have to master.
Of the two, nano is relatively easy as it shows you the commands in its menu:
- Try launching it with
nanofrom the command line;
- you will see that it informs you of the keystrokes you have to use to do basic stuff.
On the other hand, vi (which is nowadays just an alias to Vim) seems to be difficult until you realise that it takes a different approach to working with documents.
It has got what are known as “modes”,—which are just the interactive interfaces presented for the user’s benefit.
- The command mode;
- The Insert mode; and
- the Visual mode.
When you launch vi, whether with a document name as an argument or not, the default mode is the command mode.
You issue commands to vi preceded by a colon (:). Thus to quit the program, you press the colon key followed by the letter q (for “quit”).
To start typing, you must switch to the insert mode. You do this by
just pressing the letter
i (for “Insert”).
vi has got a number of ways in which to insert text:
- You can press
ito switch to the insertion mode;
- You can also press the letter
ato append text at the end of the current line;
- You can add a new line with the letter or:
- The lower letter
oinserts a line after the current one. Which means pressing “o” whilst you are on line 9 of the text will insert line 10 and your cursor moves there;
- Pressing the uppercase letter “O” adds a new line just before the current line.
- The lower letter
In both cases, the mode switches to insertion mode.
So these single letter keys only work while in command mode. If you
are unsure of which mode you are in, just press the
Escape key switches you to the command mode. In the command
mode, pressing other keys will not insert letters into the document.
The Visual mode is used for text selection. To activate it, you have
to be in the command mode, so make sure to press the
Escape key to
get out of the Insert mode.
then press the letter
v (for “Visual”) to start selecting the text.
Navigating the document
This skill is one that is important to master with any editor: how to move your cursor from one point to another.
- With nano (and most other editors, including Emacs for that matter), you can just press the Home key, End key and arrow keys to move around.
- However, with vi, you have to press the letters
j, k, l, has arrow keys:1
- j for down arrow, and k for up arrow;
- h for left arrow and l for right arrow;
- For Home keys,
- Press the letter 0 to move to the beginning of the line; and
- Press the caret key
^to move to the first non-whitespace character in the line.
gg(two lower g keys) to go to the top of the file;
- Alternatively, you can press the letter 1, followed by the letter g to go to the first line. By the way, this is the trick to move to a given line in vi: /just issue the number, followed by the letter g for “go”.
- For the end key:
- Press the dollar sign (
$) to go the end of the line.
- To go to the end of the file, just press the uppercase G once.
- Press the dollar sign (
Well, vi or vim has got a steep learning curve, but once you get hooked to its way of working, you are going to enjoy working with it on the terminal.
A summary of important keystrokes for nano and vi
The table below is going to show you some important keystrokes you may need to remember whenever you start editing files on the console. As this post is not meant to be a detail tutorial, I will only give the very basic keystrokes for you to get by until you invest some time in understanding your preferred editor.
|Launch it||type “nano” on the terminal||Type |
|To close it|
|Save a document|
|Select text||Start the selection with ||Press |
|Quit without saving|
Being comfortable with any text editor is key to your
productivity. This post was just a scratch of how you can master
your terminal or console text editors. Don’t forget that you can
set any editor—including those not covered here— as your
default by getting into your
Just substitute the “vim” or “emacs” above with the name of your preferred text editor, as long as you can call it from the console.
Otherwise, next time when you want to participate in any editor wars, I think it makes sense to understand which editors are being discussed on your own. You may be staying away from a text editor that may speed your work habits by some fraction of seconds if not minutes.
Until next time, have a pleasant text editing journey and enjoy the hacker culture!
You are free to use the cursor keys though. It is just that those keys are considered standard for vi users ↩︎