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 literalC-cto the sub-shellC-c <char>is equivalent toC-x <char>in normal Emacs modes, e.g.C-c oinvokes the global binding ofC-x owhich 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 themoreprogram.When the bash complains “cannot read termcap database”, then create a symlink
~/.terminfo/65 -> /usr/local/share/emacs/26.3/etc/ethat 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 beebut in this macos it has to be65. Check this byls /usr/share/terminfoand thensudo find /usr/share/terminfo -name "eterm*".
2 (Auto)reverting¶
3 \*buffers*¶
C-x C-bhelm-buffersM-SPCorC-SPCmarkC-x >andC-x <horizontal scrollC-x C-ssave buffer without leaving HelmC-x ssave all unsaved buffersC-c ddelete buffer without leaving HelmM-Ddelete marked buffers and leave HelmC-x kis meant to kill buffers but cannot kill multiple buffers selected byC-SPCwithRET.
4 \*kill-buffer*¶
This seems only useful when killing the currently opened buffer
C-x kopen kill-buffer
5 Abbrevs mode¶
abbrev-modestart the (minor) modeC-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 abbrevlist-abbrevsDisplay a list of defined abbrevsedit-abbrevsedit 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-nameThe 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 pointC-x r l(list-bookmarks) list all bookmarksC-x r b(bookmark-jump/helm-bookmarks/helm-filtered-bookmarks) then type a bookmark name. You can type\*for wildcardbookmark-savesave all the current bookmark values in the default bookmark file\~/.emacs.d/bookmarks. Can be modified by setting the variablebookmark-default-filewhen in the bookmark file:
dmark the current item for removexremove all marked onesrrename current item’s titlessave 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-dDelete the character before/after pointM-d~/~M-<Del>Kill forward/back to the end/beginning of the next/previous wordC-k~/~M-kKill to the end of the line/sentenceC-oInsert a blank line after the cursorC-S-<DEL>(kill-whole-line) Kill a whole lineC-u 50orM-5 0 <moving_command>repeat the moving command by 50 times. The tutorial saysC-uis better `because it works on any terminal’. `50’ is called a “prefix argument”. Exception:C-u 8 C-v~/~C-u 8 M-vInstead 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-wKill the selected regionC-x <TAB>Indent the selected regionM-$Check the spelling of wordsC-/,C-_,C-x uundo. On some text terminals, typingC-/actually sendsC-_to Emacs.C-gchanges the `direction’. So, after undoingC-/n-times, doingC-g C-/and repeatingC-/(n-1)-times will do redo all.the auto saved file’s name for “
hello.c” is “#hello.c#”. To recover auto-saved editing, runM-x recover-this-file <RET>
10 Dired¶
C-x dinvoke Dired,C-x 5 d(dired-other-frame)gre-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-cto apply the changes, orC-c <ESC>(orC-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 editTARGET. 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 inDIRthat contain matches forREGEXPand start Dired on output.
10.1 directories¶
+(dired-create-directory) create a directoryC-M-n(dired-next-subdir) go to next subdirectory header line regardless of levelC-M-p(dired-prev-subdir) go to previous subdirectory header line regardless of levelC-M-u(dired-tree-up) go up to the parent directory’s header lineC-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 linea(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 deletionu(dired-unmark) remove the deletion flag
10.3 files¶
x(dired-do-flagged-delete) delete files flagged for deletionto create a new file in the directory, just use
C-x C-fas usualf,e,<ret>(dired-find-file) visit the filev(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;NEWis the directory to copy into ir the new nameD(dired-do-delete) delete the specified or marked filesx(dired-do-flagged-delete) delete all flagged filesS 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¶
mmark with ‘*’u(dired-unmark) remove any markU(dired-unmark-all-marks) remove all marks from all files in the bufferM-}(dired-next-marked-file) move down to the next marked fileM-{(dired-prev-marked-file) move up to the previous marked fileC-_(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-ssave 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-0reset to the default font size
15 frames¶
C-x 5 2(make-frame-command) create a new frameC-x 5 0(delete-frame) delete the selected frameC-x 5 1delete all frames except the selected oneC-x 5 ocycle and select other frames
16 helm¶
RETexecutes the default action upon exiting the Helm sessionC-jexecutes the default action but without exiting the Helm sessionTABdisplays a list of actions availableC-@mark/unmark a candidateM-Uunmark all visible marked candidates at once
17 help and info¶
C-h i(info) run Info, the GNU documentation browserC-h b(describe-bindings) display all active key bindingsC-h c KEY(describe-key-briefly) display the name of the command thatKEYis bound toC-h k KEY(describe-key) display the name and documentation of the command thatKEYrunsto open separate buffer for
info, doM-2 M-x info,M-3 M-x info, etc.C-hhelp aproposC-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>orM-x <keyword>(command apropos) list all the commands whose names contain that keyword. Eg.C-h a file<RET>displays a list of allM-xcommands 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-modeC-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-blockSelect 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.iInsert 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-fmove back/forward by one characterC-p~/~C-nmove up/down by one line
25.2 by words¶
M-b~/~M-fmove back/forward by one word
25.3 horizontally¶
C-a~/~C-emove to the beginning/end of the lineM-m(back-to-indentation) Jump to first non-whitespace character of the line
25.4 by sentence¶
M-a~/~M-emove to the beginning/end of the sentence
25.5 by paragraph¶
M-{(backward-paragraph) Move back to previous paragraph beginningM-}(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-vM-vpage down/up by one pageC-M-vscroll the other windowC-lClear 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 pointC-x C-xSet the mark at point, activate, then move back to the original point
25.10 GO TO¶
M-g ggo to the line
26 Outline minor mode¶
A heading line together with all following body lines is called an entry.
outline-hide-entryandoutline-show-entryonly applies to the body.The body as in
outline-hide-bodyrefers to all body lines in the buffer.All as in
outline-show-allliterally 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 lineC-c @ C-p(outline-previous-visible-heading) move point to the previous visible heading lineC-c @ C-f(outline-forward-same-level) move point to the next visible heading line of the same levelC-c @ C-b(outline-backward-same-level) move point to the previous visible heading line of the same levelC-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) sublevelsC-c @ C-d(outline-hide-subtree) hide everything under the current heading except the heading itselfC-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 subheadingsC-c @ C-k(outline-show-branches) show all subheadings, at all levels, of the current line
27 Performance profiling¶
M-x profiler-startto begin profilingThen run the code you’d like to speed up.
M-x profiler-reportto display a summary bufferM-x profiler-stopafter finishing the profiling.
28 Reference sites¶
http://pragmaticemacs.com/emacs/org-mode-basics-structuring-your-notes/
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 rdisplay a description of what registerrcontainsSaving Positions
C-x r SPC r(point-to-register) record the position of point and the current buffer in registerrC-x r j r(jump-to-register) jump to the position and buffer saved in registerr
Saving text
C-x r s r(copy-to-register) copy region into registerrC-x r i r(insert-register) insert text from registerrM-x append-to-register RET rappend region to text in registerrC-x r +(increment-register) if the registerrcontains a text, append region to text in registerr. If the register contains a number, increase it by 1M-x prepend-to-register RET rprepend region to text in registerr
Saving numbers
C-u number C-x r n r(number-to-register) storenumberinto registerrC-u number C-x r + rif the registerrcontains a number, increse that number bynumber
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-modeist,Emacs looks, when it started, for a saved desktop in the current directory or in the location specified by
desktop-pathif specified by commanddesktop-change-dirEmacs, by default, auto-saves the desktop whenever any of it changes.
desktop-auto-save-timeoutdetermines how frequently Emacs checks for modifications
M-x desktop-saveManually save the desktopM-x desktop-clearempty the Emacs desktop by killing all buffers and clears the global variables listed indesktop-clear-preserve-buffers-regexpif 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-desktopvaluednilwill load the locked desktop file without asking
33 Symbol¶
C-q C-<KEY>Enter^<KEY>symbol, eg.^AforCTRL-Akey combination
34 Timestamp¶
C-u M-! dateinsert timestamp manually.Placing the line
Time-stamp: " "orTime-stamp: "< >"within the first eight lines will make Emacs automatically put date and time on that line. The keywordTime-stampcan be customizing variablestime-stamp-start,time-stamp-endas 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
DATEwhenever saving any change. The functiontime-stamp-toggle-activewill switch time-stamping on/off, the optiontime-stamp-activewill set the mode on/off.
35 Variables¶
-
# -*- coding: utf-8 -*-local variable example
Per-Directory Local Variables
specified by the file named
.dir-locals.elin the directoryExample
((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-Prefixmakes 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¶
top and bottom windows open and want to switch their positions:
C-x 0 C-x 4 bbuttom window active and moving it to the top window:
C-x 0 C-x 2 C-x b RET
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, useC-c C-cto save it.C-c & C-v(yas-visit-snippet-file)C-c & C-s(yas-insert-snippet)yas-expand-from-trigger-keyis the main function—literally, it expand a snippet from the inserted trigger key.yas-expandis an alias.yas-expandis an alias defined as(defalias 'yas-expand 'yas-expand-from-trigger-key)
38.1 variables¶
yas-global-modeNon-nil if Yas-Global mode is enabledyas-snippet-dirslist of top-level snippet directoriesyas-triggers-in-fieldIf non-nil, allow stacked expansions (snippets inside snippets). Otherwise ‘yas-next-field-or-maybe-expand’ just moves on to the next fieldyas-maybe-expandcontains a special value which, when bound in a keymap, tells Emacs to callyas-expandif 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))