ctarbide / emacs-tips

Emacs Tips

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Emacs Tips

For the absolute beginner

Press Control + ‘h’ twice then press ‘t’, or press Control + ‘h’ then ‘t’ to go directly to the tutorial.

Control + ‘h’ then ‘?’ (C-h ?) also opens Metahelp, press ‘q’ to dismiss.

The menu can be very helpful to learn common key bindings, use F10 to bring it up.

Finding more information

  • Metahelp (q to dismiss)
    • C-h C-h
    • C-h ?
  • Symbol Search
    • M-x apropos RET
  • Emacs
    • Emacs Manual (C-h r or C-h i d m emacs RET)
      • info:emacs
  • Emacs Lisp
    • Introduction to Programming in Emacs Lisp (C-h i m Emacs Lisp Intro)
      • info:eintr
    • Emacs Lisp Reference Manual (C-h i m Elisp)
      • info:elisp

Shortcuts/Key Bindings

Help/Discovery/Diagnostics

C-h ? b
Display all key bindings
C-h C-h b
Display all key bindings
C-h b
Display all key bindings
C-h k
Describe a key binding
C-h c
Describe a key binding briefly
C-h m
Describe current major and minor modes
C-c C-h
show all bindings starting with “C-c” prefix
C-x C-h
show all bindings starting with “C-x” prefix
C-x c C-h
show all bindings starting with “C-x c” prefix
C-h e
show messages buffer

Info Pages

Info pages are a rich and reliable source of information.

C-h i
show Info Browser
?
list key bindings, press SPC to scroll
d
go to info directoy
m
go to a menu item
I
search in the indices of current document
TAB
next link
S-TAB
previous link
c
copy name of the current info node
M-x info-apropos
search in the indices of all manuals
s
regexp search document
C-s
incremental search forward
C-r
incremental search backward
n
next
p
previous
u
up
t
top of current document
q
quit (selects previous buffer)

Searching goes through multiple Info nodes, see (info) Search Text for more information.

Text Searching

C-s
incremental search forward
C-r
incremental search backward
C-M-s
incremental search forward (regular expression)
C-M-r
incremental search backward (regular expression)

Text Selection and Basic Editing

C-SPC
set-mark-command (start selection)
C-w
kill-region (cut)
M-w
kill-ring-save (copy)
C-y
yank (paste)
C-x h
mark-whole-buffer (select all)
M-\
delete-horizontal-space

Keyboard Macros

C-x (
kmacro-start-macro
C-x )
kmacro-end-macro
C-x e
kmacro-end-and-call-macro

See (emacs) Keyboard Macros for more information.

Lisp evaluation

  • M-:
  • C-x C-e
  • C-M-x
  • M-x ielm
  • M-x eshell

You really should known what each of these do (use C-h k to describe each key binding).

Shell

M-p
previous command
M-n
next command
C-c C-c
send interrupt signal
C-c C-d
close stdin (send eof)
C-c C-p
previous prompt
C-c C-n
next prompt
C-c C-l
show input history
C-c M-o
clear buffer

File Variables

File variables are important to select and customize major and minor modes in a file-by-file basis, see Specifying File Variables

How to access/examine/inspect file variables? <elisp:(symbol-value ‘file-local-variables-alist)>, this variable may be nil, as it is best practice to remove them as they are consumed by their handling modes.

See also:

  • Directory Variables

Org Mode Tips

Shortcuts

Basics and navigation.

TAB / S-TAB
(un)fold
M-up / M-down
move a headline up or down
M-left / M-right
promote or demote a headline
M-S-left / M-S-right
promote or demote a subtree
C-RET
insert a new headline
M-RET
insert a new item
C-c C-w
move entries
C-c -
change list style
C-c / t
show TODOs in current document
S-left / S-right
very useful, depends on context
C-c C-c
context-dependant, toggle checkboxes, tagging
C-c C-o
opens link
C-c C-e
export menu (try ‘h’ then ‘o’)
C-c C-e C-s
export scope configuration
C-c C-z
take note
C-c C-t
cycle task states
C-u 5 C-c C-t
set 5th task state (or clean it if 5th is not set)

Miscellaneous

=C-c C-,=
org-insert-structure-template (e.g. insert being_src blocks)
=C-c ‘=
org-edit-special, org-edit-export-block and others
C-c $ or C-c C-x C-s
org-archive-subtree

Links

  • explict using angular/angle brackets: <info:eintr#Symbols as Chest>
  • implict/nice/rich using square brackets: eintr: Symbols as Chest

Examples

Lord of the Rings (from org4beginners, see references)

My favorite scenes are (in this order)

  1. The attack of the Rohirrim
  2. Eowyn’s fight with the witch king
    1. this was already my favorite scene in the book
    2. I really like Miranda Otto.
  3. Peter Jackson being shot by Legolas
    1. on DVD only

    He makes a really funny face when it happens. But in the end, no individual scenes matter but the film as a whole. Important actors in this film are:

    Elijah Wood
    He plays Frodo
    Sean Austin
    He plays Sam, Frodo’s friend. I still remember him very well from his role as Mikey Walsh in The Goonies.

Text Markups

You can make words bold, italic, underlined, code and verbatim, and, if you must, strike-through.

Checkboxes

Basic [1/2] [50%]

  • [ ] an item
  • [X] uset C-c C-c to toggle

Sublists

  • [-] Group 1
    • [ ] item 1
    • [X] item 2
  • [1/2] Group 2
    • [ ] item 1
    • [X] item 2
  • [50%] Group 3
    • [ ] item 1
    • [X] item 2
  • [1/2] Group 4 (does not count subitems)
    • [X] item 1
    • [-] item 2
      1. [X] subitem 2.1
      2. [ ] subitem 2.2
  • [50%] Group 5 (does not count subitems)
    • [X] item 1
    • [-] item 2
      1. [X] subitem 2.1
      2. [ ] subitem 2.2

Export

Shortcuts

  • HTML
    • C-c C-e h h (M-x org-html-export-to-html)
    • C-c C-e h H (M-x org-html-export-as-html)
    • C-c C-e h o (C-u M-x org-html-export-to-html)
      • exports and open in browser
    • M-x org-html-convert-region-to-html
      • insert html in current buffer at point
  • LaTeX
    • C-c C-e l l (M-x org-latex-export-to-latex)

References

  • M-: then (info-other-window "(org) HTML export commands")
  • M-: then (info-other-window "(org) LaTeX/PDF export commands")

Configuration

Change default workflow states

Add this to your initialization script:

(setq org-todo-keywords
  '((sequence "TODO" "IN-PROGRESS" "WAITING" "WONTDO" "DONE")))

Add timestamp when tasks are DONE

#+STARTUP: logdone

Add nice indentation and other configurations

#+STARTUP: indent logdone

Evaluation of Code Blocks

  • (org) Evaluating code blocks
  • (org) Results of evaluation

More Shortcuts

M-2 C-c .
insert timestamp

References

Some nice material.

IDO Tips

C-s, C-r, C-f, C-d, C-e, C-j are really useful, learn how to use them.

Shortcuts for ido-find-file

RET     Select the file at the front of the list of matches.
If the list is empty, possibly prompt to create new file.

C-j     Use the current input string verbatim.

C-s     Put the first element at the end of the list.
C-r     Put the last element at the start of the list.
TAB     Complete a common suffix to the current string that matches
all files.  If there is only one match, select that file.
If there is no common suffix, show a list of all matching files
in a separate window.
C-d     Open the specified directory in Dired mode.
C-e     Edit input string (including directory).
M-p     Go to previous directory in work directory history.
M-n     Go to next directory in work directory history.
M-s     Search for file in the work directory history.
M-k     Remove current directory from the work directory history.
M-o     Cycle to previous file in work file history.
C-M-o   Cycle to next file in work file history.
M-f     Prompt for a file and use find to locate it.
M-d     Prompt for a directory and use find to locate it.
M-m     Prompt for a directory to create in current directory.
C-x C-f Fallback to non-Ido version of current command.
C-t     Toggle regexp searching.
C-p     Toggle between substring and prefix matching.
C-c     Toggle case-sensitive searching of file names.
M-l     Toggle literal reading of this file.
?       Show list of matching files in separate window.
C-a     Toggle ignoring files listed in ‘ido-ignore-files’.

Shortcuts for ido-switch-buffer

RET     Select the buffer at the front of the list of matches.
        If the list is empty, possibly prompt to create new buffer.

C-j     Use the current input string verbatim.

C-s     Put the first element at the end of the list.
C-r     Put the last element at the start of the list.
TAB     Complete a common suffix to the current string that matches
        all buffers.  If there is only one match, select that buffer.
        If there is no common suffix, show a list of all matching buffers
        in a separate window.
C-e     Edit input string.
C-x C-b Fallback to non-ido version of current command.
C-t     Toggle regexp searching.
C-p     Toggle between substring and prefix matching.
C-c     Toggle case-sensitive searching of buffer names.
?       Show list of matching buffers in separate window.
C-x C-f Drop into ‘ido-find-file’.
C-k     Kill buffer at head of buffer list.
C-a     Toggle ignoring buffers listed in ‘ido-ignore-buffers’.

References

Lisp Editing

C-M-f
forward sexp (s-expression)
C-M-b
backward sexp
C-M-k
kill sexp
C-- C-M-k
kill sexp (before point)

Paredit Basics

Slime Basics

Slime can be seem as a Lisp IDE

C-c C-k
compile whole file into an FASL file
C-c C-c
compile an individual block

Emacs Lisp Debugging

There are two well known interactive debugging solutions:

debugger
a debugger for the emacs lisp evaluator
edebug
A source-level Emacs Lisp debugger

Using debugger

Most Common Keybindings

?
help
q
quit
c
debugger-continue (step out)
d
debugger-step-through (step over)
e
debugger-eval-expression, eval expression in stack frame
  • see also: R

How to manually trigger the debugger

  • (describe-function 'debug-on-entry)

How to watch a variable

  • (describe-function 'add-variable-watcher)

Using edebug

SPC
edebug-step-mode

Step: stop at the next stop point encountered (edebug-step-mode).

b
edebug-set-breakpoint
g
edebug-go-mode

Go: run until the next breakpoint (edebug-go-mode).

c
edebug-continue-mode

Continue: pause one second at each breakpoint, and then continue (edebug-continue-mode).

S
edebug-stop

Stop: don’t execute any more of the program, but wait for more Edebug commands (edebug-stop).

h
edebug-goto-here
i
edebug-step-in
G
edebug-Go-nonstop-mode

Go non-stop: ignore breakpoints (edebug-Go-nonstop-mode). You can still stop the program by typing S, or any editing command.

n
edebug-next-mode
o
edebug-step-out
t
edebug-trace-mode

Trace: pause (normally one second) at each Edebug stop point (edebug-trace-mode).

Example

;; reference: https://www.youtube.com/watch?v=odkYXXYOxpo

;; (eval-buffer)
;; (main)
;; M-x edebug-Go-nonstop-mode RET (or simply G)

(require 'edebug)

(defmacro when-symbol-match (regex sym &rest body)
  `(when (string-match-p ,regex (symbol-name ,sym))
     ,@body))

(defun list-symbols (regex)
  (let ((lst))
    (mapatoms (lambda (a)
                (when-symbol-match
                 regex a
                 (push a lst))))
    lst))

(defun main ()
  (edebug-instrument-function 'list-symbols)
  (message "symbols: %S" (list-symbols "^file.*non.*"))
  (pop-to-buffer (get-buffer "*Messages*"))
  (other-window 1))

References

Emacs/Elisp Troubleshooting

Font test

C-h h
view-hello-file

Controlled startup

See (org) Batch execution for an example using -Q, --batch and --eval flags.

Example

emacs -Q --eval '(load-theme (quote wombat))'
# or
emacs -Q --eval '(load-theme (quote tango-dark))'

References

About

Emacs Tips