These are the lecture notes for 2-Sep-1998.Title: Math 481/581 Lecture 4: Editors and Shell Startup Files
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.
Esc
) key.
Here are the four most important vi commands:
Save and quit | Type :wq in command mode. |
Quit without saving | Type :q! in command mode.
|
Undo last operation | Hit u in command mode.
|
If you're confused | Hit Esc a couple of times to
get back to command mode. |
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:
C-x
means "press and hold the Control
key then press and release the 'x' key."M-x
means "press and hold the Meta
key then press and relesae the 'x' key."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 quit | Hit 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 operation | Hit C-_ to undo. |
If you're confused | Hit C-g a couple of times.
|
C-h t
.
I've seen both of them in the ASUA bookstore at various times.
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:
.cshrc
,
"cd" to your home directory and type "source .cshrc".
$HOME/man
to your MANPATH, you'll need to use something like:
setenv MANPATH $HOME/man:${MANPATH}Otherwise,
tcsh
will issue a cryptic error
message.
tcsh
has dozens of interesting commands
that can be used to customize and personalize your setup.
See the tcsh
manpage for more details.
.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 -Fbecause 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.