emacs reference card

Jim Brooks


C-X   means to hold down Control key then press key X
M-X   means to hold down Meta key (usually Alt) then press key X
ESC X means to press and release ESC key first, then key X

Apple notebooks:
Ctrl-Home = fn-ctrl-home[left]
Ctrl-End  = fn-ctrl-end[right]

-------------------------------------------------------------------------------
basic editing
-------------------------------------------------------------------------------

C-g
    Abort command.

C-/ or C-_ or C-\  (C-\ is my custom mapping)
    Undo.

C-Backspace or M-Backspace
    Delete backwards a word.

M-k
    Kill (delete) paragraph.

C-y [M-y...]
    Yank from item in kill ring.  Then M-y cycles thru previous items in kill ring.

M-t
    Transpose word (swap this word and previous word).

C-x C-x
    Exchange cursor and mark.


-------------------------------------------------------------------------------
rectangular editing
-------------------------------------------------------------------------------

C-x r k
    kill-rectangle
C-x r y
    yank-rectangle
C-x r r
    copy-rectangle-to-register


-------------------------------------------------------------------------------
searching, replacing
-------------------------------------------------------------------------------

C-s C-w [C-w...]
    Search for the word that the cursor is on.
    Press C-w repeatedly to pick up parts of a word separated by underscore.
    Then press C-s to start the search.

C-s RETURN C-w
    Do a word seach (ignores line breaks, spaces, and punctuation).

ESC C-s  or  M-C-s
    Regular expression (regex) search forward.

To search or replace in multiple files:

    Using dired, mark all files by:
    | m (XEmacs)
    * s (emacs)
    (Press 'u' over any CVS/RCS subdir to exclude it)

    A    global search        thru marked files
    M-,  continue global search
    Q    global query/replace thru marked files

    Replacement is done by regex and is tricky.
    Eg to change "st.", write "st[\.]".

replace-exp:

    Replace using a regular-expression.
    This is useful to replace strings having constant and variable parts.
    To reference the variable part (the part to replace), use a regex in parentheses.
    That can be refernences in the replacement pattern as \1

    For example:

    in: "/textures/font/masque/per_rgba.webp", // '%' 0x25
    in: "/textures/font/masque/and_rgba.webp", // '&' 0x26
    in: "/textures/font/masque/dqu_rgba.webp", // ''' 0x27

    out: DEF_CHAR( "per" )
    out: DEF_CHAR( "and" )
    out: DEF_CHAR( "dqu" )

    The goal is to replace the variable substrings"per","and","dequ"
    while keeping the constant substrings.

    pattern:    "/textures/font/masque/\(.*\)_rgba.webp"
    replacement: DEF_CHAR( "\1" )

Using grep with emacs:
    M-x grep
    This will open a *grep* buffer with matches similar to *compilation* buffer.


-------------------------------------------------------------------------------
registers
-------------------------------------------------------------------------------

C-x r SPC r 
    Save the location of point in register r (point-to-register). 
C-x r j r 
    Jump to the location saved in register r (register-to-point). 
C-x r s r 
    Copy region into register r (copy-to-register). 
C-x r g r 
    Insert text contents of register r (insert-register). 


-------------------------------------------------------------------------------
bookmarks
-------------------------------------------------------------------------------

http://www.xemacs.org/Documentation/21.5/html/xemacs_13.html

C-x r m RET
    Save bookmark for the visited file.

C-x r m (name) RET
    Save bookmark by name.

C-x r b (name) RET
    Jump to bookmark (if you remember its name).

C-x r l
    List bookmarks.
    To see help in this context: C-h m

    Keys:
    'v' jump to bookmark where cursor is on (view bookmark)

M-x bookmark-save
    Save bookmarks to file (~/emacs.bmk).


-------------------------------------------------------------------------------
frames
-------------------------------------------------------------------------------

C-x 0
    Remove this frame (sort of undo split).

C-x 2
    Split window into two rows.

C-x 3
    Split window into two columns.

C-x n n
    [narrow editing] Narrow view to region.

C-x n w
    [narrow editing] Exit narrow view.


-------------------------------------------------------------------------------
files
-------------------------------------------------------------------------------

C-x C-r
    Open file as read-only.


-------------------------------------------------------------------------------
tags/ctags
-------------------------------------------------------------------------------

Create a "TAGS" file:
    ctags -e --extra=+q `find . -name "*.cc" -or -name "*.hh"`
    ctags -e -R --extra=+q  # Exuberant will recurse into subdirs tagging anything
    ESC ! etags *.cc *.h   # from emacs

ESC x visit-tags-table
    To inform emacs after you've updated the "TAGS" file.

M-.
    Jump to a tag.
    emacs is smart enough to automatically pre-select the tag that
    the cursor is over.  Otherwise, emacs will prompt for a tag.

M-*
    Jump to previous tag.

C-u M-.
    Jump to next tag with same name.

C-u - M-.
    Jump to previous tag with same name.

M-,
    Repeat tag command ('tags-loop-continue'). 

M-x find-tag-other-window
    Split screen and open tag in second window.

M-x tags-search
    Regex search for a tag.

More info about tags:
    local www.cs.rochester.edu/u/brown/172/tools/etags.html
    C-h a tag


-------------------------------------------------------------------------------
programming
-------------------------------------------------------------------------------

M-a | M-e
    Beginning|end of statement.

C-M-a | C-M-e  (press Alt and Ctrl)
    Beginning|end of function

C-M-h
    Mark function

M-/
    Try to auto-complete a name, press again to cycle.  Useful for variables with long names.

sort-lines
    Sort lines in region.

C-x r t [string]
    Add prefix to every line, useful for making comments from text.
    Mark rectangular block, then this will insert a column of the string repeated.


-------------------------------------------------------------------------------
compiling
-------------------------------------------------------------------------------

To use emacs to run make and help edit source files with errors:
- Use dired to goto directory with the makefile.
- M-x compile

C-x `      : (back-tick) visit source file with next error
M-n        : next error (in *compilation* buffer)
M-p        : previous error (in *compilation* buffer)
C-c C-c    : visit source file for current error message (in *compilation* buffer)
or ENTER
C-c C-k    : kill compilation
SPACE      : page down
BACKSPACE  : page up

These keys also apply to emacs grep command.


-------------------------------------------------------------------------------
macros
-------------------------------------------------------------------------------

To create a new macro:

    C-x (
        Begin macro.
    C-x )
        End macro.
    C-x e
        Call the last keyboard macro that you defined with C-x (
    C-u
        Repeat 4 times.
    ESC n
        Repeat n times.

Then to save the macro:

    1. Create the macro using "C-x (" and "C-x )".
    2. Name the macro using "ESC x name-last-kbd-macro".
    3. Open the file that holds emacs macros.
    4. Insert the macro definition using "ESC x insert-kbd-macro".


-------------------------------------------------------------------------------
elisp functions
-------------------------------------------------------------------------------

list-colors-display   ;; shows colors as blocks


-------------------------------------------------------------------------------
fonts
-------------------------------------------------------------------------------

To change font size with keyboard:
    increase: C-x C-+  decrease: C-x C--  reset: C-x C-0

To change fonts (try maximizing/restoring window if font isn't redrawn):
    set-default-font 6x12
    emacs -font 6x12