Editing Text Files

Steven Zeil

Last modified: Aug 29, 2023
Contents:

An editor is a program that allows you to easily create and alter text files. There are a variety of editors on the system, of which the most popular, when working in text-mode, are vi and emacs. Neither is exactly the easiest thing in the world to learn to use. When working in graphics-mode, the most popular are emacs and gedit. The latter is simple and intuitive, but also very bare-bones.

I recommend learning emacs, because

Example 1: Try This: Launching emacs

To run emacs, make sure that you have correctly identified your terminal type. Then give the command

emacs -nw

if you are in text-mode and

emacs &

if you are in graphics mode. The -nw stands for “no window” and requests that emacs not try to open a new graphics-mode window.

Stay in emacs for now. We’ll come back to this in just a moment.

emacs has a built-in tutorial, and you should begin that very shortly. But first, just a couple of notes:

Example 2: Try This: The emacs tutorial

Returning to the emacs session you opened a few moments ago, follow the directions given to bring up the tutorial (i.e., type ^h followed by “t”.).

Continue following the instructions to make your way through the tutorial.

Exit emacs when you have completed it.

When you are done with the tutorial, here are few extra things you should know about emacs:

1 The .emacs Startup File

Before starting up, emacs tries to read a file ~/.emacs Many people store special commands in there to customize emacs to their own liking. (Reading the .emacs file of an experienced emacs user can be instructive although, unfortunately, sometimes a bit intimidating.

If you don’t already have a ~/.emacs file, you might want to download mine into your home directory to get started. After you gain some proficiency in emacs, you may want to take a good look at that file to see what’s in there and to adjust it to your own liking.

Some of the features provided in my .emacs file are

Many of these functions are subject to the limitation of your terminal program. It may or may not allow colored displays. It may or may not pass function key presses on to the remote Unix system, etc. If these things don’t seem to work when you are running emacs in a terminal window, they probably will work later when you learn to run emacs via X.

2 Emacs Modes

emacs offers customized modes for different kinds of files that you might be editing. Some of these are chosen automatically for you depending upon the file name you give. Others can be chosen automatically by giving the command M-x name-mode where name indicates the desired mode. Some of the most popular modes are: text, html, c, and c++. The programming language modes generally offer automatic indenting at the end of each line, though you may have to end lines with the “Line feed” or “C-j” key rather than “Return” or “Enter” to get this.

Example 3: Try this: C++ in emacs

Start up emacs and visit a C++ file from one of your other classes (or create a new one, typing in a few lines of C++ code).

At the very least, you should see a “(C++)” indicator in the status line indicating that you are in C++ mode. In this mode, you have available special commands for compilation and debugging (which we will discuss in a later lesson).

If your terminal program supports color and you copied my .emacs file as suggested earlier, you may see colored “syntax highlighting” as emacs renders reserved words, string constants, and comments in different colors and/or fonts.

And there are little, more subtle things that you will come to appreciate over time. For example, each time you type a “)” or “}” in your code, the cursor briefly flashes back to the “(” or “{” that you just closed.

The command M-/ is a special friend to all programmers who use long variable names but hate to type them. Type a few letters of any word, then hit M-/. emacs will search backwards through what you have previously typed looking for a word beginning with those letters. When it finds one, it fills in the remaining letters. If that wasn’t the word you wanted, just hit M-/ again and emacs will search for a different word beginning with the same characters.

3 The Mark and the Region

emacs has a number of commands that work on an entire block of text at a time. For example, the emacs tutorial told you how to delete a line using C-k. But what if you wanted to delete everything from the middle of one line to the 1st word five lines away? There is a command (C-w) for killing an entire region of text, but to use it you must first tell emacswhat region you want to kill.

The procedure for doing this is the same in all emacs commands that work on regions of text. The “current region” is the set of characters from the “mark” to the current cursor position. The “mark” is an imaginary position marker established by the set-mark-command. The keystrokes for that command are either C-[spc] (hold the control key and type a space) or C-@ (hold the control and shift keys and type ‘2’).

 

So to set up a region to operate on, you move the cursor to one end of the region, give the set-mark-command, then move the cursor to the other end of the region. Everything between the mark and the cursor constitutes the current region, and can be operated on by any region-based command. Some region commands of note are:

C-w
Kill the region, i.e., delete it but save the deleted text in the clipboard. You can “yank” (paste) the deleted text at the cursor position with C-y.
M-w
Copy the region, i.e., Save a copy of the region’s text in the clipboard. You can “yank” (paste) the deleted text at the cursor position with C-y.
C-xC-x
Exchanges the mark and the cursor. Hitting this repeatedly will flip you back and forth between the start and end of the current region. Although often useful in its own right, this command also provides a quick way to check and see if the region is really where you think it is.
C-cC-c
Comment out a region - in C++ and other programming modes, places comment markers in front of each line in the region.
M-x ispell-region or M-x flyspell-region
Run a spell check on all text in the region.

4 Where’s the Documentation?

It’s in emacs. The end of the tutorial discussed some of the built-in help features in emacs. One that isn’t mentioned is the way to get to the entire “reference manual” for emacs. The “info viewer” gives you access to extensive documents about emacs (and about a number of other programs as well, as authors of many other programmers have found the info viewer a convenient way to package on-line documentation.) The commands C-h i or M-x info will launch the info viewer, and the first page of the viewer gives basic instructions on how to use it.

5 For the emacs-phobes

Finally, I will note that, if you develop an incurable allergy to emacs, there are other editors that offer reasonable support to programmers. Some of the textbooks discuss vi, a popular editor that does not offer much support for programming, but a reasonable option is vim (“vi improved”).

Like emacs, vim is available on many, but not all, Unix systems and, once you have learned it, you can use it over telnet via keyboard commands. To learn the basics of running vim, give the command vimtutor.