The context :

  • GNU Emacs 26.1
  • working on Debian 10.3
  • currently editing YAML files (Ansible code)

Here's how my code looks like :

enter image description here

As you can see, the syntax highlighting is puzzled by the odd number of single quotes in it's difficult... and applies color to everything until it reaches the next single "single quote".

Is there a way to instruct the syntax highlighting logic that "single single quotes" are ok in lines starting with - name: and refrain it from colouring everything until the next "single single quote" ?

NB : this is a purely cosmetics question, everything works fine, no error message, just the display pictured above.

EDIT: Thank you @Lindydancer and @wasamasa. The solutions / workarounds you suggest are way beyond my Emacs skills, so I think I'll have to live with this cosmetic issue. Not able to pick my "favorite" answer either, sorry.

BTW, while working on different topics, I tried Spacemacs and realized it can highlight my YAML snippet without being puzzled by the single quotes :

Spacemacs on the left, regular Emacs on the right

May this help those investigating, and others searching for an alternative.

  • I can't reproduce your problem on Emacs 28.0.50, with yaml-mode Package-Version: 20210508.1641. For me, a yaml file is highlighted like the example you show from spacemacs. If you've installed yaml-mode from Debian, it may be outdated. Emacs 26 is three years old now too. Upgrading your Emacs and installing yaml-mode from melpa might solve your problem
    – Tyler
    May 11, 2021 at 17:32

2 Answers 2


Ideally you'd try fixing the highlighting in the mode itself. I am the maintainer of yaml-mode, if you have ideas how to do this without ending up writing a YAML parser, please let me know on its issue tracker.

One possible workaround for programming languages with comment syntax is to put a comment starter on the affected line, then a string terminator and optionally a comment ender (or none if it's a line comment). That way syntax highlighting doesn't bleed beyond that line. I'm afraid you can't do that with YAML though.


It's possible to override the syntax table for specific syntactic constructs by overriding the variable syntax-propertize-function. It should be bound to a function that set the syntax-table text property on the part of the buffer where the default syntax table should be overridden.

One way to do this it to use the support function syntax-propertize-rules.

In the following example < and > around words are treated as parentheses. (It's part of the test suite of my faceup package.)

(defvar faceup-test-mode-syntax-table
  "Syntax table for `faceup-test-mode'.")

(defvar faceup-test-font-lock-keywords
     (0 (progn
          (add-text-properties (match-beginning 0)
                               (match-end 0)
                               '(help-echo "Baloon tip: Fly smoothly!"))
  "Highlight rules for `faceup-test-mode'.")

(defun faceup-test-syntax-propertize (start end)
  (goto-char start)
     (1 "()  ")
     (3 ")(  ")))
   start end))

(defmacro faceup-test-define-prog-mode (mode name &rest args)
  "Define a major mode for a programming language.
If `prog-mode' is defined, inherit from it."
  (declare (indent defun))
     ,mode ,(and (fboundp 'prog-mode) 'prog-mode)
     ,name ,@args))

(faceup-test-define-prog-mode faceup-test-mode "faceup-test"
  "Dummy major mode for testing `faceup', a test system for font-lock."
  (set (make-local-variable 'syntax-propertize-function)
  (setq font-lock-defaults '(faceup-test-font-lock-keywords nil)))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.