# Text editors

In this chapter, we will discuss the importance of mastering an editor. We will focus mainly on the Improved `vi` editor.

After finishing this chapter, you will be able to:

- Open and close files in text mode

- Edit files

- Search text

- Undo errors

- Merge files

- Recover lost files

- Find a program or suite for office use

## Text editors

### Why should I use an editor?

It is very important to be able to use at least one text mode editor. Knowing how to use an editor on your system is the first step to independence.

editors why?

We will need to master an editor by the next chapter as we need it to edit files that influence our environment. As an advanced user, you may want to start writing scripts, or books, develop websites or new programs. Mastering an editor will immensely improve your productivity as well as your capabilities.

### Which editor should I use?

Our focus is on text editors, which can also be used on systems without a graphical environment and in terminal windows. The additional advantage of mastering a text editor is in using it on remote machines. Since you don't need to transfer the entire graphical environment over the network, working with text editors tremendously improves network speed.

editors text editor overview

There are, as usual, multiple ways to handle the problem. Let's see what editors are commonly available:

#### GNU Emacs

Emacs is the extensible, customizable, self-documenting, real-time display editor, known on many UNIX and other systems. The text being edited is visible on the screen and is updated automatically as you type your commands. It is a real-time editor because the display is updated very frequently, usually after each character or pair of characters you type. This minimizes the amount of information you must keep in your head as you edit. Emacs is called advanced because it provides facilities that go beyond simple insertion and deletion: controlling subprocesses; automatic indentation of programs; viewing two or more files at once; editing formatted text; and dealing in terms of characters, words, lines, sentences, paragraphs, and pages, as well as expressions and comments in several different programming languages.

editors emacs
emacs

Self-documenting means that at any time you can type a special character, Ctrl+H, to find out what your options are. You can also use it to find out what any command does, or to find all the commands that pertain to a topic. Customizable means that you can change the definitions of Emacs commands in little ways. For example, if you use a programming language in which comments start with <** and end with **>, you can tell the Emacs comment manipulation commands to use those strings. Another sort of customization is rearrangement of the command set. For example, if you prefer the four basic cursor motion commands (up, down, left and right) on keys in a diamond pattern on the keyboard, you can rebind the keys that way.

Extensible means that you can go beyond simple customization and write entirely new commands, programs in the Lisp language that are run by Emacs's own Lisp interpreter. Emacs is an online extensible system, which means that it is divided into many functions that call each other, any of which can be redefined in the middle of an editing session. Almost any part of Emacs can be replaced without making a separate copy of all of Emacs. Most of the editing commands of Emacs are written in Lisp already; the few exceptions could have been written in Lisp but are written in C for efficiency. Although only a programmer can write an extension, anybody can use it afterward.

When run under the X Window System (started as `xemacs`) Emacs provides its own menus and convenient bindings to mouse buttons. But Emacs can provide many of the benefits of a window system on a text-only terminal. For instance, you can look at or edit several files at once, move text between files, and edit files while running shell commands.

#### Vi(m)

Vim stands for Vi IMproved. It used to be Vi IMitation, but there are so many improvements that a name change was appropriate. Vim is a text editor which includes almost all the commands from the UNIX program `vi` and a lot of new ones.

editors vim
vim

Commands in the `vi` editor are entered using only the keyboard, which has the advantage that you can keep your fingers on the keyboard and your eyes on the screen, rather than moving your arm repeatedly to the mouse. For those who want it, mouse support and a GUI version with scrollbars and menus can be activated.

vi

We will refer to `vi` or `vim` throughout this book for editing files, while you are of course free to use the editor of your choice. However, we recommend to at least get the `vi` basics in the fingers, because it is the standard text editor on almost all UNIX systems, while `emacs` can be an optional package. There may be small differences between different computers and terminals, but the main point is that if you can work with `vi`, you can survive on any UNIX system.

Apart from the `vim` command, the vIm packages may also provide `gvim`, the Gnome version of `vim`. Beginning users might find this easier to use, because the menus offer help when you forgot or don't know how to perform a particular editing task using the standard `vim` commands.

vim gvim

## Using the Vim editor

### Two modes

The `vi` editor is a very powerful tool and has a very extensive built-in manual, which you can activate using the `:help` command when the program is started (instead of using `man` or `info`, which don't contain nearly as much information). We will only discuss the very basics here to get you started.

vi modes

What makes `vi` confusing to the beginner is that it can operate in two modes: command mode and insert mode. The editor always starts in command mode. Commands move you through the text, search, replace, mark blocks and perform other editing tasks, and some of them switch the editor to insert mode.

This means that each key has not one, but likely two meanings: it can either represent a command for the editor when in command mode, or a character that you want in a text when in insert mode.

Pronunciation

It's pronounced vee-eye.

### Basic commands

#### Moving through the text

Moving through the text is usually possible with the arrow keys. If not, try:

vi navigation

- `h` to move the cursor to the left

- `l` to move it to the right

- `k` to move up

- `j` to move down

SHIFT-G will put the prompt at the end of the document.

#### Basic operations

These are some popular `vi` commands:

- `n dd` will delete n lines starting from the current cursor position.

- `n dw` will delete n words at the right side of the cursor.

- `x` will delete the character on which the cursor is positioned

- `:n` moves to line n of the file.

- `:w` will save (write) the file

- `:q` will exit the editor.

- `:q!` forces the exit when you want to quit a file containing unsaved changes.

- `:wq` will save and exit

- `:w newfile` will save the text to `newfile`.

- `:wq!` overrides read-only permission (if you have the permission to override permissions, for instance when you are using the root account.

- `/astring` will search the string in the file and position the cursor on the first match below its position.

- `/` will perform the same search again, moving the cursor to the next match.

- `:1, $s/word/anotherword/g` will replace `word` with `anotherword` throughout the file.

- `yy` will copy a block of text.

- `n p` will paste it n times.

- `:recover` will recover a file after an unexpected interruption.

#### Commands that switch the editor to insert mode

vi insert mode

- `a` will append: it moves the cursor one position to the right before switching to insert mode

- `i` will insert

- `o` will insert a blank line under the current cursor position and move the cursor to that line.

Pressing the Esc key switches back to command mode. If you're not sure what mode you're in because you use a really old version of `vi` that doesn't display an INSERT message, type Esc and you'll be sure to return to command mode. It is possible that the system gives a little alert when you are already in command mode when hitting Esc, by beeping or giving a visual bell (a flash on the screen). This is normal behavior.

### The easy way

Instead of reading the text, which is quite boring, you can use the vimtutor to learn you first Vim commands. This is a thirty minute tutorial that teaches the most basic Vim functionality in eight easy exercises. While you can't learn everything about `vim` in just half an hour, the tutor is designed to describe enough of the commands that you will be able to easily use Vim as an all-purpose editor.

vim tutor

In UNIX and MS Windows, if Vim has been properly installed, you can start this program from the shell or command line, entering the `vimtutor` command. This will make a copy of the tutor file, so that you can edit it without the risk of damaging the original. There are a few translated versions of the tutor. To find out if yours is available, use the two-letter language code. For French this would be `vimtutor fr` (if installed on the system).

vimtutor

## Linux in the office

### History

Throughout the last decade the office domain has typically been dominated by MS Office, and, let's face it: the Microsoft Word, Excel and PowerPoint formats are industry standards that you will have to deal with sooner or later.

office suits history

This monopoly situation of Microsoft proved to be a big disadvantage for getting new users to Linux, so a group of German developers started the StarOffice project, that was, and is still, aimed at making an MS Office clone. Their company, StarDivision, was acquired by Sun Microsystems by the end of the 1990s, just before the 5.2 release. Sun continues development but restricted access to the sources. Nevertheless, development on the original set of sources continues in the Open Source community, which had to rename the project to OpenOffice. OpenOffice is now available for a variety of platforms, including MS Windows, Linux, MacOS and Solaris. There is a screenshot in .

OpenOffice.org

Almost simultaneously, a couple of other quite famous projects took off. Also a very common alternative to using MS Office is KOffice, the office suite that used to be popular among SuSE users. Like the original, this clone incorporates an MS Word and Excel compatible program, and much more.

office suites overview

Smaller projects deal with particular programs of the MS example suite, such as Abiword and MS Wordview for compatibility with MS Word documents, and Gnumeric for viewing and creating Excel compatible spreadsheets.

### Suites and programs

Current distributions usually come with all the necessary tools. Since these provide excellent guidelines and searchable indexes in the Help menus, we won't discuss them in detail. For references, see you system documentation or the web sites of the projects, such as

- http://www.openoffice.org

- http://www.koffice.org

- Freshmeat and SourceForge for various other projects.

### Remarks

#### General use of office documents

Try to limit the use of office documents for the purposes they were meant for: the office.

office suits best practice

An example: it drives most Linux users crazy if you send them a mail that says in the body something like: Hello, I want to tell you something, see attach, and then the attachement proves to be an MS Word compatible document like: Hello my friend, how is your new job going and will you have time to have lunch with me tomorrow? Also a bad idea is the attachment of your signature in such a file, for instance. If you want to sign messages or files, use GPG, the PGP-compatible GNU Privacy Guard or SSL (Secure Socket Layer) certificates.

These users are not annoyed because they are unable to read these documents, or because they are worried that these formats typically generate much larger files, but rather because of the implication that they are using MS Windows, and possibly because of the extra work of starting some additional programs.

#### System and user configuration files

In the next chapter, we start configuring our environment, and this might include editing all kinds of files that determine how a program behave.

Don't edit these files with any office component!

The default file format specification would make the program add several lines of code, defining the format of the file and the fonts used. These lines won't be interpreted in the correct way by the programs depending on them, resulting in errors or a crash of the program reading the file. In some cases, you can save the file as plain text, but you'll run into trouble when making this a habit.

#### But I want a graphical text editor!

If you really insist, try `gedit`, `kedit`, `kwrite` or `xedit`; these programs only do text files, which is what we will be needing. If you plan on doing anything serious, though, stick to a real text mode editor such as `vim` or `emacs`.

editors graphical editors

An acceptable alternative is `gvim`, the Gnome version of `vim`. You still need to use `vi` commands, but if you are stuck, you can look them up in the menus.

## Summary

In this chapter we learned to use an editor. While it depends on your own individual preference which one you use, it is necessary to at least know how to use one editor.

The `vi` editor is available on every UNIX system.

Most Linux distributions include an office suite and a graphical text editor.

## Exercises

This chapter has only one exercise: start the Vim tutor by entering `vimtutor` in a terminal session, and get started.

You may alternatively start `emacs` and type Ctrl+H and then T to invoke the self-paced Emacs tutorial.

Practice is the only way!