Emacs

Table of Contents

1 Mail

To start a mail, use compose-mail (C-x m). This will drop you to a message buffer, with message-mode.

  • message-send-and-exit (C-c C-c): send it

2 Tmp

  • toggle-truncate-lines: some file format, e.g. txt, will have a line wrap at the end of the screen. This might be annoying, and toggle this will truncate the line.

3 buffer

  • list-face-displays
  • fill-region
  • count-matches

4 Getting Help

  • describe-key-briefly: defaults to C-h c, return the command name of a key stroke.
  • where-is: defaults to C-h w, return the current shortcut for a command
  • info: defaults to C-h i, open the built-in info reader.
  • view-echo-area-messages

5 Configuration

To configure a specific key map. Note that the global-set-key will not overwrite a specific key map, because the specific one has a higher priority.

  (define-key org-mode-map (kbd "C-j") (lambda()
                                         (interactive)
                                         (join-line -1)))

Package installation is done by the built-in package management. Be sure to add it into the start script to be able to automatically install it for a new build of emacs.

  • list-package: open the package page. Use U x to update all available.

6 Window and Frame

6.1 window manage

  • balance-window
  • toggle-window-split
  • enlarge-window
  • shrink-window

6.2 dired

  • dired-next-subdir
  • dired-prev-subdir
  • dired-tree-up
  • dired-tree-down

6.3 packages

In *package* buffer, Ux to update all packages.

7 File Operation

revert-buffer
Replace current buffer text with the text of the visited file on disk. I.e. Reload file.
recover-file
recover from #xxx# file.
read-only-mode
disable it to edit read only files

8 Editing

8.1 text transformation

  • replace-rectangle
  • upcase-word
  • downcase-word
  • transpose-words
  • transpose-lines

To insert a control sequence, use C-q xxx.

  • capitalize-word
  • fill-paragraph (M-q)
  • fill-region
  • auto-fill-mode

When replace-string, use C-q C-j to represent newline.

8.2 killing

  • zap-to-char
  • zap-up-to-char

8.3 spell checking

Use ispell, and flycheck to check on-the-fly. Check emacs manual for detail.

8.4 Replace regexp for multiple files inside project

  1. find-name-dired, select the root directory, and provide a pattern for files
  2. press t to toggle mark all files
  3. press Q for query and replace in files, and provide regexp and replacement
  4. proceed each of the match by: SPC to confirm, and n to skip to next.
  5. Finally all files are not saved. To save that, C-x s will prompt all file and wait for your confirm by y. To avoid hitting y many times, use C-x s !.

9 Programming

  • checkdoc: check the warnings in doc string. It can also fix it: C-h f.
  • C-x C-e: evaluate
  • C-u C-x C-e: evaluate and insert result

10 Remote

Use tramp can easily work with remote machine. It is shipped with Emacs. To use it, simply C-x C-f and enter /user@host:, Emacs will prompt password. user@ is optional. Do not use fancy shell prompt, otherwise tramp might hang. You can also enter /su:: to edit as root for local files.

You can use helm no problem, and you can also enter the remote shell.

11 Moving

Defun movement:

C-M-a
move to the beginning of defun
C-M-e
move to the end of defun
C-M-h
mark defun
C-M-x
evaluate defun

Sexp movement

C-M-f
move forward a sexp
C-M-b
move backward a sexp
C-M-k
kill a sexp
C-M-<SPC>
mark following sexp
C-M-n
move to the next sexp
C-M-p
move to the previous sexp

Other

C-M-t
transpose expressions
C-M-u
move up parenthesis
C-M-d
move down parenthesis
M-m
back-to-indentation

12 Navigating

  • forward-sexp: forward semantic block
  • backward-sexp
  • org-forward-heading-same-level: C-c C-f
  • org-backword-heading-same-level: C-c C-b

12.1 marking

  • exhange-point-and-mark
  • mark-word
  • mark-sexp
  • mark-paragraph
  • mark-defun
  • mark-page
  • mark-whole-buffer
  • point-to-register: save ppposition in a register
  • jump-to-register
  • set-mark-command: C-SPC, set mark, and activate it
  • C-SPC C-SPC: set mark, but not activate it.
  • C-u C-SPC: pop to previous mark in mark ring. current is stored at the end of mark ring(rotating)
  • pop-global-mark: will store both position and buffer

All events that set the mark:

  • C-SPC C-SPC
  • C-w
  • search

12.2 register

  • jump-to-register: the register can store a file
  • copy-to-register
  • insert-register

12.3 Tags

  • helm-etags-select

13 Special Modes

13.1 Tex Mode

  • tex-validate-region

14 Variables

14.1 File Local Variable

On first line, emacs will try to find

-*- mode: Lisp; fill-column: 75; comment-column: 50; -*-=

mode defines the major mode for this file, while unlimited numbers of variables follows, separated by ; Emacs looks for local variable specifications in the second line if the first line specifies an interpreter, e.g. shebang.

A second way to specify file local variable is to have a "local variables list" near the end of the file (no more than 3000 characters from the end of the file). The Local Variables: and End: will be matched literally.

This     /* Local Variables:  */
Is       /* mode: c           */
Garbage  /* comment-column: 0 */
Data     /* End:              */

You can also interactively add by add-file-local-variable, reload the variable by revert-buffer

14.2 Directory Local Variable

Put .dir-locals.el at the root directory, and it will be in effect for all the files under that directory, recursively. It should be an associate list, the car can be either a mode name (or nil applies to all modes) indicating the variables are for that mode, or a sub-directory name to apply only in that directory.

  ((nil . ((indent-tabs-mode . t)
           (fill-column . 80)))
   (c-mode . ((c-file-style . "BSD")
              (subdirs . nil)))
   ("src/imported"
    . ((nil . ((change-log-default-name
                . "ChangeLog.local"))))))

15 Advanced Topics

15.1 Info

Info is a document system. It is closely bundled with emacs, so I put it here. To install some new info document in the system, issue the following commands (using gnu-c-manual as an example):

# download the gnu-c-manual code
make gnu-c-manual.info
mv gnu-c-manual.info /usr/local/share/info
cd /usr/local/share/info
sudo install-info --info-file=gnu-c-manual.info --info-dir=.

15.1.1 Operations

key description
SPC page down, can cross node
BACKSPACE page up, can cross node
M-n clone-buffer, create a new independent info window
n next node on same level
p previous
] next node regardless of level
[ previous
u up node
l back
r forward
m Info-menu, convenient for search node title
s TODO search a text in the whole info file
i TODO search indices only

15.2 Babel

How to write a ob-xxx.el file?

some good example to look at: ob-plantuml.el, ob-C.el

16 Reference

Sacha's super long Emacs Config: http://pages.sachachua.com/.emacs.d/Sacha.html Some emacs.d I started with https://github.com/jordonbiondo/.emacs.d/blob/master/init.el C++ IDE and some tutorials: http://tuhdo.github.io/