[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Lecture 4 Notes



These are the lecture notes for 2-Sep-1998.

Title: Math 481/581 Lecture 4: Editors and Shell Startup Files

Math 481/581 Lecture 4: Editors and Shell Startup Files

© 1998 by Mark Hays <hays@math.arizona.edu>. All rights reserved.


Emacs and Vi

Two of the primary UNIX text editors are programs called emacs and vi. A text editor is not a word processor. Word processors have facilities for dealing with typesetting text, font changes, page layouts, etc. Later in the semester we'll meet LaTeX, one of the most well known UNIX typesetting packages. As opposed to a word processor, a text editor is simply a program for editing text (like Windows Notepad).

If you already know emacs or vi, I suggest that you continue using whichever one you know. If you don't know either one, here are a couple of (subjective) observations that may help you decide which one to learn.

It is generally accepted that emacs is a lot more extensible than vi. For example, it can do color syntax highlighting of source code for many programming languages. If you are interested in such things, consider emacs.

One good thing about vi is that you are virtually guaranteed to find it on any post-1984 UNIX system. If you travel a lot and plan to use other people's computers, vi is a good choice. A large percentage of systems will also have emacs -- but not all.

Emacs is a lot larger than vi. This means that it takes longer to load. It also means that, if you find yourself on a system that does not have emacs, the system administrator may be unable to compile it for you due to lack of time or disk space.

Personally, I find that vi is more efficient for editing small files and emacs is more efficient for editing large files. I also find that vi is much easier on the hands (ie, I have never gotten sore after using vi for eight hours -- I have gotten sore after using emacs for extended periods of time).

Finally, if you are not familiar with either editor, many people feel that emacs is a bit easier to learn than vi. Of course, there are dissenting opinions on this.

In the end, you can accomplish the task of text editing equally well using either of these editors.


Overview of Vi

Vi operates in one of two modes: command mode or insert mode. When you are in command mode, you can perform various operations by hitting normal alphanumeric keys. Several of these keystroke commands will dump you into insert mode. For example, hitting "a" in command mode puts you in insert such that you can insert text after the cursor. Once you are in insert mode, you can simply type text. To leave insert mode, hit the escape (Esc) key.

Here are the four most important vi commands:

Save and quitType :wq in command mode.
Quit without savingType :q! in command mode.
Undo last operationHit u in command mode.
If you're confusedHit Esc a couple of times to get back to command mode.

The vi reference card you are holding (in PostScript form below and as a separate attachment in the scicomp archives) lists some other basic vi commands.


Overview of Emacs

Emacs uses a different approach than vi: instead of having two different operating modes, it uses "funny" key sequences to perform various editing operations.

The emacs reference card you are holding (in PostScript form below and as a separate attachment in the scicomp archives) uses a little bit of notation that you'll need:

The Meta key is marked with a diamond on Suns, sometimes Alt will work on PCs. Other keyboards may use a different key for Meta, or may lack such a key altogether.

If your keyboard does not have a Meta key, you should press and release the Esc key then press and release the "x" key when you see the M-x sequence.

Here are the four most important emacs commands:

Save and quitHit C-x C-s to save your changes. Then hit C-x C-c to quit.
Quit without saving Hit C-x C-c and confirm (when asked) that you do not want to save.
Undo last operationHit C-_ to undo.
If you're confusedHit C-g a couple of times.


Reference Cards


Emacs Tutorial

Emacs has an online tutorial designed to teach you the basic commands. To run the tutorial, start emacs and type C-h t.


Some Vi Tutorials From The Net


Books on Emacs and Vi

There are many books on emacs and vi. Here are two that I've read -- they're pretty good:

Learning the Vi Editor by Linda Lamb, published by O'Reilly & Associates, Inc.

GNU Emacs Manual by Richard Stallman, published by the Free Software Foundation.

I've seen both of them in the ASUA bookstore at various times.


Shell Startup Files

In UNIX, most everything is a process. As an illustration, let's look at how a console login works. When you first sit down at the console of a UNIX machine, you are presented with a login prompt by a program called getty. Once you type in your username, getty spawns the login program -- and login prompts you for your password. If you type the wrong password a couple of times, login terminates and a program called init spawns another getty on the console. If, on the other hand, you type in the correct password, login switches to your user ID (it has been blessed with special powers and can do such things) and starts up your chosen login shell. This shell usually loads some global configuration files and then loads a set of startup files that live in your account before issuing your shell prompt.

This may seem overly complicated, but, if you look at it closely, you'll see that, although several programs get called, none of them does very much (except for your shell). This is the UNIX Way: it's easier to write several little programs and hook them together than to write one big program.

UNIX is unique in that the shell is simply another program. There are many shells available, including ash, bash, bsh, csh, ksh, sh, and zsh. Although all shells do more or less the same thing, each one has certain features that some subset of the user community finds desirable.

To change your login shell, use the chsh command. You will be presented with a list of valid shells (possibly after being prompted for your password) to choose from. On the u.arizona.edu cluster, shell, password, and full name changes are processed in batches every few minutes (apparently 15 minutes or so). You can change your password with the passwd command, and your full name, as known by the system, with the chfn command.

Apparently, different people get different shells when they obtain new accounts on u.arizona.edu. To complete the first assignment, you will need to use chsh to change your login shell to /bin/tcsh.

When tcsh starts up as a login shell, it reads and executes shell commands from two files in your home directory: .login, and .cshrc. The contents of .login are only read when you first log in. You usually put stuff in .login that only needs to be done once; for example, you only need to set your umask once because it is inherited by the shell's subprocesses (unless explicitly overridden).

The .cshrc file is loaded when you log in, when you execute a tcsh shell script, and for all tcsh subshells you might start for whatever reason. Normally the .cshrc file does most of the work of setting up shell aliases, environment variables, etc.

To view the currently defined tcsh aliases, invoke the built-in "alias" command from the shell prompt. To define a new alias, you also use the "alias" built-in as follows:

	> ls
	assign.html     lapack.a        libblas.a       ntuples.tar.gz  zzz
	> alias ls 'ls -F'
	> ls
	assign.html      libblas.a        zzz/
	lapack.a         ntuples.tar.gz
	>
If the alias expansion contains spaces, you will need to enclose it in single or double quotes.

To make an alias permanent, simply add the appropriate alias command to your .cshrc file.

To define an environment variable, use the setenv built-in as discussed previously. For example, to force less to provide verbose status information (the default is brief status info), add the following to .cshrc:

	setenv LESS '-M'
Couple of things: While modifying your startup files, you'll want to meter your progress as you go: if your .login or .cshrc contains errors, tcsh will usually terminate when it tries to execute the offending command. Here "errors" includes anything from syntax errors to logic errors. An example of a syntax error is:
	alias ls 'ls -F
because the closing quote is missing. One of the most embarrassing logic errors I have committed is to paste the word "logout" in my .login file. It does just what you think.

Probably the safest way to modify your startup files is to open a telnet session for the sole purpose of editing the files. When you are ready to test your changes, open a second telnet session and make sure everything works before proceeding.

If this is not possible, you can also suspend your editor with CTRL-Z and reload the appropriate file using the "source" built-in. The only problem with this method is that non-idempotent operations will do their thing multiple times since you aren't starting with a "clean slate". For example, setting "LESS" to "-M" is idempotent, but prepending to an existing "MANPATH" is not.

After completing the first homework assignment, you should test your PAGER and EDITOR settings by sending yourself email. When composing the message, use the "~e" escape to edit the message with the editor. Type in 50 or 60 lines of whatever you like, save the message, and quit the editor. Send the message by typing a "." on a line by itself.

After a few moments, invoke Mail to read your email. Find the message you just sent and read it using the pager with Mail's "pa" command; eg, use "pa 7" if the message you sent is message 7.

Emacs Reference Card

Vi Reference Card