Linux Editors
Windows 2000

General Info

Roving Profile
Exam Papers

System Support


Editors in Linux

A text editor is like a word processor but without a lot of the features. All operating systems come with a text editor and linux is no exception. In fact linux comes with several text editors. The primary purpose of text editors is for writing a file in plain text with little or no formatting so that various other programs can read it. As students of computer systems you will use various text editors to generate code. Your choice of text editor will no doubt depend on such things as syntax highlighting, size of editor, debugging tools and various other features.

The standard text editor for Linux is called 'vi'. This is a program that comes from UNIX. There is a more recent version called 'vim' which means 'vi improved'. The problem with 'vi' or 'vim' is that a lot of people don't like it. You have to remember a lot of key combinations to do stuff that other text editors will do for you more easily. An alternative to 'vi' is 'Emacs'. Emacs is easier for first time users to become familar with and remembering key combinations is not required. Some of the popular editors are introduced below. Visit their homepages for additional information.

Emacs text editor

Emacs is one of the most popular and powerful text editors used on Linux (and Unix). You will use it to create, edit and save files. It comes with syntax highlighting for most main stream programming languages including c/C++. When Emacs works on a file, it doesn't actually work on the file itself. Instead it copies the contents of the file into a special Emacs work area called a buffer, where you can modify it. When you have finished working, you tell Emacs to save the buffer (i.e. to write the buffer's contents into the corresponding file). Until you do this, the file remains unchanged. There're a number of ways of opening emacs. Probably the simplest it to type emacs at the prompt in the terminal window. Remember to insert an ampersand "&" after the call to emacs so that the terminal can be used for other purposes following the call to emacs. Alternatively emacs can be opened by clicking start -> Applications -> emacs. This may of course vary depending on the version of linux that you are using and whether or not you are using a "GNOME" or "KDE" session. In order to become familiar with emacs it is recommended that you complete the tutorial on emacs. To view the tutorial simply launch emacs and the select help->emacs tutorial or alternatively use Ctrl-h t

Emacs is also very easy to customize. This involves changing the .emacs file.

Some Useful Emacs Commands

The most important commands listed below are in bold. 99% of your emacs experience will deal with these commands and therefore we have made a quick reference guide which you may find handy to print out and keep on hand.
A couple of pointers before proceeding.

^ stands for Ctrl or Control - Usually found in the lower left of the keyboard.
M stands for Meta (Escape key) - Usually found in the upper left corner
Note that the Meta (Esc) is always pressed prior (i.e. sequentially) to the following key.
Avoid holding down the escape key.
Note also that ^x^f is different from ^xf.In the second case, do not hold down the control key while pressing f.
A complete listing of all the key bindings in emacs is available at emacs bindings

Moving around:

^p previous line.
^n next line.
^a beginning of line.
^e end of line.
^f and ^b forward and back one letter at a time.
Arrow keys also move the cursor around (duplicating ^p, ^n, ^f, and ^b).
Mv previous screenfull.
^v next screenfull.
M< top of document.
M> end of document.

Cutting and Pasting:

^K kills (cuts) to the end of the line (a second ^k will cut the carriage return).
^y yanks back text from the last kill.
M y immediately after a ^y will replace what you just yanked by previously killed text.
^d deletes the character at the cursor location.
^2 some text ^w wipes out a region. These can be yanked back with ^y


^x^f find file.
^x^s save file.
^xi insert file at cursor position.
^xs save any files that have changed (you'll have to answer y/n for each file).
^x^w write out current buffer into new file (to save under a different file name).

Search and Replace:

^s search starting at cursor point. Repeated ^s commands will find the next occurrence.
^r reverse search.
M (Escape) or any other command or mouse click will terminate a search.
^s^s will use the previous search string.
M% prompts for a string and replacement and will ask for confirmation before replacing.
Mx replace-string prompts for string and replacement and replaces all without confirmation.

Miscellaneous commands:

^g abort command in progress.
^x^c quit emacs(^z will suspend).
^xu undo, will continue to undo until the last save.
M$ check spelling of this word.
^xb change buffers (switch between files if more than one are open).
Mq formats paragraph so all lines are no longer than "fill" amount.
^xf sets "fill" amount to current cursor position.
Use space or tab for word or command completion on the command line (minibuffer).

Window commands:

^x 2 Split current window
^x 1 One window (close the others)
^x 0 close window
^x o got to next window

Useful features:

Most Emacs commands are linked to keystrokes. Using ^x ^c, you invoke save-buffers-kill-emacs, which saves your file and quits Emacs. You can invoke any Emacs command using ESC x and entering a command. ESC x lets you enter the window (it looks like a blank line) at the bottom of the screen where you can enter commands. To save a file and quit Emacs, you can use ^x ^c or use ESC x save-buffers-kill-emacs. Emacs puts the hyphens in for you.

Emacs has command completion. If you want to save your file, but have forgotten the key sequence and the command, don't panic! Press ESC x, enter save,and press return. Emacs will list commands that start with save, and you can pick the one you want.

In is worth noting that emacs has a menu system like microsoft word. Therefore you are not required to learn all of the above comands.

Customizing Emacs:

To customize Emacs, you need to write some Lisp code. It's not really that difficult. The code should be put in a file named '.emacs' in your home directory. Every time Emacs starts up, it automatically reads any Lisp code you have put into your '.emacs' file unless you tell it not to by specifying `-q' on the command line. (The emacs -q command gives you a plain, out-of-the-box Emacs.) . To define a key binding, use an expression of this form:
(define-key key-map keystrokes function)
To set an Emacs variable, use an expression of this form:
(setq variable value)
The section below gives some typical customisations you might perform in your '.emacs' file. Note that comments in are preceded by semicolon ';' characters.
(global-set-key "\M-g" 'goto-line) Esc-G runs the goto-line function.
(global-set-key "\C-xt" 'transpose-lines) Ctrl-x t runs the transpose-lines function.
(setq default-major-mode 'text-mode) Default mode is text-mode.
Each student will have a copy of a .emacs file on their home directory. It is strongly advised that each student makes a backup of this file if they intend to modify the original. More information is available on configuring .emacs files at .emacs homepage .emacs homepage

VI Text Editor


VI is a Visual Editor, hence the name. Visual editors are ones that let you see multiple lines of the document that you are editing as you edit it. This seems pretty common in most editors today, so the idea of a non-visual editor is a little strange. Examples of non-visual editors are sed, ex, ed, and edlin (the last one being the editor shipped with DOS.) VI was written by William Joy as part of the bsd distribution of Unix. It was later used by AT&T, and has been standard Unix since.

Why should I use VI

vi is default visual editor under Unix, and is therefore shipped with all versions of Unix following the mid 80's. This means that whenever you run across a machine that is running a Unix of some sort, you will know that you have a powerful editor at your fingertips. VI is a powerful editor. Also, once you know vi, you can edit files really quickly, as it is extremely economical with the keystrokes. Due to its different modes for inserting and issuing commands, it is much faster than most non-mode based editors. It is also a very small editor been only a few hundred k big. Furthermore, as an editor it allows a lot of interaction with the Unix operating system. It allows you to use many of the powerful Unix utilities from within vi. It's possible to, from within vi, kill off a list of specific processes that you want to kill. If you haven't given yourself permission to write to the file you're editing by accident, you can change the permission without leaving vi. vi works by having commands that work on objects. By learning only a few commands and a few objects you can learn how to do a lot of things, simply by combining the commands and the objects. Even the commands are separated into subcategories. For example, the movement commands and the modification commands are different. If you know that d is the command to delete, and j is the command to go down a line, and you know how to combine them (dj), you can delete down one line. When you use numbers it gets even more powerful. d5j will delete 5 lines below you. Then, once you learn the character for "paragraph" (}), you already know how to delete 5 paragraphs.

Problems with VI

VI can be somewhat hard to learn, and until you do so, it will be slow and painful. Once you learn it, it will be faster, but the process of learning it is slow. In fact for those people new to linux I would suggest that you use emacs for the time being at least. It's easier to learn.

Using VI

To use VI type vi at the command prompt and your console should look like the image below.

VI editor
When VI is first started, it expects commands, be it text input, deletions, changes, searches. Following the command, an argument usually needs to be given, for example, following command i (insert text at the cursor), one would type text and then end the command with Escape. For deletion, one would press d and then an argument, for example w for word, or 3w for three words or } which in vi-speak means to the end of the paragraph. If simple mnemonic commands are not sufficient, command line is provided through pressing: (colon). From there long commands can be executed to be applied to ranges of lines in text. Even external programs can be used as filters to process parts of the text.

Basic VI commands

The following list of commands will only suffice to get you started. For a more detailed explaination View the numerous tutorials and FAQs on the web the links for which are given below. If this is not to your satisfaction then consult any Unix book that will typically have an introductory chapter on the VI editor. Good luck!

Saving files

Command Action
vi filename start editing filename, create it if necessary
:wq write the file to disk and quit
:q! quit without saving any changes
:w! newfile write all lines from the entire current file into the file 'newfile', overwriting any existing newfile
:n,m w! newfile write the lines from n to m, inclusive, into the file newfile, overwriting any existing newfile

Moving Cursor

Many of the vi commands take prefixes. For example 5h moves the cursor 5 places to the left.

Command Action
h one space to the left
j one line down
k one line up
l one space to the right
$ end of current line
^ beginning of current line
Enter beginning first word on the next line

Vi Tutorials

Introduction to VI by Bill Joy
Mastering the VI editor
The VI editor (Good reference)
VI Help File
VI According to google
VI Documentation


Basic Vi FAQ
Advanced FAQ

Department of computer science and information systems
University of limerick, limerick, Eire

Department of Computer Science and Information Systems, University of Limerick, Limerick, Eire
Valid HTML 4.01! Valid CSS!