Advanced Emacs Motions
Universal Argument
Invoked with C-u.
Invoked by itself C-u does the same as if you had typed C-u 4
The universal-argument is multiplicative, not additive. C-u C-u is like C-u 16.
Numeric arguments
Lets you pass a numeric constant to a command
Because these are so useful they are bound to a multitude of keys. One
can use either the Control key + the numbers or the Meta Keys + the
numbers.
M-0 through to M-9
To move 10 characters forward, type M-1,M-0,C-f
C-0 through to C-9
C-M-0 through to C-M-9
C-u num (for double digit numbers)
Negative Argument
Useful for modifying text you have just TYPED!
Let's say I am typing a word
Bound to M–; C–; and C-M–.
Limitations: One does not generally count while typing!
Basic Movement
Emacs does support the navigation keys (arrow keys and pg up/down and so
on) but it's better to learn the real Emacs keys.
C-n C-p C-f C-b
If you customize this variable C-n will also create new lines if at the
end of the buffer, obviating the need for you to hit to create a new
line.
(setq next-line-add-newlines t)
To move to the beginning of a line, type C-a; to move to the end, type
C-e
Movement by Word
M-f M-b
Note: What we consider a word to be is NB. A word in prose differs from
a word in code.
Movement by sentence
M-e M-a
;; Defining a sentence in Emacs (setq sentence-end-double-space nil) (setq sentence-end "[.\".,;!?*:'\"] ") (global-set-key (kbd "S-<f12>") 'mark-end-of-sentence)
Movement by paragraph
The paragraph keys are bound to the less-than-helpful C-down/up navigation keys.
Scrolling
C-v to scroll down one screen, and M-v to scroll up one screen.
The command C-M-v scrolls the other window. To scroll backwards with this command use the
negative argument.
I prefer to use C-v and M-v to jump to headings. My custom commands follow:
;; Go to markdown headings (defun my-markdown-mode-hook () (define-key markdown-mode-map (kbd "C-v") 'markdown-next-visible-heading) (define-key markdown-mode-map (kbd "M-v") 'markdown-previous-visible-heading)) (add-hook 'markdown-mode-hook 'my-markdown-mode-hook) ;; Go to org headings (defun my-org-mode-hook () (define-key org-mode-map (kbd "C-v") 'org-next-visible-heading) (define-key org-mode-map (kbd "M-v") 'org-previous-visible-heading)) (add-hook 'org-mode-hook 'my-org-mode-hook)
Beginning and end of buffer
M-< M->
The buffer jump commands are useful for they leave the mark at the
originating position, meaning you can jump back to where you came from
with C-u C-SPC.
Movement by symbolic-expression
This gives you the ability to move by balanced expression (or pairs)
Common examples are quotes (' ', ” “) and brackets ([ ], ( ), { }, < >).
Movement by s-expression is similar to movement by character and word,
only the prefix is C-M-.
So move forward is C-M-f and backward is C-M-b.
The movements pertain only in programming modes.
(defun my-refresh-and-find-new-packages () (interactive) (package-refresh-contents) (my-update-package-list) (my-find-new-packages)) (defalias 'fnp 'my-refresh-and-find-new-packages)
Especially useful for LISP.
Forward and Backward List
Two more functional navigational aids are C-M-n and C-M-p.
Useful for moving through nested lists.
Moving In and Out of Lists
To move into (down) a list type C-M-d; to move out (up) a list type
C-M-u.
Move by Paragraph
M-} Move forward to end of paragraph
M-{ Move backward to start of paragraph
Learn to move around the buffer with C-s or C-r
C-s C-r
C-M-s C-M-r
C-s C-w - Allows you to select a word for searching; equivalent to Vim's
Shift-*
C-s M-y - Searches for text copied to clipboard; M-y yanks the copied
text into isearch
C-s M-c + search term Provides for a case sensitive search
C-s combined with other movement keys is very powerful
Combine C-s with other navigational keys such as M-f M-b C-a etc.
Think about the best ways to move before you move
M-s w searches for whole words
While M-s w can be used in any context, its features make it especially
suited for searching code.
C-s C-s repeats the last search query
Move back to indentation (M-m)
Test M-m
Registers / Bookmarks
C-x r SPC + [any letter] to store point in a register and C-x r j + [the
letter] to jump back to it
The Mark
C-x C-x toggles between the point and the mark in the buffer
C-u C-SPC cycles mark ring
(setq set-mark-command-repeat-pop t)
Repositioning point
M-r
Moves the point between the top, center and bottom (by default) of the
visible window but without actually scrolling up or down.
Goal Column
Controls the vertical trajectory of the cursor
C-x C-n
This is line 1
This is line 2 Short
This is line 4
(C-u C-x C-n to disable)
Form Feed
Move in a considered fashion through an Emacs document by marking signs along the journey.
Use the new page character, represented by the Form Feed character.
C-q C-l inserts the form feed Character ^L
After doing this, you will see a character that looks like ^L in your buffer, representing the Form Feed.
By inserting it into one's document one can be intentional about navigation.
To navigate in Emacs from one ^L to another invoke:
C-s C-q C-l and thereafter only C-s.
To go backwards use C-r instead.
One can also use C-x ] and C-x [ but then ^L must be at the start of a new line, as in