UNIVERSITY of VIRGINIA
Computer Science
Research  Teaching  People  Community   

Search
Directory
Contact Us
 
Undergrads • Grad Students • Faculty • Staff • Alumni • Locator • Phones

Emacs Tutorial

Emacs. Powerful enough to strike fear in the hearts of Word users and envoke elation in Unix geeks. This tutorial is designed to introduce the most important and useful features in Emacs and provide links to more information. A more complete list of commands can be found at http://www.geek-girl.com/emacs/refcard.html.

In this tutorial commands will be designated by C-command and Esc-command. For C-command, hold down the Control key and whatever the "command" is. For multiple key sequences, press them sequentially, not simultaniously. With Esc-command, press and release the Esc key, also called the Meta key, and then press the command key. The commands are echoed on the bottom of the window, along with emacs messages.

The best way to become good at using emacs is to practice and play around with it. Ten minutes of practice can often result in an hour of saved editing time. This is intended to be an introduction. For more information, see Learning GNU Emacs.

As expected, emacs is started by typing emacs. If you want emacs to start and open a file, type emacs filename. To exit emacs, type C-x C-c.

Sometimes in emacs you might accidentaly type in a sequence of keys or get lost in some command. C-g is the all-purpose exit command. It automatically "normalizes" your window.

With emacs started, you can open a file with C-x C-f. This brings up a line on the bottom of the screen in which you can type in the file name.

To save the current file, execute C-x C-s.

To mark a region of text, go to the beginning of the region and press C-spacebar. This sets the mark. You can then go to another point further down in the document and execute a command on all of the text between the cursor and the mark. To kill, or cut, the entire region, press C-w. To copy the region, press Esc-w. To then paste the killed or copied text, press C-y

If the text you have is "choppy", you can straighten it up with Esc-q.

To search forward for a string, use C-s. This will bring up a line at the bottom of the screen you can type your string into. Emacs automatically starts searching for the string you type in. Press any arrow key to stop where the text is found, or press C-g to return to where the search was started.

Esc-% is the command for a search-and-replace. Enter the text to find, press enter, and then the text to replace it with, and enter. When asked if you want to replace the text, press the spacebar if you want it replaced. The Delete key skips the current one and goes to the next occurance. A ! will replace all of the occurances in the document. Esc exits the s-a-r.

To convert all occurances of three or more spaces into a tab, use Esc-x and then type tabify. This is very useful in making tables from a scanned document, combined with replace commands.

Another useful thing is the rectangle commands. A rectangle is made by setting the mark, C-spacebar, and then moving the cursor to where you want the other corner of the rectangle to be. To kill the rectangle, use C-x r k. To copy it, use C-x r r. To then paste the rectangle, use C-x r y. To insert a blank area where the rectange is, use C-x r o. To just make the selected rectangle blank, use M-x and type clear-rectangle.

Here is the way I handle the ^M in files. Put this in your .emacs: (defun dos-1 () (interactive) (goto-char (point-min)) (while (search-forward "\r" nil t) (replace-match ""))) (defun 1-dos () (interactive) (goto-char (point-min)) (while (search-forward "\n" nil t) (replace-match "\r\n"))) IP don't usually bind these to keys, but you certainly could. When you call the function M-xdos-1, it will delete all of the delete all of the characters in the file. And of course the other function will put them back.



UVa CS Department of Computer Science
School of Engineering, University of Virginia
151 Engineer's Way, P.O. Box 400740
Charlottesville, Virginia 22904-4740

(434) 982-2200  Fax: (434) 982-2214
Web Comments: webteam@cs.virginia.edu
Admissions Inquiries: inquiry@cs.virginia.edu
Site directory, Other addresses
Server statistics
© Created by the CS Web Team