emacs#

I’ll use the emacs editor for most of the class. You are welcome to continue to use nano if you prefer.

The best way to learn an editor is by using it. So we’ll go over some key commands in emacs and I’ll introduce more as the semester goes on.

Tip

A quick reference card summarizing the basic commands is available here: emacs reference card

Quick tour#

Tip

The official emacs website has a helpful guided tour to introduce emacs.

Most commands in emacs start use either the Ctrl key or something that emacs calls the Meta key, and a single character. E.g. Ctrl-a will go to the start of the line.

Note

For the “Meta” key, you have two options:

  • Hold down Alt and the other letter in the command.

  • Press (and release) ESC and then press the other letter in the command.

I’ll use M-x to indicate when the meta key is used (in this case, in combination with x).

Let’s open emacs while we try these commands:

emacs -nw

Working with files#

  • Ctrl-x Ctrl-f : read a file into emacs

  • Ctrl-x Ctrl-s : save a file to the filesystem

Moving around the file#

To move the cursor, you can just use the arrow keys. Other options include:

  • Ctrl-a : go to the beginning of the line

  • Ctrl-e : go to the end of the line

  • Ctrl-v : page down

  • M-v : page up

  • M-< : go to the beginning of the file

  • M-> : go to the end of the file

Tip

Since Ctrl-v is mapped as “page down”, to paste something into your emacs buffer that you copied from a different application (like the web browser), use Ctrl-Shift-v.

Abort / undo#

  • Ctrl-g : stop the current command (partially typed or running)

  • Ctrl-x u : undo

  • Ctrl-l : redraw the screen

Searching#

  • Ctrl-s : search forward

  • Ctrl-r : search backward

  • M-% : query-replace text

Marking#

In emacs, for a lot of commands, we define a region by first marking the start and then going to the end and executing the command.

  • Ctrl-Space : set a mark

Killing / deleting#

In emacs, killing is used to mean cut. The removed text is put into a buffer where it can be yanked back.

  • Ctrl-k : kill from cursor to the end of the line

  • Ctrl-w : kill the region (from the mark to the cursor position)

  • Ctrl-y : yank back killed text

Formatting#

  • TAB : auto-indent the line (really nice for source code)

  • M-q : word wrap a paragraph

Exiting#

  • Ctrl-x Ctrl-c : exit emacs completely