Emacs Lisp: How to Modify Syntax Table Temporarily

By Xah Lee. Date: . Last updated: .

Here's a example of how to use a temporary syntax table just for 1 command.

Suppose, we want the curly quote to be considered as a bracket.

Emacs's standard-syntax-table considers the curly quote as punctuation character.

〔►see Emacs Lisp: How to Find Syntax of a Character?

(defun test ()
  "move cursor to the right 1 char, but if it is a left curly quote, jump to the right curly quote.
Version 2017-02-13"
  (interactive)
  (let ((-temp-syn-table (make-syntax-table)))

    (modify-syntax-entry ?\“ "(”" -temp-syn-table)
    (modify-syntax-entry ?\” ")“" -temp-syn-table)

    (with-syntax-table -temp-syn-table
      (if (looking-at "\\s(")
          (forward-sexp 1)
        (right-char )))))

test “xyz” text

(info "(elisp) Syntax Table Functions")

Syntax Table Topic

  1. Emacs Lisp: Syntax Table Tutorial
  2. Emacs Lisp: How to Find Syntax of a Character?
  3. Emacs Lisp: How to Modify Syntax Table Temporarily
  4. Emacs Lisp: How to Determine If Cursor is Inside String or Comment
  5. Emacs Lisp: Find Matching Bracket Character
Like it? Buy Xah Emacs Tutorial. Thanks.

or, buy something from Best Keyboard for Emacs