Difference between revisions of "Quick Tutorial"

From WikEmacs
Jump to: navigation, search
Line 382: Line 382:
; {{Keys|C-u ''n'' C-{}} : shorten cell at point by ''n'' lines
; {{Keys|C-u ''n'' C-{}} : shorten cell at point by ''n'' lines
Cell movement:
=== Cell movement ===
* TABmove to cell on right
; {{Keys|TAB}}  : move to cell on right
* S-TABmove to cell on left
; {{Keys|S-TAB}} : move to cell on left
Cell merging/splitting:
=== Cell merging and splitting ===
* {{Keys|C-c C-c}} *interactively merge two cells
; {{Keys|C-c C-c *}} : interactively merge two cells
* C-split cell horizontally
; {{Keys|C-}}        : split cell horizontally
* C--split cell vertically
; {{Keys|C--}}      : split cell vertically
Rows & Columns:
=== Rows and Columns ===
* C-u ''n'' table-insert-rowinsert ''n'' rows
; {{Keys|C-u ''n'' table-insert-row}}    : insert ''n'' rows
* C-u ''n'' table-delete-rowdelete ''n'' rows
; {{Keys|C-u ''n'' table-delete-row}}    : delete ''n'' rows
* C-u ''n'' table-insert-columninsert ''n'' columns
; {{Keys|C-u ''n'' table-insert-column}} : insert ''n'' columns
* C-u ''n'' table-delete-columndelete ''n'' columns
; {{Keys|C-u ''n'' table-delete-column}} : delete ''n'' columns
=== Other ===
* C-:interactively justify cell, column or row text
; {{Keys|C-:}}          : interactively justify cell, column or row text
* C-!toggle table fixed width mode
; {{Keys|C-!}}          : toggle table fixed width mode
* table-generate-sourcetable code in Latex, HTML or Cals
; table-generate-source : table code in Latex, HTML or Cals
== Goodies ==
== Goodies ==

Revision as of 13:45, 19 April 2012

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.

The repository for the contents of this tutorial can be found here: https://github.com/fredmorcos/emacs-reference

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.

Technical Description

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).

Why Emacs?

  • Easily programmable
  • Kill ring
  • Rectangular editing
  • Registers
  • Command repetition
  • Macros and Lisp procedures
  • Modes
  • 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
[C-a b] 
Press a while holding the control key then press b
[C-a C-b] 
Press a and b both while holding the control key
[C-a M-b] 
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

Basic Usage


$ emacs # opens Emacs at the scratch buffer
$ emacs [file1] [file2] ... # opens Emacs in split-window mode
[C-x C-f] filename 
loads a new buffer from filename


[C-x C-s] 
saves the current buffer
[C-x C-w] filename 
saves the current buffer as filename


[C-x C-c] 
quits emacs
[C-g C-g] 
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.

[C-x 2] 
open a window vertically
[C-x 3] 
open a window horizontally
[C-x 0] 
close the current window
[C-x 1] 
close all other windows
close all other windows
[C-x o] 
move to another window
[C-x ^] 
increase window size vertically
[C-x ]
increase window size horizontally
[C-x {] 
decrease window size horizontally
[C-x -] 
shrink window to buffer size
[C-x +] 
make all windows equal in size

Navigation and Movement


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
[M-g g] 
goes to given line by number


moves up one paragraph
moves down one paragraph


[C-x [] 
moves to the beginning of the buffer
[C-x ]] 
moves to the end of the buffer
moves the buffer to position point
[C-x b] 
switches to another buffer by name
switches to previous or next buffer
[C-x k] 
kills the current buffer



deletes next character
transposes current character with previous one
[C-q TAB] 
inserts a TAB verbatim


kills the next word
[M-- M-d]  
kills to the previous word
expands current word
[M-/ SPC M-/] 
expands current word and grabs next one
completes current word
transposes current word with next one
[M-- M-t]  
transposes current word with previous one
capitalizes next word
[M-- M-c]  
capitalizes previous word
lowercase next word
[M-- M-l]  
lowercase previous word
uppercase next word
[M-- M-u]  
uppercase previous word


[M-o M-s] 
center a line
kills current line
kills from point to EOL
[M-u C-k] 
kills to the previous line
[C-a C-k] 
goes to BOL then kills to EOL
indents line depending on current mode
breaks line before/after point
[C-x C-t] 
transpose current line with previous one
[M-0 C-x] C-t 
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.

[M-n com]  
run com with prefix arg n
[M-- com]  
run com with negative prefix arg
[C-u n com] 
run com with prefix arg n
[C-u com]  
run com with prefix arg 4 (the default)
[C-u C-u com]  
run com with prefix arg 16 (four times the four times the default)
[C-u C-u com]  
run com with prefix arg 64 (four times the four times the four times the default)
also terminates the prefix argument
[C-x [z]+]  
repeats the previous zth command (including its args)
[C-x ESC ESC]  
repeats last command that used the minibuffer


[C-u 5 0 C-k] 
will kill 50 lines
[C-u 5 C-u 0] 
will insert 5 zeros

Search and Replace

Incremental search

[C-s keyword] 
search forward
[C-r keyword] 
search backward

Non-incremental search

[C-s RET keyword] 
search forward
[C-r RET keyword] 
search backward

Regular Expression Search

[C-M-s expression] 
search regexp forward
[C-M-r expression] 
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, [C-w] will increment the search term with the current word.

Unconditional Replace

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. [C-u C-SPC] 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)
[C-x C-x]  
swaps mark and point in a region
[C-u C-x C-x] 
swaps mark and point without region
[C-x h]  
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
[C-y] ([M-y])* 
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.

[C-x r k] 
kill the rectangle
[C-x r d] 
delete the rectangle
[C-x r y] 
yank the last killed rectangle
[C-x r o] 
push text to fill rectangle with spaces (open)
[C-x r c] 
replace rectangle text with spaces (clear)
[C-x r t] 
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
[C-x r SPC r]  
record current point position in register r
[C-x r j r]  
jump to position in register r
[C-x r s r]  
save region to register r
[C-u C-x r s r] 
kill region to register r
[C-x r i r]  
insert text from register r
append region to register
prepend region to register
[C-x r r r]  
save rectangle into register r


Bookmarks are like registers but persistent. They can also be named.

[C-x r m foo] 
bookmark file and point to foo
[C-x r b foo] 
jump to bookmark called foo
[C-x r l]  
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
[C-x (]  
start macro definition only
end macro definition or call macro
[C-x e]  
end macro definition and call macro
[C-x )]  
end macro definition only
[C-u C-u F3]  
append commands to last macro
[C-u F3]  
re-run last macro then append commands to it
[C-x C-k r]  
run macro on region
[C-x C-k C-n]  
rotate to select the next macro in the ring
[C-x C-k C-p]  
rotate to select the previous macro in the ring

Macro Counters

Every macro definition can have a counter to insert into the buffer.

inside a macro definition, inserts the counter
[C-x C-k C-i]  
outside a macro definition, inserts the counter
[C-x C-k C-c]  
set the macro counter value
[C-x C-k C-f]  
set the macro counter format

Saving Macros

Macros can be named and saved.

[C-x C-k n] 
name the most recently defined macro
[C-x C-k b] 
keybind the most recently defined macro
insert macro into buffer as elisp code


  • It is best to use the reserved key bindings [C-x C-k [a-zA-Z0-9]] as to not cause problems with other bindings. [C-x C-k b 4] will define [C-x C-k 4] 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
[C-u align-regexp] 
aligns using a regular expression helper
aligns current paragraph depending on mode


When sorting, prefixing with [C-u] sorts in descending order.

sort region by lines
sort region by paragraphs
[C-u n sort-fields]  
sort lines in region by nth field
[C-u -n sort-fields] 
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

Cell resizing

[C-u n C->] 
widen cell at point by n characters
[C-u n C-<] 
narrow cell at point by n characters
[C-u n C-}] 
heighten cell at point by n lines
[C-u n C-{] 
shorten cell at point by n lines

Cell movement

move to cell on right
move to cell on left

Cell merging and splitting

[C-c C-c *] 
interactively merge two cells
split cell horizontally
split cell vertically

Rows and Columns

[C-u n table-insert-row]  
insert n rows
[C-u n table-delete-row]  
delete n rows
[C-u n table-insert-column] 
insert n columns
[C-u n table-delete-column] 
delete n columns


interactively justify cell, column or row text
toggle table fixed width mode
table code in Latex, HTML or Cals


  • C-x TABforce an indentation on a region
  • [M-q] indents and breaks paragraph into multiple lines
  • [M-;] comments a region or adds comment to line
  • [M-(] inserts a new lisp function (parenthesis)
  • [M-)] checks balanced parenthesis and opens line
  • [C-x] iinserts file contents at point position
  • [M-!] run shell command
  • C-u M-!run shell command and insert output
  • M-<math>|</math>run shell command on marked region
  • C-u M-<math>|</math>run shell command on and replace marked region
  • insert-bufferinserts buffer contents at point position
  • copy-to-buffercopy region content to a buffer
  • kill-some-buffersinteractively kill buffers
  • electric-buffer-listinteractive buffer list
  • toggle-truncate-linesdo not split lines over visual lines
  • visual-line-modesplit lines by words
  • viper-modevi compatibility

Setting Key Bindings

Emacs has a global keymap which maps between keys and commands. Each major mode can define, redefine or undefine 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).

Description of some prefix keys:

  • C-xcommand prefix key
  • M-command prefix key
  • C-cmode specific prefix key
  • C-hhelp prefix key

Commands to bind keys:

  • global-set-key k combind key k to com globally
  • local-set-key k combind 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).

  • global-set-keyadds a binding to the global map
  • local-set-keyadds a binding to the local (major mode) map
  • kbdconverts 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")

The Emacs Help System

Emacs has a handy help system for quick lookup of various features, topics, functions and key bindings.

  • [C-h ?] show the help system shortcuts
  • C-h keyrun help system with shortcut key

Some useful help keys:

  • [C-h a] show apropos page about a keyword
  • [C-h k] keybindshow help for function bound to keybind
  • [C-h b] show all key bindings
  • [C-h f] show documentation for function
  • [C-h m] show documentation for current modes
  • [C-h v] show documentation for variable
  • [C-h w] show what keys a command is bound to
  • [C-h t] start the emacs tutorial
  • prefix C-hshow 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

In ediff-mode

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

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)