7

Every once and a while Emacs fails at syntax highlighting and the coloring gets all funky in a buffer. Is there any way to force Emacs to "recolor" the syntax? Just try over? I don't mind if it takes a moment.

2 Answers 2

11

I think M-x font-lock-fontify-buffer will do what you are looking for. Or select a region and do M-o M-o (or M-x font-lock-fontify-block).

3
  • Thanks Jon! Worked great. I knew that there must just be some Emacs vocabulary I was missing: "Fontify".
    – sligocki
    Nov 2, 2011 at 17:07
  • No problem! Glad it was useful.
    – user725091
    Nov 5, 2011 at 22:53
  • this doesn't (currently) take into account any changes made after loading a buffer by calling (e.g. in the scratch buffer) font-lock-add-keywords. For these to be applied, you need to do M-x normal-mode or M-x revert-buffer.
    – Mark
    Jun 6, 2015 at 17:22
1

I once wrote the following simple function to reset the buffer to its natural mode, refontify it, bring the line where the cursor is to the center of the screen, disable the menu-bar, disable the tool-bar and move the scroll-bar left.

(defun --normal-mode-no-gimmicks ()
  "Enable buffer `normal-mode' and refontify.
Disable frame menu, toolbar, scrollbars."
  (interactive)
  (menu-bar-mode 0)
  (tool-bar-mode 0)
  (set-scroll-bar-mode 'left)
  (toggle-scroll-bar 1)
  (normal-mode) (recenter-top-bottom)
  (font-lock-fontify-buffer))

This can be very useful when the mode changes, Emacs suddenly displays the menubar or something else goes wrong. Then I just press M-g g to heal it.

(global-set-key [?\M-g ?g] '--normal-mode-no-gimmicks)

I didn't know about M-o M-o; it seems as if this could be a better key binding for this function.

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.