Using Emacs and Org Mode
- 1Preliminaries(633w~4m)
- 2Setting up Emacs and Org Mode(444w~3m)
- 3Editing text in Emacs(593w~3m)
- 4Navigating the file system using dired(160w~1m)
- 5Editing in Org Mode(399w~2m)
- 6Making Org play nice with Jekyll(459w~3m)
- 7Getting more help(193w~1m)
- 8Programming in Emacs Lisp(408w~3m)
- 9Configuring Emacs further(51w~1m)
- 10Using Git from Emacs(157w~1m)
- 11Next steps(401w~3m)
- 12Obsolete content(62w~1m)
- 13Emacs woes: Sometimes Emacs is a pile of hacks and workarounds.(27w~1m)
- 14Finding out what is slowing down Emacs(11w~1m)
1Preliminaries
- 1.1How long will it take?(78w~1m)
- 1.2Why emacs?(24w~1m)
- 1.3Key notation(70w~1m)
- 1.4Checks before you jump into Org Mode(464w~3m)
1.1How long will it take?
<2018-09-16> I spent at least 3 days to set up emacs, set up org mode, and make org mode play nice with Jekyll. You may finish much faster than that because you can avoid repeating some of my ordeals. You may finish slower because you encounter surprises that I didn't.
If you're new to emacs, but don't want to spend a day configuring emacs, then don't use emacs; use Visual Studio Code or Spacemacs instead.
1.2Why emacs?
Because of org mode. If it weren't for org mode, I would never have touched emacs.
The Emacs guided tour may give some reasons.
1.3Key notation
C-a
means hold Ctrl and press the A key.M-a
means hold Meta (Alt) and press the A key.S-a
means hold Shift and press the A key.RET
means press the Return/Enter key.LEFT
means press the Left Arrow key.BKSP
means the Backspace key. Emacs calls this keyDEL
.SPC
means the Space key.C-a C-b
meansC-a
and thenC-b
.M-x help
means pressM-x
and then typehelp
(and press Enter if necessary).
1.4Checks before you jump into Org Mode
Before you jump, check whether what you want to do is possible in Org Mode. I presume we don't want to spend too much time hacking Org Mode to make it work.
- 1.4.1Can Org Mode do what you want?(189w~1m)
- 1.4.2Can you live with some Org Mode woes?(136w~1m)
- 1.4.3Are you willing to work around some annoyances?(104w~1m)
1.4.1Can Org Mode do what you want?
If you really can't afford these limitations, don't use Org Mode.
<2018-11-07> Can't bold/emphasize/underline/strikethrough part of word
- Org uses regular expression where it should use context-free grammar.
<2018-09-18> Can't put brackets in link text
Can I make links in Org Mode that contain brackets? - Emacs Stack Exchange
Org uses regex where it should use CFG.
<2018-09-15> Can't put code block in table
Code blocks inside tables for org-mode - Stack Overflow
I left Markdown only to find out that Org Mode shares this weakness.
Wanted: a markup that can put code blocks side-by-side in a table.
Do we really need this? The table is going to be too wide for mobiles anyway.
<2018-09-16> Can't change the title of a table of contents
There is no documented way to do that.
<2018-09-28> Can't decide between org-ref and pandoc-citeproc?
This was in 2016. I don't know how it is in 2018.
<2018-10-15> Org's HTML inlining is slightly more verbose than Markdown's.
You can't just type HTML in Org Mode. For example, you can do the following in Markdown, but not Org Mode.
Hello, <span class="what">world</span>.
You have to prefix it with
@@html:
. https://orgmode.org/manual/Quoting-HTML-tags.html<2018-12-26> Can't comma-separate footnote numbers
1.4.2Can you live with some Org Mode woes?
These woes are mostly non-fatal but annoying.
<2018-09-17> Incremental search doesn't search link URLs
<2018-09-16> Can't get too fancy with inline markup because Org uses regex where CFG should be used
This has been worked around in newer org version. I can't reproduce this with org 9.1.14. They are still using regex, but they replaced the pattern with a more useful default.
We can't insert verbatim "a= b". This inserts verbatim "a" followed by text "b=":
=a= b=
<2018-09-16> Link text can't contain brackets
Is this still true in org 9? emacs - How to escape square bracket in org mode links? - Stack Overflow
<2018-09-16> org-ref: "References" heading should be h2, but is h1
1.4.3Are you willing to work around some annoyances?
<2018-09-17> Publishing moves the cursor to the beginning of buffer
In short,
org-publish
clobbers excursion.To publish current file, press
C-c C-e P f
.The workaround is to wrap
org-publish
insave-mark-and-excursion
.<2018-09-16> org-ref: There is an unwanted link to the bib file if there is no cite at all in the source org file
The workaround is to remove the
bibliography:
if the document doesn't have anycite:
.<2018-10-09> Can't change title of table of contents
It's hardcoded in ox-html.el and ox.el.
Magit fails with "index.lock exists" when we press g while it is rebasing.
The workaround is: Don't press g while Magit is rebasing.
2Setting up Emacs and Org Mode
- 2.1Installing Emacs(90w~1m)
- 2.2Launching Emacs(24w~1m)
- 2.3Configuring Emacs and Org Mode(317w~2m)
- 2.4Entering org mode(11w~1m)
2.1Installing Emacs
<2018-09-16> To install Emacs 26 on Ubuntu 14.04, I follow some of How to Build Emacs on Linux, but I don't sudo.
Download Emacs source code package Check its integrity. Extract it to SOMEWHERE
.
Install build dependencies
Build Emacs from source. Close that terminal. Open a new one.
cd SOMEWHERE
./configure --prefix=$HOME/.local # assuming that $HOME doesn't contain spaces
make -j4 install # replace 4 with the number of cores you have
We assume you use bash. Ensure that $HOME/.local/bin
is in your PATH
. Ensure that this line is in your ~/.bashrc
file, preferably near the end.
Close the terminal. Open a new one. Entering emacs --version
should print something like GNU Emacs 26.1
followed by license notice.
2.2Launching Emacs
Launch emacs from your terminal in your working directory. Use emacs -fs
to start emacs fullscreen. Use emacs -mm
to start emacs maximized.
2.3Configuring Emacs and Org Mode
<2018-09-17> You can skip this by copying the emacs.d
folder of my work repository. But it may fail; I haven't tested it with a fresh installation. You can also copy other people's emacs config you find on the Internet.
- 2.3.1Ensuring that the configuration folders/files exist(60w~1m)
- 2.3.2Making Emacs remember where you left off(52w~1m)
- 2.3.3Adding MELPA(2w~1m)
- 2.3.4Installing a recent org mode from MELPA(19w~1m)
- 2.3.5Installing org-ref for citing research papers(33w~1m)
- 2.3.6<2018-09-16> Check your Emacs version and Org Mode version(28w~1m)
- 2.3.7Restart Emacs so that your config changes take effect(9w~1m)
- 2.3.8If Emacs is misbehaving(40w~1m)
- 2.3.9Configuring org mode(40w~1m)
2.3.1Ensuring that the configuration folders/files exist
Set up your emacs initialization file ~/.emacs.d/init.el
. It is an Emacs Lisp program.
Make sure that the file ~/.emacs
does not exist; otherwise Emacs won't read ~/.emacs.d/init.el
.
You may want to commit your modifications to your personal Git repository, and make ~/.emacs.d
a symbolic link.
Make sure that you don't accidentally publish sensitive data (such as IRC passwords).
2.3.2Making Emacs remember where you left off
Save a desktop/session so that emacs continues where you left off: M-x desktop-save
. (Do we still need this if we already have (setq desktop-save 1)
in the init file?)
See also "Saving Emacs Sessions" in the Emacs manual. Unfortunately Emacs 24.3 doesn't save the frames and windows.
2.3.3Adding MELPA
2.3.4Installing a recent org mode from MELPA
M-x package-install RET org RET
TODO Use use-package in init file to automate installation
2.3.5Installing org-ref for citing research papers
- org-ref installation
- jkitchin/org-ref: org-mode modules for citations, cross-references, bibliographies in org-mode and useful bibtex tools to go with it.
- Org mode for academic writing: Bibliographies with org-ref - Vivek's Info
2.3.6<2018-09-16> Check your Emacs version and Org Mode version
Check your Emacs version with M-x emacs-version
. Check your Org Mode version with M-x org-version
. This document assumes these versions:
GNU Emacs 26.1 (build 1, x86_64-pc-linux-gnu, GTK+ Version 2.24.23) of 2018-09-10
Org mode version 9.1.14 (9.1.14-1-g4931fc-elpaplus @ /home/erik/.emacs.d/elpa/org-plus-contrib-20180910/)
2.3.7Restart Emacs so that your config changes take effect
2.3.8If Emacs is misbehaving
If Emacs is misbehaving (and you have no idea why), try running emacs -q
. If it runs fine, try nuking the file ~/.emacs
and the directory ~/.emacs.d
. But you will lose all your customizations and installed packages.
2.3.9Configuring org mode
The default isn't always sane. Thus I have these customizations in my Emacs init file.
<2018-09-16> Avoiding reindentation when moving trees around
We do this to prevent unnecessary whitespace changes in version control. https://emacs.stackexchange.com/questions/41220/org-mode-disable-indentation-when-promoting-and-demoting-trees-subtrees
(setq-default org-adapt-indentation nil)
<2018-09-16> Removing day names from inserted dates
(setq org-time-stamp-formats '("<%Y-%m-%d>" . "<%Y-%m-%d %H:%M>"))
2.4Entering org mode
Visit a file with .org
extension. Alternatively, M-x org-mode
.
3Editing text in Emacs
- 3.1Inserting text(11w~1m)
- 3.2Common keys(43w~1m)
- 3.3Discover Emacs, don't memorize it(88w~1m)
- 3.4Using buffers and windows(100w~1m)
- 3.5Autocompletion(70w~1m)
- 3.6Searching(65w~1m)
- 3.7Marking, jumping, copying, cutting, and pasting(48w~1m)
- 3.8Basic motions: moving by one character, word, line(112w~1m)
- 3.9Indenting and reindenting(61w~1m)
3.1Inserting text
Most of the time, you simply type the file contents.
3.2Common keys
- To exit Emacs,
C-x C-c
. - To cancel a long-running action, or exit a prompt, press
C-g
. - To find/start/open/visit a file,
C-x C-f
. The file doesn't have to exist. - To autocomplete a file name or command name in minibuffer, press
TAB
. - To start a shell,
M-x shell
.
3.3Discover Emacs, don't memorize it
- To find out what a key does, press
C-h k
(describe key sequence), and then press the keys. Emacs will tell you what function the key is bound to. - To find out what a function does, press
C-h f
and type the function name. Emacs will also tell you what keys are bound to that function. C-h ?
help for helpC-h a
apropos (find command matching a given pattern)C-h i
info document reader; usually documents some emacs packages/pluginsC-h t
tutorial for EmacsC-h r
read Emacs manual
3.4Using buffers and windows
- buffers
C-x C-f
opens a file or directory into a bufferC-x C-b
lists buffers in the other windowC-x C-LEFT
goes to previous bufferC-x C-RIGHT
goes to next bufferC-x k
kills a bufferC-x s
saves some buffersC-x b
switches to another buffer, or opens a new bufferC-x 4 b
the same, but in the other window
- windows
C-x 0
delete current windowC-x 1
delete other windows (all windows except the focused one)C-x o
switches to another window / the other windowC-x 2
split window belowC-x 3
split window right
C-x 4 0
kill-buffer-and-windowC-x 4 C-f
open a file in the other window
3.5Autocompletion
- 3.5.1Elisp autocompletion(7w~1m)
- 3.5.2Org Mode autocompletion using pcomplete(32w~1m)
- 3.5.3Word autocompletion with dabbrev (dynamic abbreviation)(33w~1m)
3.5.1Elisp autocompletion
C-M-i
autocomplete symbol for Emacs Lisp
3.5.2Org Mode autocompletion using pcomplete
M-TAB
(that is likely reserved by your window manager) orESC TAB
orC-M-i
. For example, type#+
at the beginning of a line and then pressC-M-i
.
3.5.3Word autocompletion with dabbrev (dynamic abbreviation)
This can save us from typing long words repeatedly. See Dynamic Abbrevs - GNU Emacs Manual for what these does. Try it.
M-/
dabbrev-expandC-M-/
dabbrev-completion (I rarely use this)
3.6Searching
- entering incremental search
C-s
forward incremental searchC-r
reverse incremental searchC-M-s
forward incremental search regex- The regex syntax is somewhere in A guided tour of Emacs
- While in incremental search prompt:
C-s
go to next occurrenceC-r
go to previous occurrenceM-p
previous item in search historyM-n
next item in search history
C-h k C-s
describe your options while in incremental search promptM-%
(that is Alt+Shift+5 on an English QWERTY keyboard) query replace
3.7Marking, jumping, copying, cutting, and pasting
- rapidly moving between two locations
C-SPC
set mark to current pointC-x C-x
swap mark and current point
- while marking
- cutting
C-w
kill region (cut it into clipboard)M-w
copy region into clipboard
- cutting
C-k
kill the rest of current lineC-y
paste from clipboardC-S-BKSP
kill entire line
Some commands set the mark.
3.8Basic motions: moving by one character, word, line
- moving by a character or a line
- horizontally/characterwise
C-b
moves the cursor back to the previous characterC-f
moves the cursor forward to the next characterC-a
moves the cursor to the beginning of lineC-e
moves the cursor to the ending of line
- vertically/linewise
C-n
moves the cursor down to the next lineC-p
moves the cursor up to the previous line
- horizontally/characterwise
- moving by a screen
M-v
goes up a screenC-v
goes down a screenC-M-v
goes down a screen in the other windowC-M-S-v
goes up a screen in the other window
Emacs call scroll-up-command for what I think as going down a screen. Emacs thinks about moving the text. I think about moving the viewport.
3.9Indenting and reindenting
TAB
indents in elisp mode, but not in org modeC-M-\
(indent-region) reindents region according to current modeM-\
remove space around cursorC-u 4 M-x indent-rigidly
deepens indentation of the selected (marked) region by 4 units regardless of mode
- 3.9.1<2018-09-17> Woe: inconsistent newlines: C-j and C-m(23w~1m)
3.9.1<2018-09-17> Woe: inconsistent newlines: C-j and C-m
In org mode: C-j
inserts newline and indents; C-m
inserts newline. In elisp mode, they are flipped.
4Navigating the file system using dired
Input C-x C-f
, input a directory path, and press Enter. Emacs opens Dired (directory edit) mode.
- Navigating:
BKSP
moves the cursor up one item.SPC
moves the cursor down one item.g
rereads the directory.l
(small L) refreshes the display. Don't use this; useg
instead.
- Opening:
RET
opens the item in the current window. If it's a directory, emacs opens another dired. If it's a file, emacs opens the editor.o
(small O) opens the item in the other window.
Read the dired info documentation.
Use C-h m
or M-x describe-mode
to get some help.
- Unanswered question:
- What is the Emacs equivalent of VSCode Ctrl+P (open file fuzzy search / approximate string matching / subsequence matching)?
- Completion styles.
- arcane stuffs
- What is the Emacs equivalent of VSCode Ctrl+P (open file fuzzy search / approximate string matching / subsequence matching)?
4.1Moving files into another directory, updating buffers
- In dired (note upper and lower case shortcuts):
- Mark the files with
m
or unmark withu
. - Press
R
. - Enter the destination directory.
- Press
RET
.
- Mark the files with
- Dired: marking, copying, moving and deleting files | Pragmatic Emacs
5Editing in Org Mode
We'll mostly be inserting headers (lines that begin with asterisks) and bodies. Sometimes we make a header a TODO item. Sometimes we insert a date. Sometimes we move trees around.
- 5.1Inserting, editing, and moving headers and subtrees(68w~1m)
- 5.2Inserting code blocks quickly(25w~1m)
- 5.3Making TODOs(25w~1m)
- 5.4Navigating an org file(42w~1m)
- 5.5Inserting lists(43w~1m)
- 5.6Inserting dates(84w~1m)
- 5.7Links(74w~1m)
- 5.8Searching hidden text such as link URL(10w~1m)
- 5.9Inserting table of contents(4w~1m)
5.1Inserting, editing, and moving headers and subtrees
- Begin the line with one or more asterisks (
*
). - moving subtrees
M-UP
move subtree upM-DOWN
move subtree down
- promoting and demoting
M-LEFT
promote header one level shallowerM-RIGHT
demote header one level deeperM-S-LEFT
promote subtree one level shallowerM-S-RIGHT
demote subtree one level deeper
C-c C-w
refile (move) subtree into (as a child of) a level-1 heading in the same org file. See the help of the functionorg-refile
.
5.2Inserting code blocks quickly
Type <s
and press TAB
.
- org babel - Quickly insert source blocks in org mode - Emacs Stack Exchange
- The Org Manual: Easy templates
5.3Making TODOs
S-LEFT
/S-RIGHT
cycle item TODO stateS-M-RET
insert TODO below current itemS-UP
/S-DOWN
change item priority (see "TODO Items > Priorities" in manual)
5.4Navigating an org file
- cycling visibility
TAB
cycle subtree visibilityS-TAB
cycle global visibility- See also Org Mode manual "Visibility cycling".
- moving around
C-c C-n
next headingC-c C-p
previous headingC-c C-f
next heading same levelC-c C-b
previous heading same levelC-c C-u
backward to higher level headingC-c C-j
jump/goto
5.5Inserting lists
- Begin the line with a hyphen (
-
). M-RET
to enter the next item.RET
to end the list.TAB
to indent the current item one level inward.S-TAB
to unindent the current item one level outward.
Convert lines to lists using mark and C-x r t
: https://stackoverflow.com/questions/2250930/emacs-orgmode-how-to-make-a-list-from-a-block-of-lines
5.6Inserting dates
C-c .
open calendar to insert date; the date will show up in agenda.C-c ! RET
open calendar to insert date; the date won't show up in agenda.- In the calendar
- The default selected date is today date.
- S-LEFT / S-RIGHT move selection 1 day backward/forward
- M-S-LEFT / M-S-RIGHT move selection 1 month backward/forward
- < / > scroll calendar by 1 month
- C-v / M-v scroll calendar by 3 months
- M-S-UP / M-S-DOWN scroll calendar by 1 year
- org mode - Change date from active to inactive - Emacs Stack Exchange
5.7Links
- The syntax is
[[URL][text]]
. C-c C-l
edit link if cursor is on a link; otherwise insert link.- To copy the URL of an Org link to clipboard, use the workaround
C-c C-l C-a C-k C-g
. C-c C-o
open link. If the target is a local org file, Emacs will open it in new window. PressC-c 0
to close the currently focused window.
- 5.7.1Chrome bookmarklet: Make org-mode link for page(15w~1m)
5.7.1Chrome bookmarklet: Make org-mode link for page
This may produce invalid markup. Check before you copy.
javascript:window.prompt("Copy to clipboard: Ctrl+C, Enter", "[[" + document.URL + "][" + document.title + "]]");
5.8Searching hidden text such as link URL
- https://emacs.stackexchange.com/questions/21208/search-and-replace-invisible-url-link-in-org-mode
M-x org-toggle-link-display
- The
search-invisible
variable
5.9Inserting table of contents
https://orgmode.org/manual/Table-of-contents.html
6Making Org play nice with Jekyll
- 6.1Other people's options(101w~1m)
- 6.2My choice: Elisp program with proper metadata mapping(19w~1m)
- 6.3Exporting Org to HTML for use with Jekyll(35w~1m)
- 6.4<2018-09-16> Porting some old Markdown content to Org Mode(302w~2m)
6.1Other people's options
- 6.1.1Emacs Lisp programs with improper metadata mapping(54w~1m)
- 6.1.2Emacs Lisp programs with proper metadata mapping(22w~1m)
- 6.1.3Jekyll plugins(10w~1m)
- 6.1.4Too idealistic(16w~1m)
6.1.1Emacs Lisp programs with improper metadata mapping
I want Emacs to transform in-buffer setting to front matter. I don't want BEGIN_HTML
or BEGIN_EXPORT
. I want proper metadata mapping #+SOMETHING
. The proper solution is to define a custom backend that derives the preamble from the org file metadata.
I reject these proposed solutions.
6.1.2Emacs Lisp programs with proper metadata mapping
One problem: I write wiki not blog.
- Antoine R. Dumont's org2jekyll
- Adrian Malabarba's ox-jekyll extension
- yoshinari-nomura/org-octopress: org-mode in octopress
6.1.3Jekyll plugins
Interesting: Jekyll plugin eggcaker/jekyll-org. But does it support org-ref?
6.1.4Too idealistic
How to render org-mode file from jekyll/github without converting .org to .html? - Stack Overflow
6.2My choice: Elisp program with proper metadata mapping
I mostly follow the official tutorial, but I do proper metadata mapping.
6.3Exporting Org to HTML for use with Jekyll
I have already had a wiki in Jekyll.
C-c C-e h h
export thisfile.org to thisfile.html; silently overwrite the outputC-c C-e h H
export to buffer; don't create file
6.4<2018-09-16> Porting some old Markdown content to Org Mode
- 6.4.1Options(47w~1m)
- 6.4.2Preliminary clean-up: Remove YAML front-matter, Jekyll tags, and Liquid tags(66w~1m)
- 6.4.3Use Pandoc(42w~1m)
- 6.4.4Check for wrong code blocks, inline markups, maths, tables(9w~1m)
- 6.4.5<2018-09-17> Don't follow this manual way(134w~1m)
6.4.1Options
- How to migrate Markdown files to Emacs org mode format - Emacs Stack Exchange
- Pandoc can convert Markdown to Org. There is even Try pandoc online, but don't post sensitive data online, such as password, company secret, etc.
We want to convert from Jekyll GitHub-Flavored Markdown to Org Mode.
6.4.2Preliminary clean-up: Remove YAML front-matter, Jekyll tags, and Liquid tags
Remove the YAML front-matter. Later, manually add the corresponding Org Mode metadata into the output. (Or white a Lua script? Pandoc mentions something about Lua scripting.)
Manually resolve Jekyll link tags such as {% raw %}{% link something.md %}{% endraw %}
. You can use regex if your html file name always coincides with the source md file name.
6.4.3Use Pandoc
<2018-09-18> This assumes Pandoc 2.3. Don't use the one that comes with Ubuntu 14.04; it's too old.
Download statically-linked Pandoc 2.3 for your operating system.
pandoc --from markdown --to org --wrap=preserve --output OUTPUT.md INPUT.md
The option --wrap=preserve
is illustrated in Preserve Line Breaks in Pandoc Markdown -> LaTeX Conversion - Stack Overflow.
6.4.4Check for wrong code blocks, inline markups, maths, tables
6.4.5<2018-09-17> Don't follow this manual way
The proper solution is to write a program that transforms Markdown AST to Org AST, but the amount of Markdown I want to convert does not justify the effort of writing such program. Thus I use Visual Studio Code and regexes to transform some files manually.
(<2018-09-17> Someone else already wrote that program. It's called Pandoc.)
These regexes don't work if any wildcard matches a funny character. We need context-free grammar for that.
Converting links
VSCode regex replace pattern for converting Markdown links to Org Mode links:
\[(.*)\]\((.*)\) [[$2][$1]]
Converting headers
This is VSCode regex replace pattern for converting Markdown headers to Org Mode headers. Repeat until there is no matches. Beware of inadvertently matching Bash comments if any.
There should be a trailing space after every of these regexes.
^## * ^### ** ^#### *** etc.
Backslashes, code blocks, and inline code
Replace
`
with=
.
7Getting more help
- 7.1Online introductory materials(27w~1m)
- 7.2Org Mode introductory materials(30w~1m)
- 7.3Reading the Org Mode manual(68w~1m)
- 7.4Navigating an info document(69w~1m)
7.1Online introductory materials
- Read the emacs manual in emacs: press
C-h r
. - Open the info browser: press
C-h i
. - Vivek Haldar — The levels of Emacs proficiency
- Xah Lee's Emacs tutorial
7.2Org Mode introductory materials
- Org mode for Emacs – Your Life in Plain Text
- Creating Gantt charts by Exporting to TaskJuggler
- Learning org-mode from others
7.3Reading the Org Mode manual
Read the manual with M-x org-info
. However, on Ubuntu 14.04, there is a catch: M-x org-version
says 8.2.4, but emacs comes with its own org-mode 7.9.3f documentation in /usr/share/info/emacs-24
that precedes /usr/share/info
. Use C-u C-h i /usr/share/info/org.gz RET
to open the correct documentation.
There is a shorter HTML version online: the compact org-mode guide, but it's more convenient to browse the info document in emacs.
7.4Navigating an info document
q
quits the info viewerRET
follows the link under cursorTAB
moves the cursor to the next linkS-TAB
moves the cursor to the previous linkl
(small L) goes back to the previous page in your browsing historyu
goes up to the parent page (of the current page)n
goes to the next pagep
goes to the previous pageh
brings up help about how to use the info viewer
8Programming in Emacs Lisp
- 8.1Basics(199w~1m)
- 8.2Suggested workflow(51w~1m)
- 8.3Debugging Emacs Lisp using trace.el(143w~1m)
- 8.4Comment indentation surprises(15w~1m)
8.1Basics
M-\
delete all spaces and tabs around pointC-M-k
kill s-expressionC-x C-e
eval last s-expression before point- indenting/formatting
C-M-\
indent regionM-x indent-sexp
- partial reloading
C-M-x
orM-x eval-defun
M-x eval-region
- setq
- add-to-list
- load
- load-path
- funcall
For someone used to Scheme, Emacs Lisp is unpleasant. 2018-09-12: When will Guile Emacs be mainstream?
- How do I define a local function?
- You define it with let and lambda, as in Scheme. But you call it by prepending funcall. If in Scheme you write
(f a b)
, then in Emacs Lisp you write(funcall f a b)
.- Why do I need this funcall noise? Because it's Lisp-2. If you don't like that, and you don't mind depending on the
cl
package, then you can useflet
as described in EmacsWiki: Local Functions. However,cl
is a big package; it's a waste of dependencies if you only useflet
.
- Why do I need this funcall noise? Because it's Lisp-2. If you don't like that, and you don't mind depending on the
- You define it with let and lambda, as in Scheme. But you call it by prepending funcall. If in Scheme you write
- Warts for historical reasons
- Lexical scoping is not the default, probably because it's easier to write a dynamic-binding interpreter than a .
- Things that are not a big deal in practice
- Emacs Lisp doesn't have proper tail calls. However, in practice, I have needed proper tail calls to customize Emacs.
- Contentious
- Lisp macro is simpler than Scheme macro, but Lisp macro is unhygienic.
- Famous Programers on How Common Lisp Sucks
- EmacsWiki: Why Does Elisp Suck
- EmacsWiki: Emacs Lisp Limitations
8.2Suggested workflow
This example is for developing org mode:
Run several emacs processes:
- In workspace 1, run
emacs -mm
for writing Emacs Lisp code. - In workspace 2, run
emacs -fh --no-desktop --visit ORGFILE
for testing. We'll often restart this instance. - In workspace 2, run
emacs -Q -fh --eval '(info "org")'
for reading the org mode manual.
8.3Debugging Emacs Lisp using trace.el
<2018-09-15> My use case: Debug my custom org-mode exporter for Jekyll. I want to dump the plist passed to my-front-matter
.
Emacs Lisp manual chapter "Debugging Lisp programs" mentions trace.el
. The usage instructions are near the beginning of Emacs 26.1 trace.el source code, which also documents some limitations.
The trace.el
method is the most limited, but it is the simplest to use, among the options.
Here are the steps.
Install advices. Put cursor after closing paren, and then
C-x C-e
to eval the sexp.;; replace the function name symbol with yours (trace-function-background 'myhtml-inner-template) (trace-function-background 'my-front-matter)
- Do something that will run the function. In my case, I run
M-x my-org-export-for-jekyll
. - Open the
*trace-output*
buffer (usingC-x b
). - Prettify the s-expression: Move the cursor to the opening paren, and then
C-u M-x indent-pp-sexp
. - Remove noisy s-expression such as
:exported-data
by usingC-M-k
(M-x kill-sexp
). This can also remove a long string. After you have finished debugging, remove advices (or just restart Emacs).
(untrace-all)
8.4Comment indentation surprises
Automatic indentation of comments surprises people who don't know Emacs Lisp comment conventions.
9Configuring Emacs further
- 9.1Using emacs packages(10w~1m)
- 9.2Other people's Emacs setups and init files(40w~1m)
9.1Using emacs packages
9.2Other people's Emacs setups and init files
- John Wiegley
- Steve Yegge
- Arnaud Malapert's org mode configuration
- How does your Emacs setup look like? : emacs
- What does your Emacs setup look like? - Quora
- emacs-tw/awesome-emacs: A community driven list of useful Emacs packages, libraries and others.
10Using Git from Emacs
- 10.1TODO <2018-09-18> Compare various Emacs integrations(36w~1m)
- 10.2TODO Install Magit(28w~1m)
- 10.3Using magit(92w~1m)
10.1TODO <2018-09-18> Compare various Emacs integrations
What are the options and differences? Which one should I use?
- EmacsWiki: Git
- VC
- Magit
- git-el
VC comes with Emacs.
10.2TODO Install Magit
- 10.2.1<2018-09-16> Woe: Magit requires Git >= 1.9.4(19w~1m)
10.2.1<2018-09-16> Woe: Magit requires Git >= 1.9.4
Ubuntu 14.04 comes with Git 1.9.1. Build from source, or add a PPA.
10.3Using magit
See also "Getting started" in Magit user manual.
- 10.3.1Open the Magit status window(24w~1m)
- 10.3.2Basic commands(13w~1m)
- 10.3.3Stage or unstage files and hunks(30w~1m)
- 10.3.4Commit(12w~1m)
- 10.3.5Rebase(9w~1m)
10.3.1Open the Magit status window
Open Magit status using C-x g
or M-x magit-status
.
The following sections assume that you are in the Magit status window.
10.3.2Basic commands
q
close the Magit status windowp
move upn
move downh
helpg
refresh
10.3.3Stage or unstage files and hunks
s
stage a file or hunku
unstage a file or hunkTAB
toggle visibility of a section (files in a section, or hunks in a file)
10.3.4Commit
c
open commit dialogc
make normal commitC-c C-c
commitC-c C-k
cancel commit
10.3.5Rebase
r i
start interactive rebase; follow instructions on screen
11Next steps
- 11.1TODO <2018-09-17> Use citeproc instead of org-ref?(27w~1m)
- 11.2TODO Install evil-mode(3w~1m)
- 11.3TODO My Emacs wishlist(88w~1m)
- 11.4TODO Make a personal wiki(17w~1m)
- 11.5Unorganized content(269w~2m)
11.1TODO <2018-09-17> Use citeproc instead of org-ref?
<2018-09-17> Citeproc markup language seems to be more expressive than org-ref markup language.
Example of citeproc syntax from Pandoc - About pandoc:
[see @doe99, pp. 33-35; also @smith04, ch. 1]
11.2TODO Install evil-mode
11.3TODO My Emacs wishlist
- 11.3.1What vim can do(54w~1m)
- 11.3.2What VSCode can do(23w~1m)
- 11.3.3Convert from Lisp list to YAML for Jekyll front matter(10w~1m)
11.3.1What vim can do
Does evil-mode count?
*
search next occurrence of wordd%
delete to matching delimiterhttps://stackoverflow.com/questions/20458990/emacs-how-to-delete-until-and-including-bracket
%
jump to matching delimiterDONE <2018-09-17> keyword local completion
C-x C-p
,C-x C-n
This is dynamic abbreviations in Emacs.
Vim insert mode Emacs C-x C-p M-/ C-x C-n C-u - M-/ In Vim,
C-p
orC-n
for next match. In Emacs,M-/
for next match.line autocompletion
C-x C-l
11.3.2What VSCode can do
Copy link URL
TODO <2018-09-16> Ctrl+P fuzzy/subsequence file path matching
I heard this is possible in Emacs using helm-projectile.
Ctrl+Shift+F find in files
11.3.3Convert from Lisp list to YAML for Jekyll front matter
11.4TODO Make a personal wiki
I use Jekyll and Org Mode. I don't use these.
11.5Unorganized content
- 11.5.1Staying healthy(7w~1m)
- 11.5.2Should we do everything in Emacs?(47w~1m)
- 11.5.3Other editors I haven't tried(39w~1m)
- 11.5.4Emacs stuffs, that may be similar to org-mode, from Ubuntu 14.04 package list, that I haven't tried(68w~1m)
- 11.5.5How are we supposed to use the Org Mode Agenda?(48w~1m)
- 11.5.6Is there too much technical debt / corner-cutting in Emacs?(24w~1m)
- 11.5.7Trying spacemacs, helm, ivy, projectile(41w~1m)
11.5.1Staying healthy
11.5.2Should we do everything in Emacs?
<2018-09-16> No. Emacs isn't always fast.
- erc: IRC with emacs
- opening PDF with emacs
- browsing the Internet with emacs
- icicles: "emacs library that enhances minibuffer/input completion"
- programming: paredit, haskell-mode, golang-mode, etc.
- Compare Emacs Lisp and Vim Script
- remembrance-agent: Emacs mode to help find relevant texts
- twittering-mode: Twitter client for Emacs
11.5.3Other editors I haven't tried
- Diakonos - a linux editor for the masses
- Spacemacs
- non-free
- TeX/LaTeX
- GNU TeXmacs
- WhizzyTeX
- XEmacs? emacs-lucid?
11.5.4Emacs stuffs, that may be similar to org-mode, from Ubuntu 14.04 package list, that I haven't tried
- "BHL is an Emacs mode that enables you to convert text files into HTML, LaTeX and SGML files."
- "Howm(Hitori Otegaru Wiki Modoki) is a note-taking tool on Emacs."
- emacs-wiki
- mhc: schedule management tool for emacsen
- muse-el: Author and publish projects using Wiki-like markup
- smartdoc-elisp: emacs mode for smartdoc
- sisu: documents - structuring, publishing in multiple formats and search
11.5.5How are we supposed to use the Org Mode Agenda?
I have already been using Google Calendar. I don't use org agenda.
I don't give deadlines to my todos. Of what use is the agenda then?
An agenda summarizes/aggregates org files. See "Agenda Views > Agenda Files" in org mode manual.
11.5.6Is there too much technical debt / corner-cutting in Emacs?
Examples:
- global variables in the C code preventing multithreading
- Org ad hoc code, quick hacks and workarounds
11.5.7Trying spacemacs, helm, ivy, projectile
Spacemacs is quite polished compared to vanilla Emacs.
Woe: Emacs can't install packages in parallel.
What is helm? What is ivy? What is projectile? What is helm-projectile?
12Obsolete content
Don't read these. These are no longer useful.
- 12.1Installing Emacs 24.3 on Ubuntu 14.04(40w~1m)
- 12.2How to uninstall Emacs on Ubuntu 14.04(15w~1m)
12.1Installing Emacs 24.3 on Ubuntu 14.04
I tried using emacs 24.3 that comes with ubuntu 14.04 but it was too old. Skip this.
On 2018-09-07, the command sudo apt-get install emacs magit org-mode org-mode-doc
on Ubuntu 14.04 will install:
- emacs 24.3.1
- magit 1.2.0
- org-mode 8.2.4
12.2How to uninstall Emacs on Ubuntu 14.04
From Uninstalling Emacs with apt-get: lessons in interface design:
sudo apt-get remove 'emacs*'
13Emacs woes: Sometimes Emacs is a pile of hacks and workarounds.
- http://ergoemacs.org/emacs/emacs_narrow-to-defun_eval-defun_bug.html
- Xah found that on Emacs 24.4.1.
- <2018-09-27> I'm using Emacs 26.1 and it still has that programming error.
14Finding out what is slowing down Emacs
- https://www.reddit.com/r/emacs/comments/6uhzc9/very_slow_org_mode/
M-x profiler-start
- Do something slow.
M-x profiler-report
.