Skip to content

Instantly share code, notes, and snippets.

@nloyola
Last active July 6, 2021 07:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nloyola/5f1b8637b231ecafc4c7 to your computer and use it in GitHub Desktop.
Save nloyola/5f1b8637b231ecafc4c7 to your computer and use it in GitHub Desktop.
Emacs notes

Emacs Notes

Highlights from Emacs manual.

General

C-x C-x
Set the mark at point, and activate it; then move point where the mark used to be (exchange-point-and-mark).
C-SPC C-SPC
Instead of setting the mark in order to operate on a region, you can also use it to remember a position in the buffer (by typing C-SPC C-SPC), and later jump back there (by typing C-u C-SPC). See Section 8.4 [Mark Ring], page 51, for details
C-x h
Move point to the beginning of the buffer, and set mark at the end (mark-whole-buffer).
C-x TAB or C-M-\
indent
M-$
Check the spelling of words within
M-^
Join two lines by deleting the intervening newline, along with any indentation following it (delete-indentation).
C-S-backspace
Kill an entire line at once (kill-whole-line)
M-x append-to-buffer
To accumulate text into a buffer, use M-x append-to-buffer. This reads a buffer name, then inserts a copy of the region into the buffer specified.
C-x r t string RET
Replace rectangle contents with string on each line (string-rectangle).
M-x string-insert-rectangle RET string RET
Insert string on each line of the rectangle.
C-x r SPC r
Record the position of point and the current buffer in register r (point-to-register).
C-x r j r
Jump to the position and buffer saved in register r (jump-to-register).
M-x append-to-register RET r
Append region to text in register r.
C-u number C-x r n r
Store number into register r (number-to-register).
C-u number C-x r +r
Increment the number in register r by number (increment-register).
C-x r l
List all bookmarks (list-bookmarks).
C-x C-+
To increase the height of the default face in the current buffer, type C-x C-+ or C-x C-=. To decrease it, type C-x C--.
C-x w h regexp RET face RET
Highlight text that matches regexp using face face (highlight-regexp)
M-x whitespace-mode
show whitespace.
C-x $
To hide lines in the current buffer, type C-x $ (set-selective-display) with a numeric argument. Then lines with at least n columns of indentation disappear from the screen.
C-j
Use C-j to search for a newline character.
M-x keep-lines
Prompt for a regexp, and delete each line that does not contain a match for it, operating on the text after point
M-x flyspell-prog-mode
Enable Flyspell mode for comments and strings only
M-x set-visited-file-name
Change the file name under which the current buffer will be saved.
C-TAB
You can use the file name cache to make it easy to locate a file by name, without having to remember exactly where it is located. When typing a file name in the minibuffer, C-TAB (file-cache-minibuffer-complete) completes it using the file name cache.
C-x LEFT
Select the previous buffer in the buffer list (previous-buffer).
C-x RIGHT
Select the next buffer in the buffer list (next-buffer).
C-x C-q
Toggle read-only status of buffer (toggle-read-only).
M-x rename-buffer RET name RET
Change the name of the current buffer.
M-x kill-matching-buffers
Offer to kill all buffers matching a regular expression.
C-x 4 b bufname RET
Select buffer bufname in another window (switch-to-buffer-other-window).
C-x +
Make all windows the same height (balance-windows).
C-c left
(winner-undo) undoes the last window configuration change.
M-l
Convert following word to lower case (downcase-word).
M-u
Convert following word to upper case (upcase-word).

Ispell

r new RET
Replace the word, just this time, with new. (The replacement string will be rescanned for more spelling errors.)
R new RET
Replace the word with new, and do a query-replace so you can replace it elsewhere in the buffer if you wish.
A :: Accept the incorrect word
treat it as correct, but only in this editing session and for this buffer.

Macros

C-x q
When this point is reached during macro execution, ask for confirmation (kbd-macro-query).
M-x insert-kbd-macro
Insert in the buffer a keyboard macro’s definition, as Lisp code.
C-x C-k C-e
Edit the last defined keyboard macro (kmacro-edit-macro).

Winner Mode

  • Winner Mode is a global minor mode. When activated, it allows to “undo” (and “redo”) changes in the window configuration with the key commands C-c left and C-c right

Helm

  • To grep files use C-u C-x c M-g s, select root dir for grep, TAB TAB Enter, enter file type, TAB TAB Enter, enter pattern or press C-w to select word at point.
  • to save the results of the grep use C-x C-s in a buffer.
  • To see minibuffer command history: M-x helm-complex-command-history.
  • To see helm find files history use C-c h in the find file buffer.
  • To see the mark ring us M-x helm-all-mark-rings.
  • For a quick calculator M-x helm-calcul-expression.
  • For a history M-x helm-eshell-history.
  • yank current selection C-c C-y.
  • add the current candidate to the kill ring with C-c C-k.
  • You can insert marked candidates into the current buffer with C-c C-i.

Helm Mini

  • Mark files: C-SPC
  • Delete buffer (or marked buffers) without leaving Helm: C-c d
  • Delete marked buffers and leave Helm: M-D
  • Grep buffers: M-g s

Save Split-window Config Across Sessions

Using revive

  • use M-x save-current-configuration to save window configuration.
  • use M-x resume to load window configuration.

Emacs 24.4

C-x TAB
The behavior of C-x TAB (indent-rigidly) has Changed. When invoked without a prefix argument, it now activates a transient mode in which typing <left>, <right>, <S-left>, and <S-right> adjusts the text indentation in the region. Typing any other key resumes normal editing behavior.
C-x SPC
New command C-x SPC (rectangle-mark-mode) makes a rectangular region. Most commands are still unaware of it, but kill/yank do work on the rectangle.
M-s .
New global command M-s . (isearch-forward-symbol-at-point) starts a symbol (identifier) incremental search forward with the symbol found near point added to the search string initially.
M-s . word M-%
replace symbol. Can aslo use C-M-% for regex.
M-x superword-mode
disables subword-mode.
M-x subword-mode
disables superword-mode.

Master Emacs in One Year

Tips

  • Capture shell command output to kill ring: M-:, then in mininbuffer type (kill-new (shell-command-to-string "ls -l ."))

Ivy / Swiper / Counsel

Key bindings for navigation

  • M-< (ivy-beginning-of-buffer) selects the first candidate
  • M-> (ivy-end-of-buffer) selects the last candidate
  • C-v (ivy-scroll-up-command) scrolls up by ivy-height lines
  • M-v (ivy-scroll-down-command) scrolls down by ivy-height lines

Key bindings for single selection, action, then exit minibuffer

C-j (ivy-alt-done)
When completing file names, selects the current directory candidate and starts a new completion session there. Otherwise, it is the same as ivy-done.
C-M-j (ivy-immediate-done)
Exits with the current input instead of the current candidate (like other commands).

This is useful e.g. when you call find-file to create a new file, but the desired name matches an existing file. In that case, using C-j would select that existing file, which isn’t what you want - use this command instead.

Key bindings for multiple selections and actions, keep minibuffer open

C-M-m (ivy-call)
Is the non-exiting version of C-m (ivy-done).

Instead of closing the minibuffer, C-M-m allows selecting another candidate or another action. For example, C-M-m on functions list invokes describe-function. When combined with C-n, function descriptions can be invoked quickly in succession.

C-M-n (ivy-next-line-and-call)
Combines C-n and C-M-m. Applies an action and moves to next line.

Comes in handy when opening multiple files from counsel-find-file, counsel-git-grep, counsel-ag, counsel-rg, or counsel-locate lists. Just hold C-M-n for rapid-fire default action on each successive element of the list.

Saving the current completion session to a buffer

C-c C-o (ivy-occur)
Saves the current candidates to a new buffer and exits completion.

The new buffer is read-only and has a few useful bindings defined.

C-x C-q
Make edits to the buffer. After editing, press C-x C-s to save changes.
RET or j (ivy-occur-press)
Call the current action on the selected candidate.
mouse-1 (ivy-occur-click)
Call the current action on the selected candidate.
j (next-line)
Move to next line.
k (previous-line)
Move to previous line.
a (ivy-occur-read-action)
Read an action and make it current for this buffer.
o (ivy-occur-dispatch)
Read an action and call it on the selected candidate.
q (quit-window)
Bury the current buffer.

Magit

  • M-x magit-log-buffer-file

Tramp

Multi hop with sudo

  • /ssh:you@remotehost|sudo:remotehost:/path/to/file
  • copy link in dired:
    C-0 w
    copies absolute path
    C-u w
    copies the relative path
sort in dired
C-u s
  • -alh sorts by name
  • -alt sorts by date
  • -lhS sorts by size
M-x delete-region
delete a region of text without putting it into the kill-ring.
C-x h C-u M- | dos2unix
remove MS Windows line endings
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment