Previous Up Next

5  Editing Text

5.1  why is this important?

Many people when reading this for the first time, and inexperienced with Unix systems may question why such emphasis is often placed by Unix-types on the editing of just plain text. There are a number of historical reasons behind this, but essentially this boils down to a number of factors.

Primarily it is because most things you do with a computer really boil down to entering words that will later be read by someone else, email, Usenet, writing web-pages and reports... all things which largely consist of chunks of text for the human eye.

Secondly this is because Unix systems are designed with a fair degree of sensible engineering, thus everything that controls them, all the configuration files from the core of servers to how your individual programs work, is controlled by a verity of small simple text files, thus to control the text controls the System.

Thirdly, and with reference to the first point, Unix is a modular system, designed to link components together (see section 10.1 for examples of this) and so it seems silly to reinvent a simplistic text editor for say an email client, when the email client can simply invoke the user’s favourite text-editor and once that user is done collect the text from it.

These factors tend to make a lot of Unix users fairly careful in their choice of text-editor, selecting one from many to make their personal favourite.

Luckily for you the casual user really doesn’t need to care that much about which editor they end up in, and following this is a brief guide to the most commonly used ones, which are available on the Unix server at Lancaster, as a brief foreword it’s worth noting that “vim” and “emacs” are both widely used and thus many people will be able to help you with them (and Google has many good guides to using them), using a less well known editor risks making it harder to find information about it.

Its also worth noting that the precursor to vim (the venerable vi) was used for years by Secretaries at AT&T to write patent applications, and I’ve also heard tales that staff at MIT who weren’t programmers used to write LISP code to extend the copies of emacs they were using (they were told it wasn’t programming, just using the editor, they knew they couldn’t program, but were fine with “just using the editor” when in reality LISP is one of the oldest languages known to Computing), so these editors are not too hard for non-technical users to use.

Further documentation is available online at:
http://hedgehog:8080/help/editors.html

5.2  vi and vim http://hedgehog:8080/help/vi.html

One of the true old classic editors of the Unix world is the editor “vi”, which has been recently superseded by the more up-to-date “vim” editor (Vi IMproved), because of this the use of vi should probably be discouraged and new users should learn vim to start with. To start editing a file with vim simply type:

% vim filename

It should start up and display almost a screenful of lines starting with the tilde character (’~’), this signals that those lines are empty. The last line should read “ “filename” [New File]”.

Typing anything at this point will probably get you confused, so don’t, you see vi (and hence vim) are moded editors. This means you are either entering commands (e.g. save this file, copy that text) or inputting new text (like typing the word “dog”). This, as expected, tends to confuse new users quite a bit.

When using vim you should remember that pressing escape a few times guarantees that you will be in command-mode, and from there you can go back into insert-mode if you wish.

To go from command-mode to insert mode simply press ‘i’ after this you should find that any character you type simply appears in the file. Once you’re done writing new things press escape and you will go back to command mode.

You can only move the cursor about in command mode, your cursor keys may work, depending on your terminal, but if they don’t you can use ‘h’ to move left, ‘l’ to move right, ‘j’ to move down a line and ‘k’ to move up a line.

Other useful commands include ‘a’ which will put you into insert-mode at the end of the current line, and ‘o’ which puts you in insert mode on a new line below the current one. If you want to save the file type ‘:w’ while in command mode, to quit type ‘:q’. If you want to save and quit at the same type type ‘:x’. Deleting characters is done in command-mode and the ‘x’ key (assuming your backspace key doesn’t just work in insert mode, it might or might not, again depending on your terminal) will delete one character, typing ‘dd’ will delete the current line.

This brief introduction doesn’t really do justice to what vi or vim can do, once it’s loaded you can type ‘:help’ in command mode for its help system, or ‘:help tutor’ to go through a tutorial and learn how to use it properly.

For more on vim simply google for words like “vim” or see the following:
http://www.vim.org/
http://vimdoc.sourceforge.net/
Where the first is the homepage of the Vim editor, and the second is a large repository of documentation for the editor.

5.3  emacs http://hedgehog:8080/help/emacs.html

Emacs is the other true classic editor for Unix. Where vi is small and minimal emacs is large and comprehensive, infinitely reconfigurable and considered by many to be more user friendly. To start it simply type:

% emacs filename

Once it’s started you will see a top bar listing various familiar looking options “File”, “Edit” etc. followed by a blank screen waiting for input (assuming that “filename” is a new file). It’s worth noting that if you start Emacs without giving it a file to edit (just type “emacs” at your prompt) you will get a nice introductory text, and some instructions on various commands written as things like “C-x u”, what this means is hold down control (the ‘C’ part) and press ‘x’, then release both control and ‘x’ and press ‘u’.

Most importantly you can press “C-h t” at any point. This will start the emacs tutorial (Hold control and press ‘h’, then release both keys and press ‘t’). The emacs tutorial will cover far more than this document and give you a good grounding in using emacs for everyday editing.

Since you’re using a text-only console then the menu’s at the top of the screen cannot be clicked on with the mouse. Instead they are accessed with the F10 key. However they don’t produce a pretty “folding down” animation. Instead Emacs opens a buffer at the bottom of the screen listing the options available, each with a key associated with it. This makes the menus fast to navigate once you start to learn a few series of keystrokes to get through them, and it also makes exploring the menus fairly easy once you get the hang of it.

Its worth noting that at any point you can press C-ggg to cancel whatever Emacs is doing, be that typing a command, viewing the menus or whatever. Simply hold down control and press the ‘g’ key three times and Emacs should cancel the action.

Otherwise the operation of Emacs is fairly simple, any text you type should just appear in front of you in the file in question, your cursor keys may, or may not work. If they don’t you can use ‘C-f’ to go forwards a character, ‘C-b’ to go backwards a character, ‘C-n’ for the next line and ‘C-p’ for the previous line. ‘C-d’ will delete a character and ‘C-k’ will delete the current line. Pressing the F10 key on your keyboard should allow you to move through the menus by pressing the first letter of an option.

Finally to save your file simply press “C-x C-s” (Control and x followed by Control and s), and to quit type “C-x C-c”.

More information about Emacs is available by Googling for key words such as “emacs”. Good examples of places to look include:
http://www.gnu.org/software/emacs/emacs.html
http://www.emacswiki.org/
http://www.lib.uchicago.edu/keith/tcl-course/emacs-tutorial.html
http://www.xemacs.org/
The first is the homepage for the Emacs editor, the second is a wiki (a collaboratively built site) about Emacs and the third is another introduction (again, please see “C-h t” for the inbuilt tutorial). The forth is a link to the Xemacs homepage, which is a version of Emacs that “forked” from the project several years ago. See that page for more information.

5.4  pico http://hedgehog:8080/help/pico.html

One of most friendly editors, which is also one of the least powerful is pico. It is available via the homegrown section of cent1 (see section 13.1 and is fairly simple in operation:

% pico filename

This will open the file “filename” using pico. If this is a new file you will see a blank screen with “UW PICO(tm) 3.7” in the top left of the screen. At the bottom should be two lines reminding you of the most common commands, all of which are written as ^x where ^ means “hold down control” and x means “press this key”. So to “write out” (what pico calls saving your file) you need to hold control and press ‘o’ (then either select an option, or press enter). Since the commands are listed at the bottom I see little point repeating them here.

Apart from these commands pico’s use is fairly simple, just typing characters will cause them to appear, if your terminal can manage it (see section 6.4) your cursor keys will work to move about the text, if they don’t then ^g lists some keys that will work for movement.

5.5  ue (Lancaster Version) http://hedgehog:8080/help/ue-master.ps

ue (also known as microemacs) is always available on cent1, and its operation is quite simple:

% ue filename

Will start it editing the file “filename”. Once its loaded you should just be able to type and characters will appear, and help is shown in a window at the top of the screen (see section 5.4 for the notation used). Your cursor keys will probably not work with ue, and so you will have to use the ones offered in the help at the top of the screen, ^n for down a line, ^p for up a line, ^o to go forwards a character, ^t to go backwards a character. To save a file press ^f then press the ‘f’ key, it should say “[Wrote N lines]” in the status bar at the bottom.

Full help for ue is available by pressing escape then ‘h’.

5.6  Line Counting (wc)

Following in the steps of the modular editing design discussed above you’ll find that most editors are lacking in several features you would expect from a Windows editor, this includes doing things like counting the number of words in a file. To do this there is the Unix command wc, whose use is fairly simple:

% wc file

Will print out three numbers describing the file shown as “file”, the first is the number of lines the file has, the second the number of words and the third the number of characters (well actually number of bytes, but each character is a byte). If you only want to see the number of lines then use:

% wc -l file

To only print out the number of words in a file use:

% wc -w file

5.7  spell checking (ispell)

Also none of the Unix editors includes a spell-checker, this is because the System itself has a spell checking program in the form of “ispell” (originally “spell” was used, but ispell is far nicer to use). This is invoked by typing:

% ispell filename

This will go through the file spotting words it thinks are spelled incorrectly and asking you what to do about them, offering choices numbered from 0-N and some other options at the bottom of the screen. In its default mode it will also create a backup file called “filename.bak” which has the original text in, in case of mistakes. ispell is fairly powerful and if you want to know more you can use man ispell.


Previous Up Next