University of Minnesota Morris Computing Services

 
UMM Home > Computing Services > Guides > Text Editors > Emacs text editor

Emacs text editor

What is Emacs?

Emacs is a powerful text editor with support modes for languages such as HTML, Java, C++, Scheme, and so on. These modes provide features like automatic parentheses matching.

How to start Emacs

To start Emacs, at your prompt, type 'emacs' or 'emacs filename' and press enter to start Emacs. If you entered a file name after Emacs, Emacs will try to open the file for editing. If the file did not exist, when you save, Emacs will save to that filename.

Once Emacs has started

The interface for Emacs is much more complex then pico's, and first time users may find Emac's system daunting.

The most basic commands one needs to operate emacs are the commands to open files, save files, and exit Emacs. Emacs's commands use the following syntax:

C = control (ctrl) key
M = alt key on PC's / apple key on Mac's

The commands are combinations of the control and meta key and other letters, so exit emacs which is C-x C-c, is typed by holding the control key and pressing the x key then the c key while still holding the control key.

Function Keystrokes Description
find-file C-x C-f Opens the specified file
save-buffer C-x C-s Saves the current buffer to a file
save-buffers-kill-emacs C-x C-c Save buffers and exit emacs

Another way to access commands in Emacs is to type M-x followed by the command name. M-x find-file is the same as C-x C-f. All function names provided in this hand out will be accessible in this manner. So to save a file, you can type M-x save-buffer or press C-x C-s.

If you wish to edit a new file, you can use find-file (C-x C-f) and type in what you want the file to be named. Emacs will open a new buffer and will save to the specified file name.

Basic Editing

Basic movement within Emacs can be done with only the arrow keys and the page up and page down keys.

Emacs provides an undo function, with the key command C-x u (Control x then just u) which will undo the last thing done. This can be entered repeated to undo a series of commands or typing.

To cancel a command, use the key command C-g. C-g will stop command entry and return you to editing if you accidentally start entering a command. So if you've hit M-x accidentally, pressing C-g will cancel the command sequence.

Function Keystrokes Description
undo C-x u Undo
quit C-g Cancels the current command

You now have the commands to do basic editing on Emacs. The follow section will show some of the more advanced commands for using Emacs. These allow for more efficent use and customization of Emacs but are not necessary for basic use.


Window Management

While not necessary for basic use of Emacs, understanding how to use the windowing system of Emacs will help most users at some time or another. Ocassionally, Emacs commands (entered intentionally or unintentionally) will open up a second window within Emacs to display data of some sort. The following commands will deal with switching between windows and closing windows.

Function Keystrokes Description
other-window C-x o Select another window
scroll-other-window C-M-v Scroll the next window
delete-window C-x 0 Deletes the current window
delete-other-windows C-x 1 Deletes all windows othen then the current window

Movement

These commands provide other methods than just the arrow keys to move around within the file you are editing.

Function Keystroke Description
previous-line C-p Up one line
next-line C-n Down one line
forward-char C-f Forward one character
backward-char C-b Backward one character
forward-word M-f Move one word forward
backward-word M-b Move backwards one word
beginning-of-line C-a Move to beginning of line
end-of-line C-e Move to the end of the line
scroll-up C-v Move down one page
scroll-down M-v Move up one page
beginning-of-buffer M-< Move to the beginning of the file
end-of-buffer M-> Move to the end of the file

Killing and Deleting Text

When you Kill a portion of text it is copied into a buffer so it can be re-inserted later (much like the 'cut' in Windows©). Deleted text is not copied to a buffer.

Function Keystroke Description
kill-word M-d Kills word under the cursor
backward-kill-word M-<DEL> Kills the word preceeding the cursor
kill-sentence M-k Kill to the end of the sentence
backward-kill-sentence M-<DEL> Kills to the beginning of the sentence
kill-line C-k Kill the rest of the current line
zap-to-char M-z CHAR Kill through to the next occurrence of CHAR
delete-char C-d Delete next character
delete-horizontal-space M-\ Delete spaces and tabs around point
delete-blank-lines C-x C-o Delete blank lines around the current line
delete-indentation M-^ Joins two lines by deleting intervening newline and any indentation

Yanking (Pasting) Text

In Emacs, Yanking is analagous to Windows© Paste.

Function Keystroke Description
yank C-y Yank last killed text
yank-pop M-y Replace text just yanked with an earlier batch of killed text

Searches

Emacs provides multiple options for performing a search. Two are incremental and nonincremental search. With incremental search, Emacs will search for the string as you type it. So as you typed in 'FOO', Emacs would search for 'F' then 'O' (effectively 'FO') then 'O'. You can edit the search string as you type it, but you have to wait for Emacs to find or not find the character you want to delete before being able to change it. Nonincremental searches require the entire search string before beginning the search.

Function Keystroke Description
isearch-forward C-s Incremental search forward
isearch-backward C-r Incremental search backward
search-forward C-s <RET> STRING <RET> Nonincremental search forward
search-backward C-r <RET> STRING <RET> Nonincremental search backward

Emacs can also do Word searches. In a Word search, you can enter a string of words, seperated by single spaces. When Emacs searches for them, it will disregard any multiple spaces, tabs, newlines between them in the text.

Function Keystroke Description
word-search-forward C-s <RET> C-w WORDS <RET> Search forward for WORDS ignoring punctuation
word-search-backward C-r <RET> C-w WORDS <RET> Search backward for WORDS ignoring punctuation

Abbreviations

Emacs allows you to define an "abbrev", which is a word that has been defined to expand into a specified explansion. To use abbreviations, you must turn the Abbrev minor mode on. This can be done through 'M-x abbrev-mode', which toggles whether Abbrev is used or not. Defined abbreviations are not lost when the mode is toggled.

When abbrev-mode is enable, to trigger a defined abbreviation, just type the abbreviation and press the spacebar or any other non-word character. Emacs will then insert the expansion of the abbreviation.

Function Keystroke Description
abbrev-mode M-x abbrev-mode Toggle Abbrev mode
add-global-abbrev C-x a g Define an abbrev, using one or more words preceeding the point
inverse-add-global-abbrev C-x a i g Add a word in the buffer as an abbrev
add-global-abbrev C-u 0 C-x a g Define abbrev using region from point to mark as expansion
list-abbrevs M-x list-abbrevs Displays a listing of all abbrev definitions
edit-abbrevs M-x edit-abbrevs Edit the list of abbrevs
write-abbrev-file M-x write-abbrev-file <RET> FILE <RET> Write file FILE with all defined abbrevs
read-abbrev-file M-x read-abbrev-file <RET> FILE <RET> Read file FILE and define abbrevs accordingly
quietly-read-abbrev-file M-x quietly-read-abbrev-file <RET> FILE <RET> Similar as above except without messages

Selecting Regions to Edit

In Emacs, a region is defined as the space between the Mark and the Point (the cursor). To begin defining a region, use C- to set the Mark where the Cursor currently is. Next, move the cursor to where you want the region to end. Then you can perform any of Emacs region editing commands. Note: selecting a region and then hitting 'del' or 'backspace' will not result in the region being deleted. Emacs has a series of commands to work with regions, so the plain keyboard keys will still only affect single characters.

You can switch the Mark and the cursor around by pressing C-x C-x. Note that the region is always between the mark and the cursor, no matter which one comes first.

For example, type the sentence "Only here should be uppercase." Now, move the cursor over the 'h' in here and press C-. Next move the cursor past the last 'e' in here (so it is on the space). Enter the command C-x C-u. You should now see "Only HERE should be uppercase."

The difference between the point and the cursor is that the cursor is shown on a character, while the point is between the character preceeding and under the cursor. So if you placed the cursor over the last 'e' in here instead of the space, it would have appeared as "HERe" because the point was between the 'r' and the 'e'.

Function Keystroke Description
set-mark-command C-<SPC> Set mark at the cursor
set-mark-command C-@ Same as above, but sometimes C-<SPC> doesn't work on certain machines
exchange-point-and-mark C-x C-x Swaps positions of the cursor and mark

Region Commands

These are commands you can use when you have a region selected.

Function Keystroke Description
kill-region C-w Kill region from point to the mark
kill-ring-save M-w Save region as killed text without killing it
downcase-region C-x C-l Convert the region from point to mark to lower case
upcase-region C-x C-u Convert the region from point to mark to upper case
indent-region C-M-\ Indent the region from point to mark

Additional Help

To Obtain Further Help, You Can access the Emacs info system by typing C-h i and then move the cursor over Emacs and hit enter. This will open a menu containing all of Emacs' help files.

Also available online are many excellent Emacs HOWTO's. Many linux sites have excellent guides of various skill levels of Emacs users. A search online for Emacs HOWTO will yield a wide array of helpful sites and files.