This reference-like tutorial is aimed at Emacs beginners and probably others too. If you know how to do some basic editing and navigation in Emacs and want to quickly go through more of its capabilities, then this tutorial is for you. If you are looking for the semantics or key-bindings of a specific feature, this tutorial aims to be for you since it is a subset of the online Emacs documentation. If you are giving an introductory presentation about Emacs, this tutorial might help you.
- 1 Description and History
- 2 Technical Description
- 3 Why Emacs?
- 4 Terminology and Structure
- 5 Modeline
- 6 Keybind Terms and Minibuffer
- 7 Basic Usage
- 8 Windows
- 9 Navigation and Movement
- 10 Editing
- 11 Prefix Arguments and Command Repetition
- 12 Search and Replace
- 13 Undo and Redo
- 14 Regions
- 15 Rectangles
- 16 Registers
- 17 Bookmarks
- 18 Macros
- 19 Alignment
- 20 Sorting
- 21 Tables
- 22 Goodies
- 23 Setting Key Bindings
- 24 Storing Key Bindings
- 25 The Emacs Help System
- 26 Modes
- 27 Mode: ediff
- 28 Mode: Spell Checking
- 29 References
Description and History
- Emacs is an extensible, customizable, self-documenting, real-time display text editor.
- Originally, a set of editing macros for the TECO editor.
- GNU Emacs was developed by R. Stallman and G. Steele.
- Technically, GNU Emacs is an elisp interpreter with text editing extensions.
- GNU Emacs contains thousands of commands and allows the user to combine them into elisp procedures (called macros) to automate work.
- Commands are, themselves, elisp procedures and the GNU Emacs configuration init file is an elisp program.
- The user can define, un-define and redefine commands as well as re-use available commands into macros.
- The user can change properties of emacs elements including the key-bindings and the display.
- All defined commands and macros get automatic preliminary documentation (bound keys and parameters).
- Easily programmable
- Kill ring
- Rectangular editing
- Command repetition
- Macros and Lisp procedures
- Init file
- Remote files
- Shell, Terminal Emulator, IRC, Email, News, File Manager, Process Manager, Project Manager, Tetris, Doctor and many others.
Terminology and Structure
- A container for data (text, completions)
- The current position in the buffer (cursor)
- A visual container for a buffer
- A visual container for one or more windows
- The equivalent of cutting
- The equivalent of copying
- Kill Ring
- A circular clipboard (very handy)
- The coordinates of a selection
- The text inside a mark
- A status line
- A small buffer for commands and arguments
- The current type of data being edited
The Modeline is used to report different pieces of information.
-cs:ch-fr buf pos line (major minor)-----
- Character set (or coding system)
- Newline mode
- File modification status
- - or @
- File is local or remote
- Frame name on text terminals
- Buffer/file name
- Point/cursor position
- Current line and column number
- modes (major minor)
- Currently loaded major and minor modes
Keybind Terms and Minibuffer
Emacs has some different naming of command/control keys (probably coming from the old days).
- Meta key, Alt or ESC
- Control key
- Shift key
- Press a while holding the meta key
- Press a while holding the control and meta keys
- Press a while holding the control key then press b
- Press a and b both while holding the control key
- Press a with control then b with meta
The Minibuffer is used to input commands, arguments and setting different modes (which some of are bound to keys or menu actions).
- Execute a command by name
$ emacs # opens Emacs at the scratch buffer
$ emacs [file1] [file2] ... # opens Emacs in split-window mode
- loads a new buffer from filename
- saves the current buffer
- saves the current buffer as filename
- quits emacs
- cancels key-sequence
Windows are used to display buffer contents. Windows can be split and resized inside an Emacs frame. The minibuffer has its own window.
- open a window vertically
- open a window horizontally
- close the current window
- close all other windows
- ESC ESC ESC
- close all other windows
- move to another window
- increase window size vertically
- increase window size horizontally
- decrease window size horizontally
- shrink window to buffer size
- make all windows equal in size
- moves one word backwards
- moves one word forward
- goes to beginning of the line (Home)
- goes to end of the line (End)
- goes to beginning of the sentence
- goes to end of the sentence
- goes to given line by number
- moves point to the first non-whitespace character in this line
- moves up one paragraph
- moves down one paragraph
- moves to the top of window
- moves to the middle of window
- moves to the bottom of window
- moves to the beginning of the buffer
- moves to the end of the buffer
- moves the buffer to position point
- switches to another buffer by name
- switches to previous or next buffer
- kills the current buffer
- deletes next character
- transposes current character with previous one
- inserts a TAB verbatim
- kills the next word
- kills to the previous word
- expands current word
- expands current word and grabs next one
- completes current word
- transposes current word with next one
- transposes current word with previous one
- capitalizes next word
- capitalizes previous word
- lowercase next word
- lowercase previous word
- uppercase next word
- uppercase previous word
- center a line
- kills current line
- kills from point to EOL
- kills to the previous line
- goes to BOL then kills to EOL
- indents line depending on current mode
- breaks line before/after point
- transpose current line with previous one
- transpose current line with one at mark
- delete lines matching a regexp
Prefix Arguments and Command Repetition
Passing numerical arguments to commands can alter their behavior (e.g., repetition or inversion). Passing an argument can be done before M-x or key bindings.
- run com with prefix arg n
- run com with negative prefix arg
- run com with prefix arg n
- run com with prefix arg 4 (the default)
- run com with prefix arg 16 (four times the four times the default)
- run com with prefix arg 64 (four times the four times the four times the default)
- also terminates the prefix argument
- repeats the previous zth command (including its args)
- repeats last command that used the minibuffer
- will kill 50 lines
- will insert 5 zeros
Search and Replace
- search forward
- search backward
- search forward
- search backward
Regular Expression Search
- search regexp forward
- search regexp backward
- ESC will cancel and go to original point. RET will end at the current point.
- Searches are case-insensitive unless an uppercase letter is found in the search string.
- While searching, will increment the search term with the current word.
- replaces a string with another
- replaces a regexp match with a string
Query Replace (Conditional Replace)
- conditional replacement of a string
- conditional replacement of a regular expression
- Replaces are case-insensitive unless an uppercase letter is found in the match string.
- Replaces work from position to end of buffer unless a mark is active.
- After a replace, the position will be at the end of the last match. to go back to the position before the replace started.
- During a query replace, SPC will apply a replace and DEL will skip.
Undo and Redo
In Emacs, there is no special redo function. Instead, there is only an undo function and redo can be achieved by undo-ing an undo.
- undo a single change
- break the chain of undos
- If a mark is active, undo will only affect the marked region.
Emacs uses the mark and point to denote a region (i.e., a selection).
- creates a region (marks area)
- swaps mark and point in a region
- swaps mark and point without region
- marks the entire buffer
- marks the paragraph around the point
- indents the current region
- incrementally mark next word
Kill, Yank and Paste Regions
- kills the currently selected region
- copies the currently selected region
- ( )*
- yanks and cycles from the kill ring
- Yank is the copy command in vi and the paste command in emacs.
Rectangles are marked regions between the columns of the point and the mark. Applying operations on rectangles is some sort of vertical editing.
- kill the rectangle
- delete the rectangle
- yank the last killed rectangle
- push text to fill rectangle with spaces (open)
- replace rectangle text with spaces (clear)
- replace each line in rectangle with text
Registers are places where you can store anything: text, position, rectangle, configuration, filename, etc... Registers are named with single character symbols: a, A and 3 are three different registers.
- view the contents of a register
- record current point position in register r
- jump to position in register r
- save region to register r
- kill region to register r
- insert text from register r
- append region to register
- prepend region to register
- save rectangle into register r
Bookmarks are like registers but persistent. They can also be named.
- bookmark file and point to foo
- jump to bookmark called foo
- list all bookmarks
- save all bookmarks
- delete a bookmark
Macros are user defined commands using the Emacs command language. A user can record and replay a macro, useful for simple repeatable tasks. More complex tasks (e.g., conditions, loops) must be implemented in elisp. Macros are recorded in a macro ring.
- start macro definition or insert counter
- start macro definition only
- end macro definition or call macro
- end macro definition and call macro
- end macro definition only
- append commands to last macro
- re-run last macro then append commands to it
- run macro on region
- rotate to select the next macro in the ring
- rotate to select the previous macro in the ring
Every macro definition can have a counter to insert into the buffer.
- inside a macro definition, inserts the counter
- outside a macro definition, inserts the counter
- set the macro counter value
- set the macro counter format
Macros can be named and saved.
- name the most recently defined macro
- keybind the most recently defined macro
- insert macro into buffer as elisp code
- It is best to use the reserved key bindings as to not cause problems with other bindings. will define as a binding.
Emacs contains alignment commands. Sometimes those depend on the current mode (i.e., the language).
- aligns depending on mode
- aligns using a regular expression
- aligns using a regular expression helper
- aligns current paragraph depending on mode
When sorting, prefixing withsorts in descending order.
- sort region by lines
- sort region by paragraphs
- sort lines in region by nth field
- sort lines in region by nth field from right
- interpret field as number and not text
- sort by column specified by marked region
- reverses current region
Emacs has support for creating and editing text-based tables by keeping track of their properties (e.g., position, size) in the buffer. When a buffer is saved to file, those properties are lost.
- interactively insert a table into buffer
- detect properties of all tables in the buffer
- remove special table properties
- detect properties of tables in region
- forget properties of tables in region
- detect properties of table at point
- remove properties of table at point
- widen cell at point by n characters
- narrow cell at point by n characters
- heighten cell at point by n lines
- shorten cell at point by n lines
- move to cell on right
- move to cell on left
Cell merging and splitting
- interactively merge two cells
- split cell horizontally
- split cell vertically
Rows and Columns
- insert n rows
- delete n rows
- insert n columns
- delete n columns
- interactively justify cell, column or row text
- toggle table fixed width mode
- table code in Latex, HTML or Cals
- force an indentation on a region
- indents and breaks paragraph into multiple lines
- comments a region or adds comment to line
- inserts a new lisp function (parenthesis)
- checks balanced parenthesis and opens line
- inserts file contents at point position
- run shell command
- run shell command and insert output
- run shell command on marked region
- run shell command on and replace marked region
- inserts buffer contents at point position
- copy region content to a buffer
- interactively kill buffers
- interactive buffer list
- do not split lines over visual lines
- split lines by words
- vi compatibility mode
Setting Key Bindings
Emacs has a global keymap which maps between keys and commands. Each major mode can define, re-define or un-define its own key bindings, creating a local keymap (e.g., c-mode). Each minor mode can do so too (e.g., flymake). Each portion of text can also do so (e.g., tables).
Some prefix keys
- command prefix key
- command prefix key
- mode specific prefix key
- help prefix key
Commands to bind keys
- global-set-key k com
- bind key k to com globally
- local-set-key k com
- bind key k to com locally (major mode)
- Menu and mouse key bindings can also be set.
Storing Key Bindings
In the Emacs init file, you can either set global key bindings or hook local key bindings to mode hooks (i.e., callbacks, slots).
- adds a binding to the global map
- adds a binding to the local (major mode) map
- converts a string to a key sequence
Global key binding
(global-set-key (kbd 'C-c d') 'duplicate-line) (global-set-key (kbd 'C-c d') (kbd 'C-a C-@ C-e M-w RET C-y')) (global-set-key (kbd 'C-c d') '\C-a\C- \C-n\M-w\C-y')
Local key binding
(add-hook 'LaTeX-mode-hook (lambda () (local-set-key (kbd 'C-c n') 'forward-paragraph)))
The Emacs Help System
Emacs has a handy help system for quick lookup of various features, topics, functions and key bindings.
- show the help system shortcuts
- run help system with shortcut key
Some useful help keys
- show apropos page about a keyword
- show help for function bound to keybind
- show all key bindings
- show documentation for function
- show documentation for current modes
- show documentation for variable
- show what keys a command is bound to
- start the emacs tutorial
- show all key binds starting with prefix
Modes are used to define the type of data being edited in buffers, or what Emacs calls “the language”.
Every buffer has exactly one major mode which defines its language (C, Java, English, IRC, etc...) and provides basic elements like syntax highlighting and (re)defines functions and their key bindings for relevant actions (e.g., comments).
Each buffer can have zero or more minor modes enabled which provide non-specific functionality such as spell checking or line wrapping (i.e., mode independent).
ediff (emacs-diff) is a mode for Emacs where you can view and merge differences between two or three buffers.
- select files to view their differences
- select buffers to view their differences
- switch between horizontal/vertical view
- view ediff help
- view previous difference
- view next difference
- set buffer b’s area to what’s in a’s
- set buffer a’s area to what’s in b’s
- quit ediff session
Mode: Spell Checking
- check spelling at word or active region
- check active region or buffer, during which pressing i will accept the current word and insert it into the user's dictionary, pressing ? will show help and pressing RET will end the current ispell session.
- complete current word from dictionary
- enable “on the fly” spell checking
- spell check comments and strings (programming mode)
- Emacs Wiki: http://emacswiki.org/
- Emacs Docs: https://www.gnu.org/software/emacs/manual/html_node/emacs/index.html
- Emacs Help