Intro to Scientific Computing
PHYS 27/193
Physics Department
University of the Pacific

The Text Editor

The first thing we will need to build our scientific computing environment is a text editor. In fact you already have one on your computer--all computers have a default editor that will save files as basic ASCII text. ASCII (American Standard Code for Information Interchange ), pronounced [ass' key], is a simple character encoding format based on the English alphabet. It basically lets you encode the keyboard characters plus some special characters like a command to jump to the beginning of a new line (Enter or Return on your keyboad).

The printed ASCII characters (those that get printed to your screen, as opposed to control keys like ^D or ESC, are:


 !"#$%&'()*+,-./0123456789:;<=>?
@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_
`abcdefghijklmnopqrstuvwxyz{|}~

There are a few others for a TAB, New Line, and "Alarm" (tell the computer to make a noise or flash). When interacting with your computer, you will need to create text files containing data, programs, scripts (short programs that are not compiled), and LaTeX files. We'll learn more about each of these later.

A Text editor is very different from a WYSIWYG editor (pronounced [WIZ' zee wig]; short for What You See Is What You Get) like Microsoft Word. WYSIWYG editors are for the creation of documents that will be printed (or faxed or emailed) where layout, font, and formatting is essential--as in business documents. WYSIWYG files are huge, even if they contain only a few lines of text, since they keep formatting info, change history, and many other things.

ASCII files will often be read by another program, such as a list of numbers to be plotted, or the source code (the text) to be compiled into an executable program file (like an .exe file). You might use your computer very professionally in a business office for many years without using a text editor, but for scientific computing and programming, we can't do much without a text editor.

Windows Default Text Editor

If you are working on a computer with the Windows operating system, you already have a default text editor: Notepad which you can find from Start->Programs->Accessories->Notepad. This program will create ASCII text files. By default, Notepad saves files with the extension .txt. You can change this by adding your own extension when saving your file (call it something like filename.dat).

If you are using a Windows computer, try Notepad now.

The Unix Default Editor

Under Unix the default editor is vi, although most unix systems also have emacs and gedit loaded. The first one, vi, is short for Visual editor and dates from 1976. This editor has different modes, one for entering text and another for moving around, copying/pasting, search/replace, etc. Since it was developed at a time when unix meant interacting with a mainframe via a text window (there were no mice in 1976!), both of these modes had to use characters on the keypad. There wasn't even that much uniformity as to the binary sequence sent by an arrow key, if it even existed on your computer. In the 1980s another text editor became popular: Emacs. Emacs is extremely powerful with many features, although some say it has a steep learning curve. The devotion of emacs users to the program has spawned such things as the usenet group alt.emacs.religions. Vi and Emacs are the two major sides in the Editor Wars which has raged for some 25 years between unix geeks.

I myself, use The One True Editor: Emacs.

To get a sense of the features and comparisons of the most popular text editors, open the following link:

Impressive, eh? As you can see, there are many...

Another site (here) lists the editors by popularity.

The top three text editors are: Vim (a modern version of vi), Emacs, and jEdit
(note that Scribus is a page layout application).
Over the years, I've used all three and they are all good. Let's compare some features that we will need.

As I said earlier, an essential feature that we will need is that they can be used in text mode over a networked connection, such as an xterm window connected to a computer on the other side of the world. If you want to edit a file on that remote machine, you will want to use an efficient editor which has hi functionality using only text based commands.

Both Vim and Emacs are text based.

The alternative to this is using a GUI (Graphical User Interface) editor that has little icons for buttons and opens its own window(s)--something like MS Word. While these editors are very nice and have lots of features, they use a lot of computing power to update the windows, respond to the mouse clicks, highlight text, etc. Transmitting these features over the network can be quite demanding. Consider that anytime a character is written to the window, the entire window is usually refreshed. When the editor window is running on you own PC, you don't notice this. But when each update has to go across the country and back, you do.

jEdit is such an editor. It runs only in GUI mode. Another thing we want is that the editor has powerful features like syntax-color-coded text, modes for different types of files (C++, LaTeX, PHP, Perl, etc.). Fortunately, all three editors are very feature rich.

So in summary, I would summarize things this way.

EditorPowerful FeaturesEase of UseText Based over NetworkEditing Modes PortabitilyCost
VimYesHardYesYesAlways on Unix, available for PCs and MacsFree
EmacsYesModerateYesYesAvailable for Unix, PCs, and MacsFree
jEditYesEasyNoYesJava based (all platforms)Free

Since vi is always available on unix machines (it's part of the system), it's good to learn at least the very basics of how to use vi. We won't be doing that in this course; I'll leave that to you, if you go on in scientific computing. (If you are interested in vi, ask Dr. Louise Stark in the School of Engineering and Computer Science--she's a vi expert!).

Note too, that there are also various "Notepad clones" under Linux and Solaris (i.e. the Sunrays) such as gedit which may be available on your Applications menu. If it doesn't say "gedit", it may say "Text Editor". These editors act just like Notepad. They do text editing, but have very few features.



The Emacs Editor


As I said, I use Emacs as my text editor, partly because I am old and have had too many years of practice with emacs to switch. Another reason I am recommend it is that it's Free! If you look at the comparison list of editors, above, you will notice that many of them have a cost. I have tried to only use free software for this course. In fact Emacs was one of the first applications developed by The Free Software Foundation, the people behind GNU software, which you may have heard of. Cygwin, Linux, and much of the Unix world is built on the work of the FSF. Check them out!


So, let's fire up Emacs.

Open an xterm.

There are two ways to start emacs.
When running emacs over a network connection, or for a quick edit, use the terminal mode of emacs;
at the xterm prompt, type:

emacs -nw

The -nw stands for "No Window".

You should see something like this:


There are four areas to the emacs screen:




Type a few characters: your name for example.
Your text appears in the buffer area.
Jim Hetrick


Notice also that two **'s appear in the Mode Line:

----:**-F1  *scratch*        (Lisp Interaction)--L1--All-------------------------------------------------------

These two asterics signify that the buffer has been modified. In order to save these modifications, you will need to save the buffer to a file.
The word *scratch* is the default name given to a buffer if it's not connected to a file.



Emacs commands are entered by typing either:

Let's finish the example we have open.

So save this buffer to a file, we type the "save-buffer" command:

C-x C-s

Type this: CTRL-x CTRL-s. This means hold the CTRL button down and press "x", then still holding down the CTRL key, press "s".

This is equivalent the usual File->Save on most editors.

Using the Mini-Buffer, emacs asks you for the name of the file in which to save this buffer:

File to save in: ~/

I happen to have opened emacs in my Home directory, so emacs asks for the file name in ~/.

Enter YOURLASTNAME.txt and hit ENTER.

Emacs responds with

Wrote /home/jhetrick/hetrick.txt
in the Mini-buffer, and the Mode-line changes to
----:---F1  hetrick.txt       (Text)--L1--All---------------------------------------------------------------------------
The Mode-line now displays the name of the file which is connected to this buffer (hetrick.txt), and the "modified" asterisks (**) are gone since our changes have been saved.


We can continue to edit the file.

Go ahead and add your address to the buffer.

Once you have your address in the buffer,
move around with the arrow keys.
Try using your "Delete, Backspace, Home, and End" keys.

You can see that emacs acts pretty much like Notepad. Aside from the text based commands like C-x C-s, it's pretty intuitive (at least to me!).

Once your have your correct address back, save your changes with "C-x C-s" again.


To Quit emacs, remember that we have to type a text sequence. Do:

C-x C-c

By now I'm assuming you understand what "C-x C-c" means (CTRL-x then CTRL-c).

You should have your xterm prompt back. Do an ls to see that your file is there. Go ahead and cat it which will print the contents of your file to the screen so you can see that you indeed did just edit a text file.

Two more things to try:

At your xterm prompt, type

emacs -nw YOURLASTNAME.txt

Notice that this opens your file for editing.

Quit Emacs with C-x C-c.


Now, open emacs without telling it which file to open.

emacs -nw

Now you can tell emacs to open your YOURLASTNAME.txt file by issuing the command:

C-x C-f

Emacs responds by asking you the name for the file you wish it to find in the mini-buffer:

Find file: ~/

Type YOURLASTNAME.txt and hit ENTER. Emacs will open your file.

Quit emacs again with C-x C-c.


Summary

You now know the most basic skills of using emacs.
You can open a file, edit it, and save it, pretty much the same as you can do with Notepad.

Let's summarize:

CommandAction
At xterm prompt: emacs -nw fileOpen file in emacs
In Emacs: C-x C-fFind (Open) File for editting
In Emacs: C-x C-sSave Buffer to File
In Emacs: C-x C-cQuit Emacs