emacs Cheat Sheet

Oft used emacs commands

Terminology in this sheet
C-x   = Control key + X key
M-x   = Meta key + X key (or Alt X)
[RET] = Enter key   [DEL] = Del key   [SPC] = Space bar
[UA] = Up arrow     [DA] = Down arrow
[Name]   = A name of your choosing
[Letter] = A letter of your choosing

Cancel out of any command with C-g

Prefix command with C-u to do it for times.
For example, to move down 4 lines: C-u C-n

Buffers & Panes
C-x C-b  - Get to the buffer list
  Mark several buffers to save or delete using 'd' or 's'. Execute by 'x'
C-x b [Name]  - Open a new buffer with this name
                Very useful to open a temporary (scratch) buffer
C-x b [RET]   - Switches to the previously visited buffer
C-x b [UA] / [DA]  - Scrolls through previous buffers, select by [RET]
C-x o    - Switch to other pane
C-x 1    - Switch back to one panel
           [will make the panel you're currently in to the full one]
C-x 2    - Split current panel into two panels, stacked vertically
C-x 3    - Split current panel into two panels, stacked horizontally 
C-x 0    - Remove current panel 
C-x C-s  - Saves only the buffer you're editing
C-x s    - Save all open buffers that have changes 
             (choose y or n for each file, or ! for all)
C-x C-w  - Save as
C-M \    - Indent a marked region
C-x C-v [RET]  - reload / refresh (visit) a file that is currently open.
M-x mouse-wheel-mode   Enable scrolling with the wheel. Can also set
                       this to start up

Window size manipulation
C-x ^   grow active window by 1 line
C-x }   widen active window right
C-x {   narrow active window
C-x +   resize allow windows to equal size
CM  v   scrolls text in the next (non active) window

Text Manipulation
C-t      Switches two letters
M-t      Switches two words
C-x C-t  Switches two lines (usu. the current and line above)
C-o      Inserts a new line below the point without moving the point.
M-l      Makes following word (to right) all lower case.
M-u      Makes following word (to right) all upper case.

Tab Mode
M-x edit-tab-stops   Allows for editing of tab stop positions (Text mode only)
M-x text-mode        Change to text modes
indent-tabs-mod = nil  Sets tab usage to spaces only
M-i                    Works like tab when (also works when not in text mode)

Store a position in a Register:
C-x r [SPC] [Letter]     Mark a spot with Regeister Letter [Letter]
    ex:  C-x r [SPC] R   Marks a spot, stored in register R

C-x r j [Letter]         Jump to a spot stored in Register Letter [Letter]
    ex:  C-x r j R       Jumps to spot stored in register R

Scroll, Moving
C-l      Moves the line at cursor to the middle (vert) of the screen
M-g M-g  Goto line #   (or: M-x goto-line)
M-<      Goto beginning of entire buffer
M->      Goto end of entire buffer
C-[ p    Go up to the previous open parenthesis, ie, the previous "(" character
         (will go to top of document if no open paren is present).
C-[ n    Go down to next closed paren

M-\      Deletes all space around the cursor (both sides).
M-[SPC]  Deletes all spaces around cursor (both sides). Leaves 1 space on LHS.
M-d      Deletes word going forward (right)
M-[DEL]  Deletes word going backward (left)
C-[DEL]  Deletes word going backward (left) (any difference to M-[DEL]??)

C-[SPC]    Starts mark at current position.
C-M-[SPC]  Starts mark at end of next LHS word. Extends mark to current pos.
C-x C-x  Switches your current point and the Mark point. So you can use it
         to nav back to a point where you were (and toggle quickly back with
         the same command). The mark point is switched and can then be used.
C-x h    Mark entire buffer
M-h      Mark current paragrph
C-x r k  Start with a normal mark position, and then use C-x r k 
         to kill a rectangular region from the original mark to your position.

Kill Ring
C-y  Yank the last entry
M-y  Yank an earlier kill. Must be preceeded with C-y. May be continue
     calling and cycling back.

M-x comment-region     -  Block comments a region that is highlighted
M-x uncomment-region   -  Block uncomments a region that is highlighted

Directory Listing (Dired)
C-x d       Enter dired  (or M-x dired)
C-x C-d     Lists current directory contents in (an)other pane
M-x shell   Starts a shell session in current pane (or switches 
              pane to shell buffer if it currently exists)

 within shell mode, use M-p to get up to the last command (rather
 than the arrow or C-p, which actually moves the pointer up)

C-u M-x shell    To get a 2nd shell working

Dired commands
d  to mark files for deletion
x  to complete file deletion
g  to update buffer contents (refresh)
! command    issues "command" in this directory
             (this can work on multiple files that are marked)

C-M-s   or  C-s M-r   Regex search
M-x replace-regex     Non-interactive replace. Replaces all from point to 
                      end of document.

If you want to replace something with a carriage return, use C-Q C-J.
For example, with Query-replace (M-%), then type into text to be searched,
 and for the text to be replace, use C-Q C-J, once for each CR desired.

Shell Scripts
C-c C-x [RET]   - executes the shell script you are currently editing

Work on remote files.  
Anywhere you want to load or work on a remote
file (via ssh, for example), you can prefix the file with:


Load up a file with (as usua) with C-x C-f, and use the syntax above.
Or diff local and remote files or dirs (see "M-x ediff-directories" below).

+ or =      Expands directory you're on
-           Contract
U           Up a directory
b           Switch to Quick Buffers mode, for selecting buffers
f           Switch to File mode, for selecting files
e           edit / open the file (or directory) you're currently on

Installing .el files
1) Create ~/elisp directory
2) Ensure that the elisp dir is present in Emacs's load-path  variable. 
   This is done by adding something like this to your ~/.emacs file:
     (add-to-list 'load-path "~/elisp")
3) Copy .el file into elisp directory
4) Add the following line to .emacs file

     (require 'theFile)  

   Where the file is called theFile.el  (leave off .el in require statement,
   and only use the first ' as indicated.)

Steps 1&2 only have to be done once.  From there on, just do step 3&4.

Program (modes) within emacs
M-! shellCommand  - runs a shellCommand 
M-x shell
M-x dired
M-x calc          - calculator
M-x calendar      - calendar
M-x diff               -  Run a diff of two files
M-x ediff              -  Runs an interactive ediff of two files
M-x ediff-buffers      -  Runs an interactive ediff of two buffers
M-x ediff-directories  -  ediffs two directories
  When differencing, use the filter to eliminate directories you
  don't want to view. This is a regex.  So, for example, to filter
  out the .svn directories (since we likely don't want to diff those),
  type:   [^.svn]   into the filter.
M-x mc                 - run the MC emulator
M-x gdb                - runs gdb
M-x grep			   - runs grep		
  M-x grep  [return]
  then enter the grep string followed by * to search all files.
  Searches CWD.
M-x find-grep-dired    - finds all files with name or containing string.
                         Dumps results into dired.
M-x man                - runs a man page (convenient for search / copy)
M-x speedbar           - runs the speedbar
M-x ispell             - Starts ispell checker on document / buffer from
M-x ispell-word        - Starts ispell checker on current word
© 2019  Give Me Fish, LLC