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

Learning More About Emacs

Even though we just saw these commands on the previous page (that may have been a few days ago), it's worth repeating these most basic commands here. With these you can write/edit a file and save it. I addition you would use the arrow keys to move around, plus backspace and delete.

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

Of course we'd like a little more functionality.

Recall that C-x means CTRL-x. There are many commands which take the form C-some key.

And, there is another type of command that we haven't yet used yet (I mentioned it on the previous page).
These are commands that take the form

ESC-key  this is abreviated M-key

The reason these commands are abreviated M-key is that M stands for Meta, which comes from the days before keyboards were standarized. Now, the dust has settled and M stands for ESC, the Escape key. This key is a prefix key--you his Esc then another key, as opposed to the Ctrl key which is a modifier--you hold CTRL down while you press another key.



Playing with Emacs

Let's learn a bit more about Emacs.

On the previous pages I had you start emacs with the command: emacs -nw  which opens emacs in an xterm window in text (no GUI window) mode.
Now let's open emacs in GUI mode. Open an xterm and at the prompt type:

emacs

Just emacs, no -nw.

This will open an emacs window on your screen that looks like this:

Notice that GUI version has the same four areas as the text mode version:

Can you identify them all? In addition you have an icon toolbar.



Let's open a file to play with. We're going to open it a couple of different ways.

First, click on the File menu on the Toolbar.

Click on "Open File...".

You might expect this to open a GUI window that allows you to click on the file you want.
What happens however, is that the text: "Find file: ~/" in the mini-buffer.
What you do is type the name of the file that you want to edit in the mini-buffer. Emacs puts the name of the current directory after the "Find File:".

Before we open a file, let's Abort this Find File. This is a very useful thing to know how to do.

In the Emacs window, type C-g (CTRL-g)

The mini-buffer says "Quit". This is because we Aborted the command to Find a File.
In general, you can always abort whatever you are doing (or were trying to do).


Now click on the Find File Icon:

The same thing happens as before: "Find File:..." appears in the mini-buffer.

Abort this with C-g


Finally, you can open the file using the method you already know!--the text sequence mode we used before. This is the method to use when connected to a computer over the network. In the emacs window, type:

C-x C-f

Same thing: you get "Find File:" in the mini-buffer.


This time let's open a file.

If you type the name of a file which does not exist, the buffer is renamed to this name, and the words (New File> appears in the mini-buffer.

Go ahead and type the name of a new file: zzz

Do you see the (New File) text in the mini-buffer?

Now type some characters in the buffer window. Anything will do.

Let's not save this file. We can Kill the buffer by clicking on the X in the Tool Icons.

This brings up a dialog window that asks you: Buffer ... modified; kill anyway? [Yes] [No]

Click [No].


Now let's do the same via text-based commands. In the emacs window, where you still have the new zzz buffer, type:

C-x k

This is CTRL-x followed by typing the key k.

The mini-buffer should say: Kill buffer: (default zzz)

Type ENTER

The mini-buffer will respond with: Buffer zzz modified; kill anyway? (yes or no)

With the cursor at the right end of the mini-buffer (click on the end of this text if it's not there already).

type: yes

The zzz buffer should disappear.



Let's summarize:

To open a file  (find-file)

Windowed Mode (GUI)Text Mode
Click on the File->Open File... buttonC-x C-f

To kill a buffer  (kill-buffer)

Windowed Mode (GUI)Text Mode
Click on the X in the Toolbar IconsC-x k



So now let's step up the pace a bit.

Your emacs window should still be open. Go ahead and close it (quit). Remember how?

Now cd to your home dir and make a new subdirectory called editplay/.
Cd to editplay/.
Now save the following file into this directory:

stairway.txt

Open it in emacs (use the GUI version of emacs), by typing emacs stairway.txt


Movement

Let's start by summarizing movement commands.

In graphical GUI mode, things work pretty much as you would expect from other word processors like MS Word:

in Window Mode (GUI)

Most normal movement cammands work:

Go ahead and try these. You may have to use both mouse buttons simultaneously on the scroll bar to emulate the Unix three button mouse--the middle button is emulated by holding both buttons down together.

In text mode movement can be achieved with the same keys usually, but sometimes on a remote machine the terminal emulation can be strange. For this reason, emacs has some text-based key strokes which implement movement commands. These are

in Text Mode

C-aBeginning of line
M-fForward one word
M-aPrevious sentence
M-vPrevious screen
M-<Beginning of buffer
C-eEnd of line
M-bBack one word
M-eNext sentence
C-vNext screen
M->End of buffer

Notice that the GUI mode of Emacs recognizes all the Text-Based commands as well. In fact, I work with emacs over a network connection so often that when I am in GUI mode, I still use the text-based versions of most commands.

Try the text-based movement commands above

Remember that M-f (for example) means ESC f.


Cutting and Pasting

Window (GUI) mode

In your open "stairway.txt" buffer,
Click/drag your mouse in the text to highlight a region of text as shown below.
(pick one of the stanzas of the song)


In Emacs lingo this highlighted area is called  The Region.
We'll come back to this concept in a minute.

With the region still highlighted,

click the Edit menu item on the Toolbar,
then click Cut

The highlighted region should disappear. This is the same as CTRL-x in MS Word. You have "Cut" the region of text, and at the same time Copied the region to the Clipboard.

Now go to the Toolbar and

click Edit->Paste.

This should put back the text you just "Cut". It will be Pasted at the place where the cursor is located.

Notice too that under Edit you also have the option to Undo your changes.

Try this:

Highlight some text again
Cut the text by doing: Edit->Cut
Undo your "Cut" by clicking: Edit->Undo

This should put back the text you Cut.


Now let's try this in

Text mode

This is how you cut/copy text when you are editing using emacs on a remote machine via a text terminal.
We can still issue the key sequences in the open emacs window; it will respond the same way.

In your open emacs window,

move the cursor to the beginning of a stanza of the song.

Preferrably do this with the arrow keys as you would in an xtext window.

Now, Set the Mark, by typing

C-spacebar

The words Mark set will appear in the mini-buffer.

The Region is now the area between the Mark and the Cursor.
Having set the Mark at the beginning of the stanza,

move the cursor to end of the stanza

Now type

C-w

This is the text-mode key sequence that does "Cut".

Similarly, the text-mode key sequence to "Copy" is M-w (ESC w).

The text-mode key sequence to Undo is

C-x u

Try this now. Does the text you "Cut" come back (i.e. was your "Cut" undone)?


Pasting

In GUI mode, pasting the text you just cut is easy. Highlight the text, click Edit->Cut, put the cursor where you want it and click Edit->Paste. Pretty easy.

In Text-Mode, the Paste command is C-y ("y" for yank).

Go back and "Cut" a stanza again:

Move the cursor back to the beginning of the stanza
Set the Mark, with C-spacebar
Move the cursor to the end of the stanza
Type C-w

This has just "Cut" some text, as we did above.
To "Paste" the text, move the cursor to the place you want to paste it, then type:

C-y

This should "Paste" the text you just "Cut" at that point.

Let's summarize Cutting and Pasting:

Cut Text  (kill-region)

Windowed Mode (GUI)Text Mode
Highlight text; Edit->CutSet Region with Mark and Cursor; C-w

Copy Text  (kill-region)

Windowed Mode (GUI)Text Mode
Highlight text; Edit->CopySet Region with Mark and Cursor; M-w

Paste Text  (yank)

Windowed Mode (GUI)Text Mode
Edit->PasteC-y



Summary



Startup
GUI-modeText-modeAction
at the prompt: emacs fileemacs -nw fileOpen file in emacs

Open/Close Files
GUI-modeText-modeAction
File->Open File...C-x C-fFind (Open) File for editting
File->Save Buffer As...C-x C-sSave Buffer to File
File->Exit EmacsC-x C-cQuit Emacs
File->Close Current BufferC-x kClose (Kill) Buffer

Movement
GUI-modeText-modeAction
HomeC-aBeginning of line
 M-fForward one word
 M-aPrevious sentence
PgUpM-vPrevious screen
 M-<Beginning of buffer
EndC-eEnd of line
 M-bBack one word
 M-eNext sentence
PgDnC-vNext screen
 M->End of buffer
Notice that there are several more text based commands for moving around. Learning these can make you quit fast in the editor.

Cut/Copy/Paste
GUI-modeText-modeAction
Highlight text; Edit->CutSet Region with Mark (C-space) and Cursor; C-wCut
Highlight text; Edit->CopySet Region with Mark (C-space) and Cursor; M-wCopy
Edit->PasteC-yPaste