Logo of Science Foundation Ireland  Logo of the Higher Education Authority, Ireland7 CapacitiesGPGPU Research Projects
Ireland's High-Performance Computing Centre | ICHEC
Home | News | Infrastructure | Outreach | Services | Research | Support | Education & Training | Consultancy | About Us | Login

Emacs tutorial

What is Emacs?

Emacs is a text editing program. The name stands for Editor macros (a macro is a miniature program which allows a complex function to be built up out of small, simple steps: like making a single keystroke do a lot of work). It is worth spending a little time learning how to use Emacs: because it is so common, so portable, and so powerful. Because of Emacs' wide portability, it has become conventional to use keyboard control characters for many functions, as this avoids you having to move your hand away from the keyboard to use the mouse for common functions, and then back again. In these examples, the notation ^x means "Control-X", which you get by holding fown the "Ctrl" or "Control" key and pressing the named letter key once, then releasing both.

Visiting Emacs

Type emacs at the prompt. To open a file (Emacs uses the term "visit"), type ^x^f followed by the filename. It displays the current directory name as a prefix, but you can backspace over this if you want to load a file from another directory. If the file exists, it is displayed. If it does not, it is created.

Emacs lets you use a trick called "completion" on all filenames or commands you type: if you type the first few characters and then press the TAB key, Emacs will complete it for you if what you have typed is unique. If not, it will let you press TAB again, and display a list of all valid completions for you to pick one of.

Leaving Emacs

To exit from Emacs, press ^x^c. If you have not saved your file(s), Emacs will ask if you want to save them: answer with y for yes or n for no to each one, as prompted.

Emacs automatically makes a backup (safety) copy of your file every 300 keystrokes (this frequency can be changed). To save your file explicitly (a good idea from time to time), type ^x^s.

Moving around

Arrow keys will work if your terminal is set correctly, otherwise:

  • Next line (down): ^n
  • Previous line (up): ^p
  • Forward (right 1 character): ^f
  • Backward (left 1 character): ^b
  • New line: RETURN
  • Forward one word: ESC f
  • Backward one word: ESC b
  • Next screenful (down a page): ^v
  • Previous screenful (up a page): ESC v
  • Jump to start of line: ^a
  • Jump to end of line: ^e

Deleting

  • Delete forwards: ^d (deletes character following point)
  • Delete backwards: Del or Backsp (deletes character before point)
  • Delete to end of line: ^k (deletes text to the end of line: to remove the line entirely, press ^k twice. If the line was blank, once is enough.)
  • Delete word forwards: ESC d
  • Delete word backwards: ESC del
  • Undelete last deletion: ^y ("yank")

Searching/Replacing

  • Incremental search forwards: ^s word
  • Incremental search backwards: ^r word
  • Repeat the search ("find next"): ^s or ^r (as appropriate)
  • Stop incremental search: ESC or RETURN

With an incremental search, as you type each character of the word, Emacs immediately starts searching, so you may find what you are looking for before completing the word. Press RETURN to stop searching, or ^g to abort the search and return to where you started.

To do a non-incremental search, type ^s or ^r followed by RETURN and then the word or characters you are looking for.

To do global search-and-replace, type ESC x replace-string RETURN then type the text to search for (and press RETURN) then the text to replace it with (and press RETURN). If you are going to do a lot of these, you can assign the command to a keystroke to make it faster to use. There are other more complex pattern-matching searches (using Unix-like "regular expressions") that you can learn later.

Don't Panic

If something goes wrong don't worry:

  • you can press ^g to cancel whatever it's doing and go back to editing;
  • you can undo the last thing you did by typing ESC x undo RETURN
  • If the screen gets messed up with modem noise or other interruptions, pressing ^L will refresh it.
  • For brief help, press ^h at any time. You can get more specific, context-sensitive help by typing ESC x describe-mode RETURN, and the entire manual is online as well (type ESC x info RETURN).
  • In an emergency, you can abort Emacs pressing ^z, but this will not save any files.

Further Reading