Emacs: Move Cursor to Punctuation

By Xah Lee. Date: . Last updated: .

Jump to Punctuation

This command lets you jump to any punctuation. You can change what character are considered punctuation.

(defvar xah-punctuation-regex nil "A regex string for the purpose of moving cursor to a punctuation.")
(setq xah-punctuation-regex "[\\!\?\"\.'#$%&*+,/:;<=>@^`|~]+")

(defun xah-forward-punct (&optional n)
  "Move cursor to the next occurrence of punctuation.
The list of punctuations to jump to is defined by `xah-punctuation-regex'

URL `http://ergoemacs.org/emacs/emacs_jump_to_punctuations.html'
Version 2017-06-26"
  (interactive "p")
  (re-search-forward xah-punctuation-regex nil t n))

(defun xah-backward-punct (&optional n)
  "Move cursor to the previous occurrence of punctuation.
See `xah-forward-punct'

URL `http://ergoemacs.org/emacs/emacs_jump_to_punctuations.html'
Version 2017-06-26"
  (interactive "p")
  (re-search-backward xah-punctuation-regex nil t n))

You need to give them a easy key for this idea to work, such as {【Ctrl+7】, 【Ctrl+8】}. 〔►see Emacs: How to Define Keys〕 (you'll need a key that can be held down to repeat, such as 【F8】, 【Ctrl+8】, and for example 【Ctrl+c c】 wouldn't work. 〔►see Keybinding Design, Fast-Repeat Commands〕)

This is to be used together with Emacs: Move Cursor to Bracket/Quote. If you don't have that, use the backward/forward brackets first, because that's more useful.

The jump to punctuation is inspired by ace-jump-mode: https://github.com/winterTTr by winterTTr. I heard about ace-jump from jcs's blog

Jump to Equal Sign

(defun xah-forward-equal-sign ()
  "Move cursor to the next occurrence of equal sign 「=」.
URL `http://ergoemacs.org/emacs/emacs_jump_to_punctuations.html'
Version 2015-06-15"
  (interactive)
  (re-search-forward "=+" nil t))

(defun xah-backward-equal-sign ()
  "Move cursor to previous occurrence of equal sign 「=」.
URL `http://ergoemacs.org/emacs/emacs_jump_to_punctuations.html'
Version 2015-06-15"
  (interactive)
  (when (search-backward-regexp "=+" nil t)
    (while (search-backward "=" (- (point) 1) t)
      (left-char))))

Jump to Dot/Comma

(defun xah-forward-dot-comma ()
  "Move cursor to the next occurrence of 「.」 「,」 「;」.
URL `http://ergoemacs.org/emacs/emacs_jump_to_punctuations.html'
Version 2015-03-24"
  (interactive)
  (re-search-forward "\\.+\\|,+\\|;+" nil t))

(defun xah-backward-dot-comma ()
  "Move cursor to the previous occurrence of 「.」 「,」 「;」
URL `http://ergoemacs.org/emacs/emacs_jump_to_punctuations.html'
Version 2015-03-24"
  (interactive)
  (search-backward-regexp "\\.+\\|,+\\|;+" nil t))

Enhanced Editing Commands Topic

  1. Emacs: Select Line, between Quotes, Extend Selection
  2. Emacs: Copy/Cut Current Line If No Selection
  3. Emacs: Copy/Cut All or Selection
  4. Emacs: Paste or Paste Previous
  5. Emacs: Delete Whitespace around Cursor
  6. Emacs: Move Cursor to Beginning of Line/Paragraph
  7. Emacs: Move Cursor by Text Block
  8. Emacs: Move Cursor to Bracket/Quote
  9. Emacs: Move Cursor to Punctuation
  10. Emacs: Delete Text Block
  11. Emacs: Toggle Comment Current Line
  12. Emacs: Insert Date Time

Working with Brackets Topic

  1. Emacs: Highlight Brackets ({[]})
  2. Emacs: Insert Brackets by Pair
  3. Emacs: Delete Bracket ({[]}) by Pair
  4. Emacs: Select Line, between Quotes, Extend Selection
  5. Emacs: Move Cursor to Bracket/Quote
Like it? Buy Xah Emacs Tutorial. Thanks.