We are going to learn how to edit texts. There are many text editors, but GNU Emacs and vi are the 2 common editors. There are other ``mouse-oriented'' editors such as gedit or nedit, but I recommend you to learn emacs or vi. Each of these editors have pros and cons, and a unix users generally become either vi or emacs believer (editor war) . I happened to be in the later camp, so we are going to learn emacs now. I like bells and whistles, and emacs is easier to learn (in my opinion).
GNU Emacs is a free, portable text editor. Emacs comes with Mac OS-X, and there is a version for Windows, too. Emacs is not a WYSIWYG word processor which most of you are accustomed to. It is primarily designed as a programmer's editor, and it provide useful features, such as nice indentation of source code, highlighting syntax, matching brackets, etc.
Notation
Here is the standard Emacs notation to describe keystrokes:
C-x | Control-x. Hold down Ctrl and press x simultatneously. |
C-x u | Hold down Ctrl and press x simultatneously. Then, after releasing all keys, press u. |
M-x | Meta-x. Press Escape and release. Then press x |
RET | The return key |
SPC | The space bar |
ESC | The escape key |
Example
Let's dive into the world of emacs by an example.
Press and release ESC. Do you notice the bottom left corner of the terminal says ESC-. It is waiting for a next command (keystroke). Now type >. Do you remember this keyboad short cut works in less, too? M-< has the opposite effect, and the cursor goes to the beginning of the document.
Press and release ESC, then press x. Now try to type in a word (without pressing RET).
Mmmm, what's happening? Emacs is trying to do something you don't understand. When things become screwy (and something is displayed at the bottom line), say ``g''addamit and press C-g (hold down Control and type g). This command will interupt what emacs is trying to do.
This will get you out of questions which emacs may be asking, searching for strings, or you accidentally typed a partial command.
You are currently at the end of the document, right? Now type in this: 1 RET 2 RET 3 RET. So the document become like this:
.....
Bubba shot the juke box stopped it with one shot
Bubba shot the jukebox last night
1
2
3
Notice ``**'' at the 2nd line from the bottom of the screen. This indicates that the document is modified.
Now try C-_. What happened? Try this command 3 more times. This is undo command.
To ``redo'', get out of ``undo'' mode by moving the cursor (any of Up, down, right left arrow keys will do). Then, you can do C-_.
Move to a line with a few words. With C-e, the cursor moves to the ``e''nd of a line. Try C-a, it moved to the beginning of the line (``a'' is the beginning of alphabet).
With C-LeftArrow and C-RightArrow, you can move word by word.
Note that these commands work when you are typing commands in (bash) shell, too.
There are more shortcuts listed in the emacs cheetsheet, so play with it. More you know these navigation commands, more you like emacs. Once you get accustomed to navigation through a keyboard, you become frustrated with other editors/word processors, where you have to use a mouse to move around.
These opertaions are a little awkward at first. Copy or Cut involve 2 actions. First, mark the beginning or end of a region you want to copy by C-SPC. Then go to the other end, and type M-w for Copy, C-w for Cut.
To paste, use C-y (y for yank).
Try this:
M-<
C-SPC
C-DownArrow
C-w
M->
C-y
Sometimes, you want to confirm that you set the ``mark'' correctly. You can use C-x C-x to e``x''change the current cursor and the marked position.
To delete a large region, you just use Cut, and don't paste after you cut it.
C-k will delete (kill) words from the cursor to the end of the line. The deleted part is copied, so you can paste it by C-y.
To search a word in a document, use C-s (for ``s''earch). Then type a word or phrase to search. To find the next match, type C-s again. The search start from the current cursor position, so you need to go to the beginning of the document to search the entire document.
So go to the beginning of the document and search for Bubba.
Go to the beginning of the document and type M-%. At the bottom of the screen, you can type in a word (or phrase) to replace (e.g. sad) and press return. Then type in a new replacement word (e.g. funny), and press return. It will find the matches and ask if you want to replace (answer y or n.
Sometime, it is convenient to see 2 parts of the document at the same time. When you are using emacs under X-Window system, C-x 5 2 will create a new window.
To save a document, C-x C-s. To quit, C-x C-c (``c''omplete).
Cancelation
C-g | Cancel previous key strokes (command). | |
C-_ or C-x u | Undo. |
Navigation
arrows, PageUp, PageDown | move the cursor around | |
C-a | Moves to the beginning of the current line. | |
C-e | Moves to the end of the current line. | |
E for end, A for the beginning of the alphabet. | ||
M-a | Moves to the beginning of the current sentence. | |
M-e | Moves to the end of the current line. | |
C-LeftArrow | Move to a left by a word. | |
C-RightArrow | Move to a Right by a word. | |
C-UpArrow | Move 1 paragraph up. | |
C-DownArrow | Move 1 paragraph up. | |
M-< | Move to the beginning of the document (buffer). | |
M-> | Move to the end of the document (buffer). |
Copy, Cut, Paste, Delete
C-SPC | Mark the position | |
C-w | Cut a region | |
M-w | Copy a region | |
C-y | Paste (Yank) | |
C-k | Delete to the end of a line | |
M-Backspace | Delete a word (backward) | |
M-d | Delete a word forward |
Search and replace
C-s | Incremental search forward. | |
C-r | Incremental search backward (``r''everse search). | |
M-% | Query replace | |
y yes, replace it | ||
n no, don't replace it |
Misc
M-q | break a long line to lines which fit to the width. | |
C-x 1 | Go back to 1 Window (when the window accidentally get splitted into 2). | |
C-x 5 2 | Open another frame of the same buffer | |
C-x C-s | Save file | |
C-x C-w | Save as. emacs will ask for a new file name (``w''right) | |
C-x C-c | quit (``c''omplete) |
This is a practice of using emacs. In the next class, we will use the source code you type in this homework.
White spaces are not critical (i.e. the numbers of spaces don't matter much), but make it look nice.
/* immigration-emigration simulation */ #include <stdlib.h> #define MAXTIME 100 #define PRNT_INTVL 10 int main(void) { int ttt, event, popSize, seed; double alpha, beta; alpha = 0.1; beta = 0.1; popSize = 10; seed = 456739853; srand48(seed) printf("time\tpopSize\n"); for (ttt = 0; ttt < MAXTIME; ttt++) { event = 0; if(drand48() < alpha) event = 1; if(drand48() < beta event = event - 1; popSize += event; if(n < 0) n = 0; if(ttt % PRNT_INTVL == 0) printf("%4d\t%-4d\n", ttt, popSize); } return(0); }
White spaces are not critical (i.e. the numbers of spaces don't matter much), but make it look nice.
Case sensitive: so make sure the case matches with what's in here
#!/usr/bin/perl -w $seqA = "ATATA"; $seqB = "GCGCG"; # concatenating 2 seqs print "1st method:\t", $seqA, $seqB, "\n"; $result = "$seqA$seqB"; print "2nd method:\t$result\n"; $result = $seqA . $seqB; # using concatenate operator (.) print "3rd method:\t", $result,"\n"; exit;