emacs primer

Goals

  • Learn how to create and edit a file in emacs
  • Learn how to move around in emacs

Introduction

Emacs is a powerful editor found on most UNIX systems. It is capable of many many things, but we will use it simply as a tool to create and edit files. It makes extensive use of keystrokes, but with only a handful you can accomplish quite a bit. Note: emacs is not required for any part of the course, so if you already know how to use another editor of your choice, feel free to skip this primer.

Starting emacs

Before we start, locate the control key on your keyboard. Emacs uses this key a lot. The control key is like the shift key, you press it while also pressing another key.

There are essentially two ways to start emacs.

command description
emacs -nw this launches emacs in the current window, without spawning a new window (-nw = no window)
emacs & this will open emacs in a new window, and give you your command line back in the current window

When emacs is running, the part that you see is called a "buffer". In most cases you would launch emacs with the goal of editing or examining a file. To do this, you simply supply the filename as an argument to emacs. If the file does not exist, emacs will create it.

emacs -nw newfile

The 4 essential keystrokes

To use emacs to edit files, you really only need to know four keystrokes (and really just the first two):

  • control-x control-c quit emacs
  • control-x control-s save file
  • control-g stop whatever is happening
  • control-x control-w save as (save current buffer in a new file)

With these 4 keystroke combos you can make emacs your friend for file editing. Emacs can be complicated; there are modes for editing files, editing directories, running commands, and more. All this fanciness is available to you if you know the right keystrokes - and there are a gazillion keystrokes available. Because of this, sometimes you inadvertantly type something and find yourself in a strange mode of emacs where nothing looks familiar and emacs isn't responding as expected. In these cases, typing control-g can usually get you back to normal.

Moving Around

To move around in emacs you can use the arrow keys. If you want to move more efficiently, you have to learn more keystrokes.

command description
control-v page down
Esc-v page up
Esc shift < begining of document
Esc shift > end of document

Common editing commands

As you edit files, these commands come in handy. Also these tend to work on the UNIX command line, and the first two in R.

command description
control-a begining of line
control-e end of line
control-k delete line
control-y paste line
Esc-f forward one word
Esc-b back one word
Esc-d delete forward one word

Helpful extras

Find/Search for a pattern

  • control-s search forward
  • control-r search backward

Meta-X commands (aka Esc-X)

Typing Esc-x puts the cursor at the bottom of the emacs screen and allows us to give commands to emacs. Two useful one are:

  • Esc-x goto-line go to a paricular line number
  • Exc-x replace-string search and replace

These respond to auto-complete. Thus after you type Esc-x you can type just the first few letters of your command, and if you hit the Tab key the system will try to finish your typing.

Working with text selections

A selection of text in emacs is called a block. To start a selection you move the cursor to where you want to start the block and "mark" the begining using control-space. You can then move the cursor, or move up or down in your emacs buffer however you like to extend the selection. If you then type Esc-w the block will be copied to a buffer that you can paste from later using control-y.

Keystroke Result
control-space set mark
control-w cut/delete block
Esc-w copy block
control-y paste block

other commands

You can also use emacs to "edit" directories. That is, you can use emacs as a file manager to view and delete files. If you call emacs on a directory instead of a file, you can "edit" the directory, i.e. view, edit, and delete files.

command description
v view file
q quit viewer
e edit file
d mark file for deletion
u unmark file
x execute deletion

Workflow

Since working in Unix often involves working on a remote server, you might often find yourself with only a single window open to that server. If you want to do two things at once, you can open another window, but it is also possible to do two things at once in a single window. UNIX has the concept of foreground and background. If you have an emacs window open, and you want to put it aside for a second without quiting the program, you can type control-z to "put it in the bakground" and your emacs window will go away, giving you your command line back. When you are ready to use emacs again, type fg to bring it back into the foreground, and pick up where you left off.

Keystroke Result
control-z suspend process, put window in background
fg foreground process, resume work

Emacs Macros

Something I love about emacs is it's ability to execute macros. When you have to manipulate text in some odd repetitive way, you can simply define a macro to do it. Whatever operations you perform can be repeated in a macro.

To start a macro you specify when it starts and when it ends, and any sequence of events you type in between can be exectued over and over again. Macros have the basic structure:

control-x (  # begin macro

type stuff   # whatever you type here is performed by executing the macro

control-x )  # end macro

To execute the macro:

control-x e executes the macro

To execute the macro n times: Esc-n control-x e

where n = the number of times you want it executed.


Back to Introduction

Author: Chris Seidel Chris Seidel

Email: cws@stowers.org

Created: 2020-10-06 Tue 00:41

Emacs 26.1 (Org mode 9.1.1)

Validate