I want to use auto-fill-mode in Markdown files. By default, it works fine, with one exception. I'm using the RMarkdown variant of markdown. This requires that code block headers remain on one line. They are frequently much longer than my fill-column, so if I use auto-fill, they get wrapped. How can I exclude these lines from getting filled?

1 Answer 1


auto-fill-mode refers to the variable auto-fill-function to determine how to fill lines. auto-fill-function is always a local variable, making it easy to define your own function for any mode you like. To solve my own problem, I use the following:

(defun tws-markdown-auto-fill ()
  (setq auto-fill-function 'tws-markdown-fill-function))

(defun tws-markdown-fill-function ()
        (looking-at "```{"))

(add-hook 'markdown-mode-hook 'tws-markdown-auto-fill)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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