Emacs cheat sheet

This is my ~/notes/emacs file, I usually have it in a buffer so I can quickly search it…


debug start on osx:
open -a /Applications/Emacs.app --args --debug-init

save all
C-x s

C-f	Forward one character
C-n	Next line

C-b	Back one character
C-p	Previous line

Here are some ways to move around in larger increments:
C-a	Beginning of line
M-f	Forward one WORD
M-a	Previous sentence
M-v	Previous screen
M-< Beginning of buffer

C-e	End of line
M-b	Back one word
M-e	Next sentence
C-v	Next screen
M->	End of buffer

C-u 3 C-p	Back 3 lines
C-u 10 C-f	Forward 10 characters
M-1 M-0 C-f	Forward 10 characters
C-u C-n	Forward 4 lines
C-u C-u C-n	Forward 16 lines
C-u C-u C-u C-n	Forward 64 lines

M-g g	Jump to specified line, goto line, go to line
C-s	Incremental search forward
C-r	Incremental search backward

C-SPC	Set mark to the current location
C-x C-x	Swap point and mark

You can set mark explicitly, but certain commands set mark for you,
providing you with convenient footholds as you move around your
buffer:

Type C-SPC	your current location
Jump to either end of the buffer (M-< or M->)

C-u C-SPC	Cycle through mark ring

C-x h	Make region contain the entire buffer ("Select all")
M-h	Make region contain the current paragraph

C-x n n	Narrow buffer to the current region
C-x n w	Restore ("widen") buffer

C-k	Kill line
C-u 10 C-k	Kill 10 lines

C-w	Kill region ("cut")
M-w	Save region to kill ring without deleting ("copy")

M-d	Kill next word
M-k	Kill to end of sentence

C-y	Yanks last killed text
M-y	Replace yanked text with previously killed text

C-/	Undo

C-_	Undo
C-x u	Undo

C-s	Incremental search

C-s C-s	Search for most recently searched item
C-s M-p	Previous item in search history
C-s M-n	Next item in search history
C-h k C-s	Guide to more commands available in incremental search

C-r	Backward incremental search

M-%	Query replace or
M-x query-replace or
M-x regexp-replace
Type y to replace the current match.
Type n to skip to the next match without replacing.
Type q to exit without doing any more replacements.
Type . to replace this match, then exit.
Type ! to replace all remaining matches with no more questions.

C-M-s	Regular expression incremental search

When you type this
in a replacement string:	Emacs replaces it with:
\&		 		the original found text
\1, \2, etc.			the 1st, 2nd, etc. parenthesized subgroup in the found text
\#				the number of replacements done so far
\?				a string obtained by prompting the user on each match
\,(lisp-expression …)	the result of evaluating an arbitrary function

C-h k shows doc for command k
C-h f shows doc for particular command

C-h a search for commands by keywords

M-x shell
M-x compile
M-x gdb
M-x grep
M-x man
M-x shell-command or M-!
M-x shell-command-on-region or M-|


C-u 10 C-f	Forward 10 characters
C-u M-a	Backward 4 sentences

M-x shell	Create or switch to shell buffer named *shell*
C-u M-x shell	Create or switch to shell buffer with specified name

M-x java-mode	Mode for editing Java files
M-x python-mode	Mode for editing Python files
M-x text-mode	Mode for editing text files
M-x fundamental-mode	Mode with no specializations at all

2nd shell:

	Mx rename-buffer

	C-u Alt-x shell

M-! tail -f  &

Switching between windows:
M-left, M-Right, M-Up, M-Down does what you want it to!


server:
	start emacs as $ emacs --daemon
	connect with   $ emacsclient -c
	kill server    $ save-buffers-kill-emacs or kill-emacs

[un]comment-region

M-x eval-buffer ; have .emacs in buffer and do this to eval it

M-x revert-buffer ; reverts all unsaved changes
M-x replace-string  string  newstring  ; unconditional replace

You can use M-x check-parens to find any unbalanced parentheses and
unbalanced string quotes in the buffer.

Saving positions in a register;
C-x r  a	 ; save point in register a
C-x r j a        ; jump to point a

M-x dired, then C-x C-q ; switches to Editable dired mode, rename files/dirs

M-| # run a shell command on a selected region

Info mode:
Enter  or click to visit a link.
u for going to the parent node.
n for next node
p for previous node
l (lower case L) for the last node you visited (like the Back button in browsers).
Tab  to move cursor to the next link.
q to quit info.

Dired mode
C-x d
Enter	open
q	close
C	copy file
R	rename
D	delete
+	mkdir
Z	gzip
!	shell command
S	symlink
H	hardlink
P	print
B	byte compile
L	load
T	mod timestamp
M	x mode
G	x group
O	x owner
A	search files
Q	query replace in files
C-t d	display thumbnails

Mark/Unmark files to operate on;
m	    mark
u	    unmark
U	    unmark all
%m	    mark by regex
then do a ! to operate on em

Other Dired stuff;
g     refresh
^     parent

Manually rename multiple files
M-x wdired-change-to-wdired-mode, then do the renaming and C-c C-c to execute it

query-replace-regex on file names in wdired mode then
M-x query-replace[-regex] - can't get that to prepend so this works instead;

Dired mode: mark files with m or *s for all, then %R for interactive regex replace

M-r ;; move cursor to middle/top/bottom of window

Macros
C-x (	;; start recording
C-x )   ;; stop
C-x e	;; execute
M-x kmacro-edit-macro	;; edit last defined macro

Name last macro
M-x mkacro-name-last-macro

Insert named macro into buffer
M-x insert-kbd-macro RET macroname RET
# save it to .emacs.d/macros

F3	Start recording macro
F4	Stop recording macro
F4	Play back macro once
M-5 F4	Play back macro 5 times
M-0 F4	Play back macro over and over until it fails


M-t 	;; swap words
M-q	;; fill-paragraph

C-x C-o	;; delete blank lines nearby, leave only 1 or none

C-x * c	;; Calculatore, or M-x calc
C-x * q ;;    "      quick mode
C-x * e ;;    "	     embedded mode

Copy/paste from/to named region
C-x r s A    Copy the region into register A (copy-to-register).
C-x r i A    Insert the region stored in register A (if it contains a rectangle) (insert-register).

C-x i 	;; insert file at point
C-x v	;; find alternate file
C-x s	;; save buffer
C-x f	;; find file
C-x w	;; write file (save as)
C-x f	;; describe function
C-x k	;; describe function bound to key

M-x refill-mode	    ;; keeps fill formatted while typing
M-x auto-fill-mode  ;; doesn't reformat when re-editing, must use
M-q 		    ;; to reformat after editting (fill-paragraph)
M-x fill-region	    ;;

C-h b		    ;; show all keybindings

C- Candidates for repurposing;
C-j	      newline-and-indent (nearly same as return)
C-o	      open-line (equiv to C-a & RET)
C-q	      quoted-insert ??

C-q C-j ;; insert newline in mini buffer

full path of file name
> M-: (buffer-file-name)

M-0 M-r for the top of the screen
M-- M-r for the bottom of the screen.

swap 2 windows
‘C-x 0 C-x 4 b’

kill to end of word
M-d

Join line up
M-^

tramp

	C-x C-f /remotehost:

	C-x C-f /ssh:user@remotehost:filename

	C-x C-f /ssh:you@remotehost|sudo:remotehost:/path/to/file RET

	C-x C-f /su::/etc/hosts RET


split 3 equally "M-x split-3.."


Open all files in a dir recursively
C-u C-x d
add R to the -al default
-alR
then %m to mark files matching pattern
then M-x dired-find-marked-files



Finding Files
C-x C-f: The standard way, I use the ffap bindings
;;; find file at point
(require 'ffap)
;; rebind C-x C-f and others to the ffap bindings (see variable ffap-bindings)
(ffap-bindings)
;; C-u C-x C-f finds the file at point
(setq ffap-require-prefix t)
;; browse urls at point via w3m
(setq ffap-url-fetcher 'w3m-browse-url)

C-u C-x C-f: Find the file at point (via the ffap package), relative filenames work also

http://www.xsteve.at/prg/emacs/power-user-tips.html
https://www.gnu.org/software/emacs/manual/html_node/emacs/FFAP.html


C-H i, built in manuals

select elisp code in a buffer
M-x eval-region ; eval the region
M-x eval-buffer ; whole buffer



Magit
Getting started with Magit is really easy:
M-x magit-status to see git status, and in the status buffer:
s to stage files
c c to commit (type the message then C-c C-c to actually commit)
b b to switch to another branch
Other:
P u to do a git push
F u to do a git pull
try to press TAB


flycheck
C-c ! l - see full list of errors in a buffer. Details
C-c ! n - next error
C-c ! p - previous error


Retrieve a list of custom keybindings specified in your .emacs file
describe-personal-keybindings

Now using package and use-package to manage packages

tips: https://github.com/emacs-tw/awesome-emacs

;; Bring up Dash docs at point
(global-set-key "\C-cd" 'dash-at-point)

display a var
C-h v var

;; selective display aka hide columns past 
C-u  C-x $

describe-key → Find the command name of a keyboard shortcut.
describe-function → Find the keyboard shortcut of a command.
apropos-command → Search command by name.
describe-mode → Show current mode's documentation.

Type 【Ctrl+s Ctrl+w】 to search the word under the cursor. You can type 【Ctrl+w】 multiple times to expand the word selection.

list-matching-lines
delete-matching-lines
delete-non-matching-lines
delete-duplicate-lines (Emacs 24.4)


highlight-phrase
highlight-regexp
highlight-lines-matching-regexp


Advantages of Eshell Over a Regular Shell

Platform agnostic - it works the same on unix based OSes and Windows

tramp integration - if you start an eshell while default-directory is pointing at a buffer that is served over tramp, then eshell will use the same connection, and allow manipulation of the remote filesystem without using dired.

find-file … It's just so much easier being able to type 'find-file blah' as a command it open blah in an emacs buffer (even over tramp, see above)

consistency - eshell uses its own method of colouring the output of ls and thus is consistent across platforms and shell configurations, which can be important if you ever have to use it on ac

counts that you don't have authority to modify .bashrc/.profile on.
/dev/clip and /dev/kill - you can redirect shell commands to the emacs clipboard and killring

redirect to buffers - along with the above, you can also do things like ls >> # to output the result from ls to the scratch buffer


back-to-indentation = first non-whitespace char  
M-m

delete all spaces/tabs around point
M-\


C-h c describe-key-briefly
C-h k describe-key
C-h l view-lossage
C-h w where-is
C-h f describe-function
C-h v describe-variable
C-h m describe-mode
C-h b describe-bindings
C-h t help-with-tutorial
C-h i info


Replace in multiple files
-------------------------
M-x find-name-dired: you will be prompted for a root directory and a filename pattern.
Press t to “toggle mark” for all files found.
Press Q for “Query-Replace in Files…”: you will be prompted for query/substitution regexps.
Proceed as with query-replace-regexp: SPACE to replace and move to next match, n to skip a match, etc.
Press Y to finish replacing in all buffers.
C-x C-s ! to save all buffers.


source code indexing:
GNU Global, gtags, ggtags package, use gctags wrapper around gtags command (in ~/scripts)
Doesn't do html tho, haven't found anything that does.