Lab 9
Emacs
Objectives
Before Starting
What is emacs?
Emacs is best known as an extremely powerful text editing program that is installed on most UNIX computers. You can use it to do just about anything. You can write, compile, and debug your programs without ever exiting emacs. You can also use emacs for your mail program, web browser, newsreader, UNIX shell, and much more! Sure beats notepad or pico! Since emacs is so powerful, we will only give you a brief tutorial of the text editing portion today. There is plenty of documentation included with emacs for you to figure out the rest.
Using emacs
When you first type "emacs" at the acme prompt, you will get a screen that looks something like this:

hit enter and the screen will clear, leaving you only with some instructions about the "scratch buffer." The screens that hold text are called buffers in emacs. You can work with multiple buffers at a time for maximum efficiency, but we'll work with just one for now. We'll talk about multiple buffers later.
Control keys
So what is all this "C-x C-s" stuff? If you read the information carefully, you will see that C means the Ctrl key on your keyboard, and M (meta) means the Alt or Esc key on your keyboard. If Alt doesn't work when you use a key combination, try Esc.
Tips:
Practice a little. It's not as hard as it may sound.
Canceling operations
If you accidentally type the wrong control keys for a command, you can cancel it by typing "C-g". This way you will not get stuck in unfamiliar territory.
File operations
Open existing file
You can do this by typing "emacs <filename>" at the prompt. If you're already in emacs, don't quit and restart emacs with the file name! Just type "C-x C-f <filename>".
Note: emacs has an extremely useful feature called "tab completion" that can save you from carpal tunnel syndrome. If you have a file called "pneumonoultramicroscopicsilicovolcanoconiosis.txt", you can type "C-x C-f pneu<tab>", and if you have no other files that start with "pneu", emacs will automatically fill in the rest of the file name for you. All you have to do is hit "enter" to accept the file name. If you do have two or more files that start with "pneu", emacs will give you a list of those files, and you can type in more letters and hit tab, or you can switch into the buffer listing the file names and choose one (more about buffers later).
Create new file
Do not edit text in the scratch buffer. If you exit emacs before saving the text in the scratch buffer, you will lose it. To create a new file, type "C-x C-f <filename>" and give emacs the name of a file that does not exist. Then if you forget to save and try to exit, emacs will remind you to save your file.
Saving the file
After you've typed a few words, it's time to save. If you are editing in the scratch buffer or want to give your current file a new name, type "C-x C-w" (w is for "write"). Emacs will prompt you for the new name. If you have already given a name to the file and want to save your changes to the same file, type "C-x C-s".
Note: emacs "auto-saves", but it's only a backup for when someone trips over the power cord as you were just about to save. You need to permanently write your changes to your file by invoking the save operations!!
Insert file
To insert a file into the current buffer, type "C-x i <filename>". Tab completion works here. The file will be inserted where the cursor is.
Exit emacs
When you are tired of working, exit emacs with "C-x C-c". If you have files open, it may ask if you want to save them. Answer with "y" or "n".
Moving around
"C-f" for forward one character.
"C-b" for back one character.
"M-f" for forward one word.
"M-b" for back one word.
"C-e" for end of line.
"C-a" for beginning of line.
"C-n" for next line.
"C-p" for previous line.
"C-v" for forward one page.
"M-v" for backward one page.
"M-<" for beginning of file (remember to use shift to get to <).
"M->" for end of file (remember to use shift to get to >).
Useful Text Manipulation Commands
Centering Text
Changing the Case of Text
Emacs has a convenient function for changing the case of text from upper to lower case or lower case to upper. Just place the cursor over the word you want to change and do the following:
Wrapping lines
If you use Windows programs or pico a lot, you will notice that they automatically wrap your text to the next line when your text fills up a line. This is not always desirable in Unix, so emacs does not do it automatically. Most users of emacs just press Enter when they want text to go on the next line. If you find that you are too used to Windows and pico, these are the emacs line-wrap commands:
Cut & paste
"C-k" cuts from the cursor point to the end of the line, and "C-y" pastes the line(s) that were cut.
You can also select text and cut/copy by marking a region:
| 1. | Set the mark (where you want the selection to start) by typing "C-@". The mark won't show up, so you have to remember where it is. | |
| 2. | Move the cursor to then end of the selection. The mark will be deactivated if you edit the buffer before you finish these steps, so if you accidentally edit something, type "C-x C-x" to reactivate the region. | |
| 3. | To cut the region, use "C-w". To copy the region, use "M-w". |
Find & replace
For the following commands, put your cursor where you would like to start searching and replacing (this is usually the top of the file).
Goto Line
To go to a particular line, type "M-x goto-line", then type in the line number that you want to jump to.
Undo
"C-x u" or "C-_" (remember shift key) will undo your last change. If you keep using undo, emacs will undo older and older changes. You can undo as many changes as emacs has stored.
Spell checking
On Unix systems, there is a program called ispell that can check the spelling in your text files. When you do "M-x ispell-buffer", ispell will run within emacs. It will flag questionable words and give you suggestions in a small buffer at the top of your screen. Hit Space to move on and ignore the flagged word or type the number that corresponds to the suggested word that you want to use. You can also hit "?" for more options. Unfortunately, ispell is not set up correctly on acme, so you cannot use ispell within emacs.
Buffers
What are buffers?
As said before, the screens that hold text are called "buffers" in emacs. By working with multiple buffers at a time, you can look at the top and bottom of a file at the same time, look at multiple files at once, and edit, compile, and run the programs you write without exiting emacs. These are only a few of the useful things you can do with buffers.
Buffer command
"C-x 2" divides the current buffer into 2 horizontal buffers.
"C-x 3" splits the current buffer into 2 vertical buffers.
"C-x 1" hides the other buffers and makes the current buffer take up the whole screen.
"C-x 0" (zero, not the letter O) deletes the current buffer.
"C-x O" (the letter O for Other) moves the cursor to the other visible buffer(s).
"C-x C-b" to access a list of buffers. Move the cursor into that buffer. To view a certain buffer, move the cursor over the name of it and press Enter.
Getting help
Apropos
If you do not know a command name, type "M-x apropos", and enter a descriptive word or phrase to search for.
Tab completion
You can also type "M-x <tab>" or "M-x <part of command><tab>" and emacs will help you out by giving you a list of matching commands. If the list is too big, use the search feature.
Tutorial
There is an extremely helpful tutorial that you can play with if you do "M-x help-with-tutorial".
FAQ
For answers to questions like "How do I turn on auto-fill mode by default?", use "M-x view-emacs-FAQ" (case sensitive!).
Your Assignment: Editing a text file with emacs
You will do some basic text editing with emacs. Download the files from here and here.
Hint: Many of these tasks require you to go from the top of the file to the very
bottom and then back. Look up the commands to jump to the beginning and end of your file
to make your life easier.
Procedure: