4.2. 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. In addition you would use the arrow keys to move around, plus the Backspace and Delete keys.
Command | Meaning |
---|---|
At Terminal prompt: emacs -nw file | Open file in emacs |
In Emacs: C-x C-f | Find (Open) File for editting |
In Emacs: C-x C-s | Save Buffer to file on disk |
In Emacs: C-x C-c | Quit Emacs and Exit |
Of course we’d like a little more functionality.
4.2.1. The GUI version of Emacs¶
Let’s bring Emacs into the 1990’s.
On the previous pages I had you start emacs with the command: emacs -nw which opens emacs in a text-mode terminal window (not a GUI window). Now let’s open emacs in GUI-mode. Open a terminal and at the prompt type:
emacs
Just emacs: NO -nw switch!
The naked emacs
command should 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:
- the Toolbar
- the Buffer Area
- the Mode Line
- the Mini-Buffer
Can you identify them all? In addition you have icons in the 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 in the Toolbar.
Click on ” Open File...”.
This opens the File browser window.
Compare this with the command line directory listing, using your “l” (ell) alias for ls -F
.
Going back to the Open File window, you see your directories and any normal files there.
But suppose you needed to edit your .bashrc
file, which is there but not listed because its
a Hidden unix file–it starts with a . (dot). If you need to see all the files in
a directory, including Hidden files, ...simply click the Hidden Files button on the Open File
explorer window.
For the moment, Click Cancel and close the Open File explorer window. We’ll come back to it in a minute.
The Emacs window should still be open. This time (with the mouse somewhere in the emacs window), type:
C-x C-f (Ctrl-x Ctrl-f).
In mini-buffer at the bottom, you will see: Find file: ~/
Recall ~/
is an alias for YOUR HOME DIRECTORY
, which in my case is /home/jhetrick/
.
Notice also that the cursor is waiting for you to type a file name that would be in your home directory. You could, for example, type the path to a file that is in a subdirectory by typing the path to the file.
For example, with the Find file: ~/
prompt still showing in the mini-buffer, type unixplay/ and hit
the TAB button, twice. The TAB tells emacs to try to complete the file name for you. Since there are many
options in the ~/unixplay/
directory, emacs displays them by opening another buffer:
You can click on the file you want in that window, and emacs will open it. You can even click on one
of the subdirectories in the ~/unixplay/
directory.
For example, Click on dirTWO/
, emacs will add dirTWO/
to the path in the mini-buffer.
Find file: ~/unixplay/dirTWO/
If you hit TAB again (twice), you can play the same game in the ~/unixplay/dirTWO/
directory. Emacs
will show you all the files (and subdirectories) there and you can choose one of those to edit.
At this point, if you’ve been following along, the mini-buffer should be waiting for you to choose a file
in the ~/unixplay/dirTWO/
directory.
Before we open a file, let’s Abort this Find File process. This is a very useful thing to know how to do. Suppose you type the wrong command sequence, or decide to do something different, you can always ABORT the current command by typing C-g (Ctrl-g).
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), by typing C-g.
You can also Abort an action by clicking on the Red (X) in the Toolbar.
Note
To Abort a command, type: C-g (Ctrl-g), or the Red (X) in the Toolbar.
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, something like:
zzz
Do you see the (New File)
text in the mini-buffer?
Now type some characters in the buffer window. Anything will do.
OK. 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] (i.e. don’t kill it)
Now let’s do the same action: kill the buffer, using text-based commands. In the emacs window, where you still have the new zzz buffer, type:
C-x k (Ctrl-x, followed by typing the k key)
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.
Last thing: let’s open a file, edit, and save it.
At the terminal prompt, type
emacs testfile.txt
(I always give my files a suffix .txt
when they are just text)
This should open emacs with two screens. In the top is your new file, testfile.txt
. In the lower
window is the Welcome to *EMACS screen.
You don’t need the Welcome Screen any more, so click on the Checkbox which
says “Never show it again
”,
then click the line of text to its left, which says “Dismiss this startup screen”. The Welcome screen will go away, and never come back.
Click in the upper window, and type your name.
Before you Save the ``testfile.txt`` file, notice that after you typed your name, the *mode-line,
where emacs shows you useful information, there are two new \*\*
‘s. This means that the buffer
is modified–there are changes that need to be saved.
Now, to Save this “buffer” to a file, you surely know to Click File->Save
in the Tool Bar.
If you do so, nothing happens, except those \*\*
‘s disappear.
Recall: To do the same with a Text Mode command, the command is: C-x C-s (Ctrl-x Ctrl-s).
At this point you can use either the intuitive GUI (menu/mouse) mode to click on buttons, or the Text mode keyboard commands, to open, modify, and save
Since there are senior Computer Science students taking this course along side freshmen Physics students, let me through out a little candy for those already familiar with remote logins and ssh (don’t worry freshmen, we’ll learn about those in Chapter 10).
Edit a file on a remote unix machine
Suppose you have an account on another machine called: secrets.nca.gov
.
From your laptop, you can do:
C-x C-f (to open a file)
Then, erase (backspace over) the ~/ in the mini-buffer and put for the filename:
/secrets.nca.gov:missilecodes.txt
Note the beginning “/” (without the usual ~
), and the “:” separating the filename.
This tells emacs to open the remote file for editting locally. Emacs will open an sftp connection, grab your file (asking for your remote password, of course) and open it in a buffer on your laptop here. When you save it, with: C-x C-s it goes out through the network and puts the new version back “over there”.
4.2.2. GUI v. Text¶
Let’s summarize, the GUI and Text-mode methods.
To open a file (find-file ) |
|
GUI (menu/mouse) Mode | Click: File->Open File |
Text Mode | type: C-x C-f |
To kill a buffer (kill-buffer ) |
|
GUI (menu/mouse) Mode | Click: File->Close |
Text Mode | type C-x C-f |
To save a buffer (save-buffer ) |
|
GUI (menu/mouse) Mode | Click: File->Save |
Text Mode | type C-x C-s |
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 then cd into it.
cd editplay.
Now save the following file into this directory: stairway.txt
Remember, you can download the file with the curl
command, by copying and pasting
this text to the command line:
curl -O http://dirac.physics.pacific.edu/phys/jhetrick/www/phys27/download/stairway.txt
(I know I could have included this file in the unixplay.tar.gz
bundle, but I wanted
to remind you about the very useful curl
command. Just remember you need the
-O (minus capital Oh) switch to save the file to your current directory)
Open it in emacs (use the GUI version of emacs), by typing
emacs stairway.txt
4.2.3. Movement¶
Let’s start by summarizing movement commands.
In graphical GUI mode, things work pretty much as you would expect from any other word processors like MS Word, Notepad, etc.
Most normal movement cammands work:
- Right, Left, Up, Down Arrows
- Page Up, Page Down
- Home, End
- and the Scroll Bar on the left side
Go ahead and try these.
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 (recall: C- means Ctrl-, and M- means ESC then key)
Emacs Text-mode Movement commands | |
C-a | Beginning of line (Home) |
C-e | End of line (End) |
M-f | Forward one whole word |
M-b | Back one word |
M-e | Next sentence |
M-a | Previous sentence |
C-v | Next screen |
M-v | Previous screen |
M-< | Beginning of buffer |
M-> | End of buffer |
Notice that the GUI version of Emacs (the window you have open) recognizes all the Text-Based commands as well. In fact, when I am in GUI mode, I still use the text-based versions of most commands. It’s a lot faster–your hands don’t have to leave the keyboard to grab the mouse, so you can really learn to type documents fast.
Try all of the text-based movement commands above now on the stairway.txt
file.
4.2.4. Cutting and Pasting¶
4.2.4.1. In 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 your favorite stanza 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 typing 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.
4.2.4.2. In Text-mode¶
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, or sometimes in GUI mode when you don’t want to grab the mouse.
In your open emacs window, using the arrow keys, move the cursor to the beginning of a stanza of the song. Now, Set the Mark, by typing
C-spacebar (Ctrl-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 (when you typed C-spacebar), move the cursor to end of the stanza, using the arrow keys. Now type
C-w (cut-region
)
This is the text-mode key sequence that does the “Cut”.
Similarly, the text-mode key sequence to “Copy” is
M-w (copy-region
).
The text-mode key sequence to Undo is
C-x u (undo
)
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 or Edit->Copy, put the cursor where you want to put the text, and click Edit->Paste. Pretty easy.
In Text-Mode , the Paste
command is
C-y (“y” for yank, as in “yank it out of the clipboard, and put it here).
Go back and “Cut” a stanza again:
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.
Kill a Whole Line of Text
Another very useful thing to do is to kill a single line of text.
Move your cursor to the beginning of a line, although you actually can do this anyhwere in the buffer. Type:
C-k
This will kill all the text from the cursor to the end of the line.
Try it on the stairway.txt
file.
Put the cursor at the beginning (or the middle) of a line of text.
Type:
C-k
Notice that this does not delete the empty line, but merely removes the text. If you also want to also delete the blank line, hit C-k again. So, typing C-k C-k kills both the line of text, and moves the text below, up by one line.
The text that was removed is now in the Clipboard (or “yank buffer”), and so if you want to place it somewhere in your document, you simply move the cursor to that point, and type C-y.
If you did the double C-k C-k, then the blank line (actually the trailing newline character) is also saved in the Clipboard. So, if you paste (yank) the text into a new location, it will now move the rest of the text down one line.
You will need to practice a little, so go ahead and hack up the stairway.txt
file
by cutting and pasting lines, parts of lines, etc. Use both the single C-k and
the double C-k C-k to get used to the difference.
Let’s summarize Cutting and Pasting:
Cut Text (kill-region ) |
|
GUI (menu/mouse) Mode | Highlight text; Edit->Cut |
Text Mode | Set Region with Mark and Cursor; type: C-w |
Copy Text (copy-region ) |
|
GUI (menu/mouse) Mode | Highlight text; Edit->Copy |
Text Mode | Set Region with Mark and Cursor; type: M-w |
Paste Text (yank ) |
|
GUI (menu/mouse) Mode | Highlight text; Edit->Paste |
Text Mode | Place Cursor at insertion point; type: C-y |