Introduction to Emacs

Created by Ian Kelly and Nate Sommer


Contents:

1) Launching Emacs
2) Commands
3) Buffers
4) Frames and Windows
5) Basic Editing
6) Syntax Highlighting
7) Compiling
8) Customizaing Emacs


1) Launching Emacs

Emacs window

To launch Emacs through VTWM open up the main menu (left-click on the root window), go into the "Applications" sub-menu, and select "emacs". Once the program has been launched, you should open a file. If you click on the "File" menu and click on "Open File..." you'll notice the small white rectangle at the bottom of the screen now reads "Find file: ~/" followed by a blinking cursor. This space is called the "mini-buffer" and is where commands are typed and messages are displayed. You can now type the name of a file to open.

the mini-buffer

You may have noticed that in the "File" menu there is no "New File" option. If you want to open a new file, type the name of the file and Emacs will create the blank file for you.

Back to Contents


2) Commands

You may have noticed in the "File" menu that many of the items have things in parentheses to the right of them. For example, "Open File..." has (C-x C-f) next to it.

file menu

"C-" followed by a key means to press that key while holding down the Ctrl key. So to open a file you hold down the Ctrl key and press x, then let go of x and press f while still holding down Ctrl. These keystroke commands can speed up your work considerably as clicking through menus can be tiresome and distracting.

Back to Contents


3) Buffers

When Emacs opens a file it is read into a "buffer". Once you have opened a file, click on the "Buffers" menu and you will see the name of your file at the top of the list, along with two other buffers. The *messages* buffer is a list of messages Emacs has displayed in the current session, and the *scratch* buffer is a scratch pad to write little notes that will not be saved. Don't worry about these buffers too much. If you open another file, the other files you have open are still available to you in the "Buffers" menu. Buffers that have been changed since they were last saved are followed by a space and an asterix.

the buffers menu

Back to Contents


4) Frames and Windows

The Emacs "window" you see in front of you is called a "frame". Since it is often useful to be able to see more than one file at a time, new frames can be opened within the same Emacs session. Click on the "File" menu and select "New Frame". You will be presented with a new frame displaying the same buffer as the first frame. Try editing the buffer in one frame, then look at the other frame. Your changes will be reflected on both, since both frames are displaying the same buffer in the same Emacs session. The "Buffers" menus in each frame contain the same buffers as all the other frames in the same session, so any buffer can be edited in any frame at any time. Another way to view multiple buffers at the same time is by splitting a frame into multiple windows. Go to the "File" menu and select "Split Window" (or better yet, press (C-x 2)). The frame is now divided into two "windows". As with the multiple frames, each window shows the same buffer at first. You can change which buffer is shown in each window with the "Buffers" menu. You can change which window you're editing in by clicking in the text area of a different window.

2 windows

Back to Contents


5) Basic Editing

Open a file with some text, or write several lines of text in a new file. To copy text, all you have to do is highlight it with your mouse. To paste, move the cursor where you want the text and click the middle mouse button. Tada! To cut (or "kill," as Emacs refers to it) a section of highlighted text, use C-w. The killed text can now be pasted. You can see the history of text you've killed and copied in the "Edit" menu under "Select and Paste." Clicking on one of these will paste the text wherever the cursor is.

It's often handy to kill an entire line of text. Place the cursor at the beginning of a line and press C-k. This command kills all the text after the cursor in the current line.

Emacs shows you what line your cursor is currently on in the gray bar just above the minibuffer. To jump directly to another line, press M-g (the M here stands for Meta, which is essentially the same thing as Alt. So whenever emacs says "M-x" that means press x while holding down Alt). The minibuffer now prompts you for a line number.

When editing source code Emacs will automatically indent lines for you. This makes the Tab key behave a bit differently than you might expect. Pressing Tab indents the current line however Emacs believes it should be indented, so if the line is already indented correctly the Tab key will do nothing.

Back to Contents


6) Syntax Highlighting

Emacs identifies source code and the language it is written in through file extensions. If you open a file called helloworld.cpp, Emacs believes the file to contain code written in C++. Emacs highlights text with different colors in source files according to syntax and keywords, which makes editing much easier.

syntax highlighting example

This feature can be turned on and off via the first item in the "Options" menu.

Back to Contents


7) Compiling

Once you have code that you want to compile, you don't have to leave Emacs. If you've written the code in C++ and your file's extension ends in .cpp, you should see a "C++" menu. Click on it and select "Compile...". A compile command now shows up in the mini-buffer. The default compile command is often what you want, but there will be times when you'll want to change the command. Once the command is satisfactory, hit enter. If any of the buffers in the Emacs session have not been saved, you will be asked in the mini-buffer if you would like them saved before compiling begins. This is very nice since many programs span multiple files and you'll often forget to save all the buffers you've made changes to (this is also where the advantage of using multiple frames and windows, as opposed to multiple Emacs sessions, comes in). Once all the files are saved (or not saved, if that's what you choose) a new buffer appears in the lower window to show the compiler's progress.

compiling

If the compilation was not successful, errors will be displayed. You can cycle through the places in the code where the errors were detected by clicking on the "C++" menu and selecting "Next Error" or by middle clicking on the error in the *compilation* buffer. This causes the cursor to jump to the appropriate line of code.

stupid curly braces!!

Back to Contents


8) Customizing Emacs

Emacs is very customizable. All of your customizations are stored in the file .emacs, which is in your home directory. Files that begin with a period are hidden, but you can type "ls -a" at the command line to view all the files in a directory, including those that are hidden. Type "less .emacs" to check out what's in your current .emacs file.

Important: Before you start editing this file, it is a very good idea to make a backup. This is not meant to scare you, but if you screw up the file you're going to temporarily screw up the functionality of Emacs. It's pretty easy to get it working again if you have a backup. Make a copy by typing "cp .emacs .emacs.old". .emacs.old is now a working copy of the file that you can revert to if things go bonkers. You can now feel free to put some random garbage in your .emacs file, if you really want to see what happens.

An easy way to customize some of Emacs's basic functions is through the "Customize Emacs" menu under the "Options" menu. Click on the "Top-Level Customization Group" menu item. A buffer will open that has a bunch of customization groups. Go to the "Editing" group, and then to the "Indent" group. The "Standard Indent" option is what controls the auto-indent size. Try changing the 4 to 8, click on the "State" button, and select "Save for Future Sessions." This writes the changes to your .emacs file. You can go back and look at .emacs again to see what changes were made. If you want to go back to the default standard indent setting, click on the "State" button and select "Erase Customization," but feel free to set this option, and any other options, to whatever you want. It's your .emacs file, it's there to serve you!

As you get more comfortable using Linux you'll find that there is room for customization all over the place. Check out the tutorials on editing .xsession and .vtwmrc to customize your desktop. Don't be afraid to play around, and if you screw something up, don't be afraid to ask a CS major who has been around for a while for some help. We've all been in your shoes, and we're pretty friendly :)

Back to Contents


© 2002-2004 Earlham College Computer Science Department
Last updated: