NOTES ON EMACS


1 SHORTCUTS

1.1 Notation and Definition

C-
Control key followed by one or several keys.
M-
Alt key followed by one or several keys (on Macs, it is the ESC key instead).
S-
Shift key followed by or several keys.
DEL
Backspace key.
RET
Return or Enter key.
SPC
→ space key.
ESC
Escape key.
TAB
Tab key.
Frame
→ main window of the application.
Window
→ sub-window within the frame.
Buffer
→ window at the bottom of the frame to enter commands and select files.

1.2 General Commands

emacs -nw <file>
→ launch emacs in terminal mode (no frame).
C-u <number> <shortcut>
→ repeat <shortcut> <number> times.
M-x <extended-command>
→ execute <extended-command>.
C-g
→ cancel the current command.

1.2.1 On-line help

C-h t
→ launch the Emacs tutorial.
C-h k <key>
→ explicit the shortcut <key>.
C-h f <func>
→ explicit the function <func>.
C-h w <func>
→ give the shortcut corresponding to function <func>.
C-h m
→ help about the current text mode.

1.3 Managing Files

1.3.1 Opening and closing

C-x C-f
→ open a new file.
C-x C-s
→ save the file.
C-x C-w
→ save the file under a different name.
C-x C-b
→ open a new buffer (useful to open files closed during the current session).
C-x C-c
→ close all windows and quit Emacs.

1.3.2 Splitting windows

C-x 5 2
→ open a new frame.
C-x 5 0
→ close the frame.
C-x 2
→ open a new window below.
C-x 3
→ open a new window on the left.
C-x 1
→ close all other window except the current one.
C-x o
→ move the cursor to the other windows.
C-x left
→ open the previous file in the buffer.
C-x right
→ open the next file in the buffer.

1.3.3 Appearance

M-F8
→ resize the frame (this is a Ubuntu shortcut).
C-x }
→ move to the right the vertical bar splitting a frame (opened with C-x 3).
C-x {
→ move to the left the vertical bar splitting a frame (opened with C-x 3).
C-u 1 C-x 3
→ split the frame horizontally in two windows, with the right one 1.618 times wider. This is not a standard emacs command, it is implemented in my .emacs.

1.3.4 Bookmarks to manage files

C-x r m <name>
→ save the current file in a bookmark.
C-x r b <name>
→ open a bookmark.
C-x r l
→ list the saved bookmarks:
d
→ mark the file to be erased;
x
→ erase all the marked files;
r
→ rename the bookmark;
s
→ save the bookmark file.

1.4 Text Editing

C-/
→ undo the previous command.
C-u <number> <character>
→ insert <character> <number> times.
M-x visual-line-mode
→ toggle to cyclic text mode (without linebreak symbols; soft wrapping).
M-x toggle-truncate-lines
→ same as above, but for hard-wrapping.
M-;
→ comment/uncomment the selection.
C-M-SPC
→ if the cursor is on a left parenthesis, this command selects the whole text until the corresponding right parenthesis (there can be other parentheses within the text).

1.4.1 Moving

C-v
→ go to the next page.
M-v
→ go to the previous page.
M-f
→ go to the next word.
M-b
→ go to the previous word.
C-a
→ go to the beginning of the line.
C-e
→ go to the end of the line.
M-<
→ go to the beginning of the document.
M->
→ go to the end of the document.
C-f
→ go to the next character.
C-b
→ go to the previous character.
C-n
→ go to the next line.
C-p
→ go to the previous line.
C-l
→ center the window on the cursor.

1.4.2 Case

C-x C-u
→ convert the selected text to upper case.
C-x C-l
→ convert the selected text to lower case.

1.4.3 Order by alphabetical order

M-x sort-lines
→ sort the selected lines by alphabetical order.
M-x reverse-region
→ invert the order of the lines.

1.5 Text Searching

1.5.1 Regular expressions

.
→ any character except \n.
\.
→ the character ..
+
→ search the expression once or more.
*
→ search the expression 0 times or more.
?
→ search the expression 0 or 1 times.
^...
→ beginning of a line.
...$
→ end of a line.
\b
→ borders of a word.
[0-9]+
→ one or several digits.
[^0-9]+
→ one or several non-digits.
[A-za-z]+
→ one or several letters.
[;A-Z-z0-9]+
→ one or several characters of the ensemble: letter, digite or ;.

1.5.2 Simple search

C-s
→ forward search.
C-r
→ backward search.
M-g g <num>
→ go to line <num>.

1.5.3 Search and replace

M-%
→ replace a sequence of characters:
  • y to remplace the current sequence and go to the next one;
  • n to go to the next sequence without replacing the current one;
  • ! to replace everything at once.

1.5.4 Search by line

M-s o <exp>
→ search all the lines containing <exp>.
M-s h p <word>
→ highlight all occurrences of <word>.
M-s h r <exp>
→ highlight all occurrences of <exp>.
M-s h l
→ highlight all the lines containing an occurrence of <exp>.
M-s h u
→ cancel the highlighting.

1.6 Copying and Pasting

1.6.1 General commands

C-x h
→ select the whole window.
C-SPC
→ initialize text selection (set a marker).
M-w
→ copy the selected text.
C-w
→ copy and cut the selected text.
C-y
→ paste the copied text.
M-y
→ after C-y, this command remplaces the cut text by the previous copy (can iteratively work).
C-k
→ copy and cut the end of a line.
M-DEL
→ copy and cut the word to the left of the cursor.
M-d
→ copy and cut the word to the right of the cursor.
C-x r t
→ cut a rectangle of text between the cursor and the marker.
C-x r y
→ paste the copied rectangle of text.

1.6.2 Rectangular selection

  1. Go to the upper left corner of the text to be selected;
  2. C-SPC;
  3. Go to the lower right corner of the text to be selected;
  4. C-x SPC.
  5. Once the rectangle is selected, you can do C-x r t and enter a string that will appear on each line of the rectangle.

1.6.3 Managing several sequences

Several sequences to be pasted can be save in memory.

  1. Select a text sequence with C-SPC.
  2. C-x r s <id> to copy. The ID <id> of the sequence can be a single character.
  3. At the desired location, C-x r i <id> pastes the sequence.

1.7 Acrobatic editing

1.7.1 Indenting

C-x TAB
→ indent the selected text (you then need to use the arrows to choose the indentation size; 4 characters in Python).
C-u <N> C-x TAB
→ indent the selected text by <N> characters (<N> can be negative).

1.7.2 Add a prefix to each selected line

  1. Select a region with C-SPC.
  2. C-x r t.
  3. Enter a string that will be placed at the beginning.

This command also allows us to add a column of characters in the middle or at the end of a text. The position of the insertion depends on the abscissa of the cursor at the beginning (x1) and at the end of the selection (x2). If x1≠x2, the column (max(x1,x2),|x2-x1|) is suppressed before inserting the new string. The command C-x r k allows us to remove the column (max(x1,x2),|x2-x1|).

1.7.3 Blanks

M-x delete-trailing-whitespace
→ erase all the blanks at the end of a line.

1.8 Analyzing

1.8.1 Spelling checks

M-x ispell
→ launch the check.
M-x ispell-change-dictionnary
→ change the dictionnary (TAB to print the list).
M-$
→ verify the spelling of the word at the cursor position.
M-x flyspell-mode
→ launch the spell check on the fly.

1.8.2 Counting

M-=
→ prints the number of lines, words and characters of a selection of of the whole buffer.

1.9 Other

C-x C-+
zoom in.
C-x C--
zoom out.
M-x tetris
→ open a new tetris.

2 ORG MODE

2.1 Structure

Sections start with one or several *. The higher the number of stars, the lower the level of the section is (section, subsection, etc.).

TAB
→ show/hide the current section.
S-TAB
→ show/hide all sections.
C-c C-n
→ go to the next section.
C-c C-p
→ go to the previous section.
C-c C-f
→ go to the next section of the same level.
C-c C-b
→ go to the previous section of the same level.
M-up
→ move the current section upward.
M-down
→ move the current section downward.
M-right
→ decrease the level of the current section.
M-left
→ increase the level of the current section.

2.2 Lists

2.2.1 Normal lists

Bullets lists
SPC followed by one of the characters: -, +, *.
Numbered lists
SPC followed by 1. or 1).
Description lists
→ bullet lists with a description followed by ::.
M-RET
→ create the next element in the list.
S-left or S-right
→ change the bullet or number style.

2.2.2 Checkmark lists [2/4]

[X] Defining a box
→ after the bullet, type [ ].
[X] Defining a counter
→ type [0/0] at the first line above the list to count the number of checkmarks.
[ ] C-c C-c
→ check/uncheck the box at the current line.
[-] C-u C-u C-c C-c
→ intermediate state of the box.

2.3 Tables

Cells are separated by |.

2.3.1 Editing tables

C-c -
→ create a line of ----.
TAB
→ create the next line or move to the next cell.
C-c |
→ create a new empty table (prompt for dimensions) or convert text to a table.
C-c C-c
→ align the table.
M-S-left
→ erase the current column.
M-S-right
→ insert a new column to the left.
M-S-up
→ erase the current row.
M-S-down
→ insert a new row above..

2.3.2 Exporting tables

M-x org-table-export
→ start exportation to a file (prompt).
orgtbl-to-csv
→ save to a CSV file.
orgtbl-to-latex
→ save as a LaTeX table.
orgtbl-to-tsv
→ save to a TSV file.
orgtbl-to-html
→ save to HTML.

2.3.3 Formulae

C-c +
→ give the sum of the numbers in the current column.

2.4 Hyperlinks

Format
[ [link][description] ] or simply [ [link] ] without the spaces.
C-c C-l
→ edit the link.
Internal links
[ [Hyperlinks][pipo] ] is a link to the pipo section.
External links
[ [http: or file: or mailto:][description] ].

2.5 To do lists

Setting
→ any section starting with TODO is a TODO list.
C-c C-t or S-left/S-right
→ toggle the status (TODODONE).
Customizing
→ more status can be defined in = ~/.emacs= (e.g. WAITING, CANCELLED, etc.).
Task counter
→ adding [/] or [%] at the level above the tasks counts how many of them have been completed.
Tip
→ combine TODO lists and checkmark lists.

2.6 Properties and tags

C-c C-c
→ put a tag at the end of the line.

2.7 Rich text

2.7.1 Fonts

Fonts can be modified by putting two similar symbols on each side of the text:

*bold*
bold;
/italic/
italic;
_underline_
underline;
+barred+
barred;
=verbatim=
verbatim;
~overline~
overline (my alias).

2.7.2 Indices et exponents

a^1
→ a1;
b_2
→ b2.

2.7.3 Special symbols

  • Greek letters and math symbols can be typed with LaTeX commands: α, β, ∫, etc.
  • Other useful symbols are given in Table 1.
Table 1: LaTeX symbols (remove the + sign).
LaTeX ORG
\ + checkmark
\ + pound £
\ + cent ¢
\ + yen ¥
\ + euro
\ + S §
\ + copy ©
\ + laquo «
\ + raquo »
\ + pm ±
\ + odot o
\ + times ×
\ + to
\ + gets
\ + uarr
\ + darr
\ + harr
\ + lArr
\ + rArr
\ + hArr
\ + gg
\ + ll
\ + ge
\ + le
\ + radic
\ + prop
\ + infty
\ + approx
\ + ne

2.7.4 Equations

A LaTeX equation will be exported (to PDF or HTML) as a compiled equation:

\begin{equation} F = \int f(x)\,dx. \end{equation}

2.7.5 Horizontal lines

Horizontal lines can be traced with 5 consecutive dashes:


2.8 Exporting

Export options can be given, starting with #+:

C-c C-e t a
→ export to an ASCII file;
C-c C-e t u
→ export to a UTF-8 file;
C-c C-e h h
→ export to an HTML file;
C-c C-e l p
→ export to a LaTeX file and compiles it to PDF;
C-c C-e m m
→ export to a Markdown file.

2.9 Code Evaluation and Tangling

2.9.1 Evaluating a code snippet

It is possible to execute source code blocks within org mode.

  1. Write a piece of code between #+BEGIN_SRC <language> and #+END_SRC.
  2. C-c C-c ⇒ execute the code and write the results in a #+RESULTS: field.

The most useful language environments are: emacs-lisp, shell, awk, python, F90.

2.9.2 Tangling

If one or several source blocks are in an org file, tangling consists in writing a file with this code. C-c C-v t does the job. The


3 AGENDA AND ORG MODE

3.1 Date Formats

<YYYY-MM-DD>
→ standard format.
<YYYY-MM-DD day>
→ format with the day of the week.
<YYYY-MM-DD day HH:MM>
→ format with the time.
<YYYY-MM-DD +Nd ou +Nw ou +Nm ou +Ny>
→ program a task every N days, N weeks, N months or N years.
<YYYY-MM-DD>–<YYYY-MM-DD>
→ task during several days.
[YYYY-MM-DD]
→ inactive date (will not have an agenda entry).

3.2 Inserting Formatted Dates

C-c .
→ open the calendar to insert a date.
C-c !
→ open the calendar to insert an inactive date.
C-u C-c .
→ open the calendar to insert a date with time.
C-u C-c !
→ open the calendar to insert an inactive date with time.
Shortcuts
→ from the calendar prompt:
18-10-22 RET
<lun. 22 oct. 2018>;
23 RET
<mar. 23 oct. 2018>;
fri RET
<ven. 26 oct. 2018>;
nov 12 RET
<lun. 12 nov. 2018>;
15:10 RET
<lun. 12 nov. 2018, 15:10>;
+4d RET
<ven. 26 oct. 2018>;
-2wed RET
<mer. 10 oct. 2018>.
S-arrowss RET
→ move throug the open calendar and select the date on which RET is typed.
C-c C-c
→ normalize the date on which the cursor is.

3.3 Programming Tasks

Simple date
→ gives the date an event happens.
C-c C-s
→ open the calendar to select the scheduled beginning of the task (SCHEDULED).
C-c C-d
→ open the calendar to select the deadline of the task (DEADLINE).
-Nd
→ if added to the date, the task will be announced N days before.
+Nw
→ if added to the date, the task will be repeated every N weeks.
C-c / d
→ print the upcoming deadlines of a ORG file.

3.4 ORG Agenda

C-c a t
→ open the list of tasks to do defined in the ORG file (agenda-org-files variable in ~/.emacs):
t
→ toggle the status of the task the cursor is on;
RET
→ open the file where the task is defined, in the same frame;
SPC
→ point to the task in the file where it is defined, in another window.
C-c a a
→ open the ORG agenda:
l
→ display the task journal.
g
→ update an open agenda.

4 AUCTEX

4.1 Preview Mode

C-c C-p C-s
→ enter the Preview mode.
C-c C-p C-c C-b
→ exit the Preview mode.

4.2 Table of Contents

C-c =
→ open a buffer on the top of the frame containing the table of contents of the LaTeX file.
up/down
→ navigate within this buffer.
SPC
→ go to the selected section without closing the buffer.
RET
→ go to the selected section and close the buffer.

4.3 Compiling

C-c C-c l TAB RET
→ compile the LaTeX file.
C-c C-c b TAB t TAB RET
→ run BibTeX.
C-c C-c v TAB RET
→ print the PDF in a window.
Zoom on the PDF
→ using the characters + and -.
C-c C-g
→ show the line in the PDF corresponding to the cursor position in the LaTeX file.
C-click-left on the PDF
→ send the cursor to the corresponding position in the LaTeX file..

5 TOOLS

5.1 Calendar

Open:
M-x calendar.
Slow browsing:
→ arrows.
Fast browsing:
< and >.
Quit:
q.

5.2 Calculator

Open:
M-x calc.
Syntax:
<number1> RET <number2> <operator>.
Quit:
q.

5.3 Hexadecimal Colors

Open:
M-x list-colors-display.

5.4 Annotate PDF

  1. Open a PDF with Emacs
  2. Highlight a part with the mouse
  3. C-c C-a: choose highlighting options. In particular:
    C-c C-a t:
    adds a text comment
    C-c C-a h:
    highlight the text

5.5 BibTeX manager: helm-bibtex

C-x b:
open the BibTeX database
Type regexp:
narrow down the search
Arrows:
to navigate and select the library
RET:
open the PDF if it is there

6 CUSTOMIZATION

6.1 General

6.1.2 Custmoization file

~/.emacs.

6.1.3 Loading the customization file after modifications

  1. M-x load-file;
  2. ~/.emacs.

6.1.4 Installing packages

Use ELPA and MELPA :

M-x list-packages
→ lists the packages that can be installed. Then, click on the name of the package and on the install button.
Installing a <package>.el
→ by hand:
  1. download <package>.el and put it in ~/.emacs.d/lisp/;
  2. in Emacs, type M-x byte-compile-file RET, and select ~/.emacs.d/lisp/<package>.el; this creates ~/.emacs.d/lisp/<package>.elc;
  3. add in ~/.emacs the instructions: (require '<package>) and (<package>-initialize).
M-x list-colors-display
→ show the available colors.

6.2 AucTeX/SyncTeX/PDF-tools

Required packages
auctex, set-alist, tablist, pdf-tools.
PDF tools
→ follow the instructions at pdf-tools.

6.3 Template

Documentation
http://lilypond.org/vc/texi2html.git/test/misc/res/templates/templates.html.
Package
http://emacs-template.sourceforge.net/details.html.
Templates
→ one per extension, in the directory ~/.templates.

6.4 Personnal Shortcuts

6.4.1 Electric Delimiters

C-M-(
→ create a pair (). If a text is selected this pair encompasses it.
C-M-{
→ same for {}.
C-M-[
→ same for [].
C-M-'
→ same for ''.
C-M-"
→ same for "".

6.4.2 Sophisticated selection

M-p s
→ select the text between two delimiters.
M-p l
→ select the current line.
M-p b
→ select the current block.

6.4.3 Format

M-p d
→ insert the date in a file.

6.4.4 Neotree

F7
→ open neotree.

6.4.5 On-line help

F1
→ open list of useful shortcuts.

6.4.6 Window

M-p f
M-x follow-mode when a frame is split into two windows, the rest of the file is displayed in the second window.
F8
→ undo the window configuration.
C-F8
→ redo a window configuration.

Footnotes:

1

the footnote is here.


Author: F. Galliano
Last update: 30 janv. 2024