Emacs

Date

November 21, 2020

1 (ansi-)term mode

  • C-c C-j (term-line-mode) switch to line mode (acts like an editor).

  • C-c C-k (term-char-mode) switch to char mode (acts like a usual terminal).

  • C-c C-c (term-interrupt-subjob) send a literal C-c to the sub-shell

  • C-c <char> is equivalent to C-x <char> in normal Emacs modes, e.g. C-c o invokes the global binding of C-x o which is normally `~other-window~’

  • C-c C-q (term-pager-toggle) toggle the page-at-a-time feature. When enabled, the mode-line displays the word `~page~’ and each time Term receives more than a screenful of output, it pauses and displays `~**MORE**~’ in the mode-line where <SPC> displays the next screenful of output and ? displays the other options – the interface is similar to the more program.

  • When the bash complains “cannot read termcap database”, then create a symlink ~/.terminfo/65 -> /usr/local/share/emacs/26.3/etc/e that is another symlink to /usr/local/Cellar/emacs-mac/emacs-26.3-z-mac-7.7/share/emacs/26.3/etc/. Sources say that the name of directory should be e but in this macos it has to be 65. Check this by ls /usr/share/terminfo and then sudo find /usr/share/terminfo -name "eterm*".

2 (Auto)reverting

3 \*buffers*

  • C-x C-b helm-buffers

  • M-SPC or C-SPC mark

  • C-x > and C-x < horizontal scroll

  • C-x C-s save buffer without leaving Helm

  • C-x s save all unsaved buffers

  • C-c d delete buffer without leaving Helm

  • M-D delete marked buffers and leave Helm

  • C-x k is meant to kill buffers but cannot kill multiple buffers selected by C-SPC with RET.

4 \*kill-buffer*

  • This seems only useful when killing the currently opened buffer

  • C-x k open kill-buffer

5 Abbrevs mode

  • https://www.emacswiki.org/emacs/AbbrevMode

  • abbrev-mode start the (minor) mode

  • C-x a l (add-mode-abbrev) Define abbrev for current mode.

  • C-x a i l (inverse-add-mode-abbrev) define a word in the buffer as a mode-specific abbrev

  • list-abbrevs Display a list of defined abbrevs

  • edit-abbrevs edit abbrevs. This is the best way to add or remove abbrevs. The number in the middle column is the number of times you’ve used (expanded) the abbrev. To remove a abbrev, just delete the line. To add a abbrev, just add a line. When done, to load and or save, call any of:

    • C-c C-c (edit-abbrevs-redefine) Redefine abbrevs according to current buffer contents.

    • C-x C-s (abbrev-edit-save-buffer) Redefine and save to abbrev file.

5.1 options

  • abbrev-file-name The abbrevs is saved in a file at a path specified by this variable (default: \~/.emacs.d/abbrev_defs)

  • save-abbrevs

6 Bookmarks

  • C-x r m (bookmark-set) Set the bookmark for the visited file, at point

  • C-x r l (list-bookmarks) list all bookmarks

  • C-x r b (bookmark-jump / helm-bookmarks / helm-filtered-bookmarks) then type a bookmark name. You can type \* for wildcard

  • bookmark-save save all the current bookmark values in the default bookmark file \~/.emacs.d/bookmarks. Can be modified by setting the variable bookmark-default-file

  • when in the bookmark file:

    • d mark the current item for remove

    • x remove all marked ones

    • r rename current item’s title

    • s save the change

7 Case conversion A <-> a

  • M-l/M-u (downcase-word/upcase-word) convert following word to lower/upper case.

  • M-c (capitalize-word) capitalize the following word.

  • C-x C-l/C-x C-u (downcase-region/upcase-region) convert region to lower/upper case

8 current file name without extension

(file-name-sans-extension (buffer-name))

9 Deleting and killing

9.1 Deleting by character

  • <Del>~/~C-d Delete the character before/after point

  • M-d~/~M-<Del> Kill forward/back to the end/beginning of the next/previous word

  • C-k~/~M-k Kill to the end of the line/sentence

  • C-o Insert a blank line after the cursor

  • C-S-<DEL> (kill-whole-line) Kill a whole line

  • C-u 50 or M-5 0 <moving_command> repeat the moving command by 50 times. The tutorial says C-u is better `because it works on any terminal’. `50’ is called a “prefix argument”. Exception: C-u 8 C-v~/~C-u 8 M-v Instead of scrolling 8 pages up/down, it scrolls the current screen by 8 lines up/down.

  • mouse-3 (right click) set the mark at point then select up to and move point to where you click

  • 'shifted cursor motion keys' Set the mark at point if the mark is inactive, then move point.

  • C-w Kill the selected region

  • C-x <TAB> Indent the selected region

  • M-$ Check the spelling of words

  • C-/, C-_, C-x u undo. On some text terminals, typing C-/ actually sends C-_ to Emacs.

  • C-g changes the `direction’. So, after undoing C-/ n-times, doing C-g C-/ and repeating C-/ (n-1)-times will do redo all.

  • the auto saved file’s name for “hello.c” is “#hello.c#”. To recover auto-saved editing, run M-x recover-this-file <RET>

10 Dired

  • C-x d invoke Dired, C-x 5 d (dired-other-frame)

  • g re-read all directories (retains all marks)

  • C-x C-q (dired-toggle-read-only) edit Dired buffer with Wdired, or make it read-only. After the edit, C-c C-c to apply the changes, or C-c <ESC> (or C-c C-k) to cancel.

    • We can even edit symlinks by editing the dired buffer: a symlink is displayed as <SYMLINK> -> <TARGET> and we can edit TARGET. Short listing mode only shows <SYMLINK> -> without target but we can switch to the long listing with (.

  • ( (dired-hide-details-mode)

  • find-grep-dired / (find-grep-dired DIR REGEXP) Find files in DIR that contain matches for REGEXP and start Dired on output.

10.1 directories

  • + (dired-create-directory) create a directory

  • C-M-n (dired-next-subdir) go to next subdirectory header line regardless of level

  • C-M-p (dired-prev-subdir) go to previous subdirectory header line regardless of level

  • C-M-u (dired-tree-up) go up to the parent directory’s header line

  • C-M-d (dired-tree-down)

  • ^ (dired-up-directory) go to parent directory

  • < (dired-prev-dirline) move up to the previous directory-file line

  • > (dired-next-dirline) move down to the next directory-file line

  • a (dired-find-alternate-file) visit file or directory on current line via

10.2 flags

  • d (dired-flag-file-deletion) flag ‘D’ the file for deletion

  • u (dired-unmark) remove the deletion flag

10.3 files

  • x (dired-do-flagged-delete) delete files flagged for deletion

  • to create a new file in the directory, just use C-x C-f as usual

  • f, e, <ret> (dired-find-file) visit the file

  • v (dired-view-file) view the file with view mode (visit without change)

  • a (dired-find-alternate-file) visit file or directory on current line via `find-alternate-file’

  • C NEW <RET> (dired-do-copy) copy; NEW is the directory to copy into ir the new name

  • D (dired-do-delete) delete the specified or marked files

  • x (dired-do-flagged-delete) delete all flagged files

  • S NEW <RET> (dired-do-symlink) make symbolic links; this is line ‘ln -s’

  • M MODESPEC <RET> (dired-do-chmod) change the mode

10.4 marks

  • m mark with ‘*’

  • u (dired-unmark) remove any mark

  • U (dired-unmark-all-marks) remove all marks from all files in the buffer

  • M-} (dired-next-marked-file) move down to the next marked file

  • M-{ (dired-prev-marked-file) move up to the previous marked file

  • C-_ (dired-undo) undo changes in the Dired buffer

10.5 shell command

  • M-x ! (‘dired-do-shell-command’) reads a shell command string in the minibuffer, and runs that shell command on one or more files

10.6 options

  • Dired-Listing-Switches

11 emacsclient

emacsclient -e '(with-current-buffer "Downloads" (dired-previous-line 3))'

12 File

  • save: C-x C-s

  • save as: C-x C-w

13 Find/replace

  • incremental search: C-s (forward), C-r (backward)

  • find all occurrence: M-s o (occur)

14 font

  • M-x customize-face<RET>default<RET>

  • C-x C-+ ((text-scale-adjust INC)) / C-x C-- ((text-scale-adjust -INC)) to increase/decrease the size.

  • C-x C-0 reset to the default font size

15 frames

  • C-x 5 2 (make-frame-command) create a new frame

  • C-x 5 0 (delete-frame) delete the selected frame

  • C-x 5 1 delete all frames except the selected one

  • C-x 5 o cycle and select other frames

16 helm

  • RET executes the default action upon exiting the Helm session

  • C-j executes the default action but without exiting the Helm session

  • TAB displays a list of actions available

  • C-@ mark/unmark a candidate

  • M-U unmark all visible marked candidates at once

17 help and info

  • C-h i (info) run Info, the GNU documentation browser

  • C-h b (describe-bindings) display all active key bindings

  • C-h c KEY (describe-key-briefly) display the name of the command that KEY is bound to

  • C-h k KEY (describe-key) display the name and documentation of the command that KEY runs

  • to open separate buffer for info, do M-2 M-x info, M-3 M-x info, etc.

  • C-h help apropos

  • C-h c <command> displays a very brief description of the command. Eg. C-h c C-p.

  • C-h k <command> displays the documentation of the function along with its name.

  • C-h f <function_name> describe a function. Eg. C-h f previous-line<RET>.

  • C-h a <keyword> or M-x <keyword> (command apropos) list all the commands whose names contain that keyword. Eg. C-h a file<RET> displays a list of all M-x commands with “file” in their names with character-command.

17.1 Browsing inside info

  • L (Info-history), l (Info-history-back), r (Info-history-forward)

  • ^ (Info-up)

18 Hide-show mode

  • hs-minor-mode

  • C-c @ C-a, C-c @ C-M-s (hs-show-all)

  • C-c @ C-t, C-c @ C-M-h (hs-hide-all)

  • C-c @ C-s (hs-show-block) Select a block and show it.

  • hs-hide-block Select a block and hide it.

  • C-c @ C-e, C-c @ C-c (hs-toggle-hiding) Toggle hiding/showing of a block.

19 history

  • C-h l (view-lossage) Display last few input keystrokes and the commands run.

20 ispell (flyspell)

  • M-$ Check and correct spelling of the word at point (ispell-word). If the region is active, do it for all words in the region instead.

    • i Insert this word in your private dictionary file so that it will be considered correct from now on, even in future sessions.

21 Key-binding

In case of conflict between key-bindings with major or minor modes, we can add a hook as the following to remove or change the undesired key-bindings. Once ‘unbounded’, the it will be bound to another mode in lower priority.

(add-hook 'reftex-mode-hook
          (lambda ()
            (define-key reftex-mode-map "\C-c&" nil)
            (define-key reftex-mode-map "\C-c(" 'my-insert-label)))

22 Magit

24 Modes, listing all major and minor

  • C-h m (M-x describe-mode) Display documentation of current major mode and minor modes.

25 Moving around

25.1 by characters/lines

  • C-b~/~C-f move back/forward by one character

  • C-p~/~C-n move up/down by one line

25.2 by words

  • M-b~/~M-f move back/forward by one word

25.3 horizontally

  • C-a~/~C-e move to the beginning/end of the line

  • M-m (back-to-indentation) Jump to first non-whitespace character of the line

25.4 by sentence

  • M-a~/~M-e move to the beginning/end of the sentence

25.5 by paragraph

  • M-{ (backward-paragraph) Move back to previous paragraph beginning

  • M-} (forward-paragraph) Move forward to next paragraph end.

25.6 by matching parentheses

  • C-M-f (forward-sexp)

  • C-M-b (backward-sexp)

25.7 by buffer

  • M-< M-> move to the beginning/end of the buffer

25.8 by page

  • C-v M-v page down/up by one page

  • C-M-v scroll the other window

  • C-l Clear screen and redisplay all the text, moving the text around the cursor to the center of the screen

25.9 back to previous position

  • C-<SPC> Set the mark at point

  • C-x C-x Set the mark at point, activate, then move back to the original point

25.10 GO TO

  • M-g g go to the line

26 Outline minor mode

  • A heading line together with all following body lines is called an entry.

    • outline-hide-entry and outline-show-entry only applies to the body.

    • The body as in outline-hide-body refers to all body lines in the buffer.

    • All as in outline-show-all literally means all of the lines in the buffer.

  • A heading line together with all of its deeper entries is called a subtree.

  • Immediate (one level down) subheadings of the current heading is called children.

  • All of deeper subheadings under the current heading are called branches.

  • Bodies of the current heading and bodies of its subheadings are collectively called leaves.

  • Everything except the top N levels of heading lines and their bodies are called sublevels

  • Everything except the heading or body with heading lines leading up to the top level are called other

26.1 Motion commands

  • C-c @ C-n (outline-next-visible-heading) move point to the next visible heading line

  • C-c @ C-p (outline-previous-visible-heading) move point to the previous visible heading line

  • C-c @ C-f (outline-forward-same-level) move point to the next visible heading line of the same level

  • C-c @ C-b (outline-backward-same-level) move point to the previous visible heading line of the same level

  • C-c @ C-u (outline-up-heading) move point up to a lower-level visible heading line

26.2 Visibility commands

  • C-c @ C-a (outline-show-all) expand all (bufferwise)

  • C-c @ C-q (outline-hide-sublevels) collapse all (bufferwise) sublevels

  • C-c @ C-d (outline-hide-subtree) hide everything under the current heading except the heading itself

  • C-c @ C-s (outline-show-subtree) show everything under the current heading (body, subheadings and their bodies)

  • C-c @ C-l (outline-hide-leaves) hide the body of the current heading line and bodies of its subheadings

  • C-c @ C-k (outline-show-branches) show all subheadings, at all levels, of the current line

27 Performance profiling

  • See info:elisp#Profiling

  • M-x profiler-start to begin profiling

  • Then run the code you’d like to speed up.

  • M-x profiler-report to display a summary buffer

  • M-x profiler-stop after finishing the profiling.

29 Region/Mark

  • C-<SPC> (set-mark-command) Set the mark at point, and activate it.

  • C-x C-x (exchange-point-and-mark) Set the mark at point, and activate it; then move point where the mark used to be.

30 Registers

  • M-x view-register RET r display a description of what register r contains

  • Saving Positions

    • C-x r SPC r (point-to-register) record the position of point and the current buffer in register r

    • C-x r j r (jump-to-register) jump to the position and buffer saved in register r

  • Saving text

    • C-x r s r (copy-to-register) copy region into register r

    • C-x r i r (insert-register) insert text from register r

    • M-x append-to-register RET r append region to text in register r

    • C-x r + (increment-register) if the register r contains a text, append region to text in register r. If the register contains a number, increase it by 1

    • M-x prepend-to-register RET r prepend region to text in register r

  • Saving numbers

    • C-u number C-x r n r (number-to-register) store number into register r

    • C-u number C-x r + r if the register r contains a number, increse that number by number

31 Select

  • C-x h (mark-whoe-buffer) put point at beginning and mark at end of buffer (select all)

32 Sessions

  • See info > (emacs)Top > Saving Emacs Sessions for detail

  • if the variable desktop-save-mode is t,

    • Emacs looks, when it started, for a saved desktop in the current directory or in the location specified by desktop-path if specified by command desktop-change-dir

    • Emacs, by default, auto-saves the desktop whenever any of it changes. desktop-auto-save-timeout determines how frequently Emacs checks for modifications

  • M-x desktop-save Manually save the desktop

  • M-x desktop-clear empty the Emacs desktop by killing all buffers and clears the global variables listed in desktop-clear-preserve-buffers-regexp

  • if Emacs crashes, the lock stays on the dektop file and Emacs will ask next time whether use the locked desktop file or not. the variable desktop-load-locked-desktop valued nil will load the locked desktop file without asking

33 Symbol

  • C-q C-<KEY> Enter ^<KEY> symbol, eg. ^A for CTRL-A key combination

34 Timestamp

  • C-u M-! date insert timestamp manually.

  • Placing the line Time-stamp: " " or Time-stamp: "< >" within the first eight lines will make Emacs automatically put date and time on that line. The keyword Time-stamp can be customizing variables time-stamp-start, time-stamp-end as the following, as an example.

    # -*- time-stamp-start: "DATE:\\s-"; time-stamp-end: "$"; time-stamp-format: "%:b %02d, %:y" -*-
    #+DATE:
    

    This will automatically update the timestamp on DATE whenever saving any change. The function time-stamp-toggle-active will switch time-stamping on/off, the option time-stamp-active will set the mode on/off.

35 Variables

  • See info:emacs#File Variables

    • # -*- coding: utf-8 -*-

    • local variable example

    • Per-Directory Local Variables

    • specified by the file named .dir-locals.el in the directory

    • See info:emacs#Directory Variables

    • Example

      ((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"))))))
      
  • Check the variable safe-local-variable-values (in .emacs) to check the list of “safe” local variables.

36 Visual line mode

  • wrap words when line breaking (soft lines wrap)

    • lines wrapping breaks long line that does not fit the screen horizontally

    • hard lines wrapping is direct wrapping that may break a line in the middle of a word

  • compare: visual line is like a logical page for PDF documents - they are not real (‘physical’) lines but just visual (‘logical’)

    • move around or edit lines by visual line

  • Adaptive-Wrap-Prefix makes visual line mode respect listing item levels with proper indentation

37 windows

  • C-x o (other-window) Select another window.

  • C-M-v (scroll-other-window) Scroll the next window.

  • C-x 0 (delete-window) Delete the selected window.

  • C-x 1 (delete-other-windows) Delete all windows in the selected frame except the selected window.

  • C-x 4 0 (kill-buffer-and-window) Delete the selected window and kill the buffer that was showing in it. The last character in this key sequence is a zero.

  • C-x ^ (enlarge-window) Make selected window taller.

  • C-x } (enlarge-window-horizontally) Make selected window wider.

  • C-x { (shrink-window-horizontally) Make selected window narrower.

  • C-x - (shrink-window-if-larger-than-buffer) Shrink this window if its buffer doesn’t need so many lines.

  • C-x + (balance-windows) Make all windows the same height.

37.1 transpose windows

38 YASnippet

  • C-c & C-n (yas-new-snippet) Creates a new buffer with a template for making a new snippet. The buffer is in snippet-mode (see below). When you are done editing the new snippet, use C-c C-c to save it.

  • C-c & C-v (yas-visit-snippet-file)

  • C-c & C-s (yas-insert-snippet)

  • yas-expand-from-trigger-key is the main function—literally, it expand a snippet from the inserted trigger key. yas-expand is an alias.

  • yas-expand is an alias defined as

    (defalias 'yas-expand 'yas-expand-from-trigger-key)
    

38.1 variables

  • yas-global-mode Non-nil if Yas-Global mode is enabled

  • yas-snippet-dirs list of top-level snippet directories

  • yas-triggers-in-field If non-nil, allow stacked expansions (snippets inside snippets). Otherwise ‘yas-next-field-or-maybe-expand’ just moves on to the next field

  • yas-maybe-expand contains a special value which, when bound in a keymap, tells Emacs to call yas-expand if and only if there is a snippet abbrev before point. If there is no snippet to expand, Emacs will behave as if yas-expand is unbound and so will run whatever command is bound to that key normally.

38.2 Suppressing warnings

When we use `(elisp function)` inside of a snippet to automatically or interactively alter the content of the snippet, while it is working fine, we get the following message:

Warning (yasnippet): ‘<key>’ modified buffer in a backquote expression.
To hide this warning, add (yasnippet backquote-change) to ‘warning-suppress-types’.

But doing so through the Emacs customization didn’t work. Instead, it worked as expected after the following:

(require 'warnings)
(add-to-list 'warning-suppress-types '(yasnippet backquote-change))