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://xahlee.info/emacs/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://xahlee.info/emacs/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 🚀. 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: [2014-05-26 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://xahlee.info/emacs/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://xahlee.info/emacs/emacs/emacs_jump_to_punctuations.html'
Version 2015-06-15"
  (interactive)
  (when (re-search-backward "=+" 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://xahlee.info/emacs/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://xahlee.info/emacs/emacs/emacs_jump_to_punctuations.html'
Version 2015-03-24"
  (interactive)
  (re-search-backward "\\.+\\|,+\\|;+" nil t))
ErgoEmacs mascot-s276x226
Buy Xah Emacs Tutorial

Xah Emacs Commands