A Beginners Guide to Emacs

Nicholas Szandor Hakobian; updated by Carl Heiles

University of California, Berkeley, Undergraduate Radio Astronomy Laboratory Version 2, January 17, 2017

                            nick@ugastro.berkeley.edu

Jan 2017: When logged in from within the lab, to make Emacs function properly you must enter the following command (including the [] ). You only need do it once—not every time you log in):

    gsettings set org.freedesktop.ibus.general.hotkey triggers []

1. So What Is Emacs?

Emacs is a text file editor in Unix environments. This includes our lab which runs a variant of Unix called Solaris, and other, more readily available operating systems such as Linux. You can use Emacs to quickly edit a simple text file, as an editor for your IDL source code, or as a nice front end to typesetting documents in L^{A}TEX, the required typesetting utility in this class. This document will outline the basics of using Emacs.

2. The Basics

2.1. The -nw Option

Emacs is normally run in a specially-created X-window. This can make it slow if you are running on a remote computer, e.g. as an astronomer who is editing a file on a distant telescope’s computer system. You can get around this by running Emacs in the window from which you invoke it; the formatting is a little less fancy and mouse won’t work, so you have to use the keyboard shortcuts, but the response time is much quicker. To run Emacs without generating a new window, type emacs -nw filename instead of emacs filename; the -nw means ‘no window’.

2.2. Opening

Most of the programs that you will be using on these systems will be started from the command line. To begin a new document, simply navagate (via the Unix command line) to the directory where you want the file to be and type:

emacs something.txt &

or
emacs -nw something.txt &

Where something.txt is the new file that you wish to create, or the old file that you wish to update. Without the -nw option, after a few moments a new window will popup with a blank screen that you can type into. You can use the mouse to navigate the menus at the top. See table 1 for information on what the different menu options do.

Buffers  Contains information about the currently open files in this instance of

Emacs.

Files  Contains basic commands for opening, saving, and closing files.
Tools  Contains advanced functions for doing things like version control.
Edit  Contains cut, paste, and spell checking capabilities of emacs.
Search  Contains search and replace functions.
Mule  Advanced editing functions.
TeX  LaTeX specific functions. Only appears when the filename you are

editing ends with .tex.

IDL or IDLWAVE IDL specific functions. Only appears when you are editing a filename

that end with .idl or .pro

Help  Emacs’ extensive help and configuration area.

Table 1: Emacs Menu Items

Now that you have emacs open you can type something in the text area. In the next section we will save the file.

2.3. Saving

In Emacs, saving a document is as simple as π. You can click on the Files menu, then click on Save Buffer... You can also save by using the keyboard shortcut C-x C-s. In the interest of simplifing your life, this document will concentrate on the usage of keyboard shortcuts in using Emacs.

2.4. Keyboard Shortcuts

Just about every aspect of Emacs can be accessed and configured with keyboard shortcuts. This means that to work on a document, you rarely need to move your hands away from the keyboard and use the mouse. This increases productivity, and just makes things easier. All the keyboard shortcuts that I mention in this document will be summarized at the end.

All keyboard shortcuts are represented in a format like C-x C-f. When you see a -, that means you hold the key that comes before and after the - down at the same time. The capitol C however, does not correspond to the letter c, but rather the control button (abbreviated CTRL) on the keyboard. So, if you see C-x C-f, that means press and hold the CTRL button, then press the x key, then press the f key, then release the CTRL button. If you see something of the form C-x d, then that means to press and hold the CTRL button, press x, release CTRL, then press d. I think you get the picture.

In some of the menus, you will see keyboard shortcuts that are labelled as M-% . On the Sun workstations that corresponds to the ◇, and works identically to the CTRL key. On non-Sun workstations this key is simulated by pressing ESC then CTRL-whatever.

Some basic keystrock Shortcuts: Opening - C-x C-f Saving - C-x C-s Exiting Emacs - C-x C-c Pasting - C-y Deleting a line - C-k Move to the beginning of a line - C-a Move to the end of a line - C-e

2.5. Cutting, Pasting, and Navigation

In Emacs cutting and pasting is simple, yet slightly different from what you may be used to. To do it using keystrokes only:

1. Select and cut the block of text by

(a) moving the cursor to its beginning, type C-spacebar.

(b) using the arrow keys, move the cursor to its end abd type C-k.

2. Paste the block by

(a) moving the cursor to where you want to paste

(b) type C-y .

To do it using the mouse: Select the text with your mouse and the double right click in the selection. To paste, move the cursor to where you want to paste and click the middle mouse button.

3. L^{A}TEXIntegration

Emacs has nice L^{A}TEXintegration. If you start a file with the ending .tex, Emacs will automatically load up in a mode to highlight special characters and special modes to make life a little easier. Emacs also has a couple modes to automatically compile L^{A}TEXfiles and display the result in xdvi (refer to the L^{A}TEXhandout if none of this makes sense to you. Below are the basic L^{A}TEXshortcuts.

Compile L^{A}TEXfile C-c C-f
View file in xdvi C-c C-v

Table 2: L^{A}TEX- Emacs commands

4. IDL Integration

Emacs also has a nice mode for integrating with IDL. For the purposes of this lab, we will only need to use the highlighting features of Emacs. This checks to make sure that you have matching parentheses, that you correctly close if...then...endif statements, and will color special keywords separately from other commands. It is recommended that you execute your IDL files from the IDL command prompt.