UNIX text editors

table of contents

expected learning outcomes

The objective of this activity is to promote understanding of basic emacs commands, and it is structured as a command reference for better continued learning effectiveness.

There are several text editors that create plain text files, such as emacs, vi and pico. Unless you are already familiar with vi or pico, we recommend that you use EMACS (Editor MACroS), known simply as emacs. We strongly recommend that you study the emacs tutorial, which you can easily start from within emacs.

getting started

In emacs you use combinations of special keys and character keys to invoke commands. The commands usually involve either the CONTROL key (CTRL or CTL) or the META key (ESC on MAC or ALT, EDIT on others). Therefore, Ctrl-h (or C-h) means that you press the CONTROL and h key together, while Ctrl-h t (C-h t) means that you press the CONTROL and h keys, then release both keys and tap t.

To start the emacs tutorial:

Ctrl-h t

To get help in emacs:

Ctrl-h

To exit emacs, type:

Ctrl-x Ctrl-c

To exit an emacs command, type:

Ctrl-g

Very Important Command:

Undo last text edit Ctrl-x u

View the emacs Command Summary

creating editing files

To open and edit a file in emacs, simply type

emacs myfile

(myfile can be a new or an existing file)

To save a file:

Ctrl-x-s

To save as:

Ctrl-x Ctrl-w

To choose a file from the current directory:

Ctrl-x Ctrl-f

To move within your file in emacs:

previous line Ctrl-p

next line Ctrl-n

backward 1 char Ctrl-b

forward 1 char Ctrl-f

backward 1 word Meta-b

forward 1 word Meta-f

To navigate by page and file:

forward one page Ctrl-v

backward one page Meta-v

backward to start of file Meta-<

forward to end of file Meta->

cut or copy and paste text

Complete lines of text:

Set cursor position to start of Cut or Copy

Set Mark Ctrl-space-bar

Cut text Ctrl-w

Copy text Meta-w

Reset cursor and paste text Ctrl-y

rectangular editing

This is useful for editing vertical blocks of text, such as removing characters in an alignment or removing tags at the beginning of lines, etc.

Set cursor position to start of Cut or Copy

Set Mark Ctrl-space

Move cursor across text and down to delineate rectangular area
Cut text Ctrl-x rk

Paste text Ctrl-x ry

Copy text Ctrl-x r r [enter]

Paste text Ctrl-x r i [enter]

( additional information)

to work with two files in two different emacs panes:

Split panes Ctrl-x 2

Move between panes Ctrl-x o

Cut, Copy and Paste as instructed above between 2 files

find and replace

There are two types of search and replace functions in emacs. The queried search and replace will find the text and then ask you if you want to change it, whereas the unconditional search will find all occurrences of the text and replace it without asking you.

To perform an unconditional search and replace:

Meta-x


Then type replace string and tap return, followed by the text you want find (return), and the text you want to replace it with.

To perform a queried search and replace:

Meta-%


Emacs will prompt you for the text you would like to find as well as the text to replace it with, then it will find each occurrence of the search string as ask what you would like to do.

Here are some options:

space bar - replace one occurrence and find the next
! - replace all occurrences
n - do not replace and skip to next occurrence
. (period) - replace one match and exit
, (comma) - replace but not move point immediately
q to exit

create keyboard macro

These are recorded keystrokes to automate text editing in emacs.

Start recording macro Ctrl-x (

tap a sequence of keystrokes
End recording of macro Ctrl-x )

Execute last macro in memory Ctrl-x e

Sample file for demonstration:

dna.nex