7

When editing Markdown files (.md, .mdwn, etc.) and Mediawiki files (.mw), I almost always need to have flyspell-mode loaded. How to have that automatically happen via configuration?

qazwsx
  • 569
  • 3
  • 12

4 Answers4

8

Put this in your .emacs:

(add-hook 'markdown-mode-hook 'flyspell-mode)

It will enable flyspell-mode whenever markdown-mode is entered; i.e. when you open markdown files.

Trebuchette
  • 236
  • 1
  • 4
  • Adding `(add-hook 'markdown-mode-hook 'flyspell-mode)` had no effect AFAICT. After restarting Emacs and opening a .mdwn file, the mini-buffer shows the Markdown-mode is automatically enabled, but not the flyspell-mode. – qazwsx Oct 10 '15 at 18:05
  • 1
    Revisiting this thread after two years, now in Emacs 25.2, your solution seems to work now. I verified that Flyspell mode is turned on by restarting Emacs, opening a new file test.md, and seeing that Flyspell is listed in `M-x describe-mode`. – qazwsx Jun 11 '17 at 20:52
2

There are two answers recommending adding flyspell-mode as a markdown-mode-hook function. Perhaps the problem is that flyspell-mode is a toggle. There is also turn-on-flyspell, defined like so:

(defun turn-on-flyspell ()
  "Unconditionally turn on Flyspell mode."
  (flyspell-mode 1))

This is a built-in function which accomplishes the same thing as in marcanuy's answer.

So, this should do what you want, unconditionally:

(add-hook 'markdown-mode-hook 'turn-on-flyspell)
(add-hook 'mediawiki-mode-hook 'turn-on-flyspell)
Jason Blevins
  • 640
  • 5
  • 12
  • 1
    To avoid those problems, minor modes were changed years ago so that calling `foo-mode` from Elisp (e.g. via a major mode hook) just turns the mode ON rather than doing the toggling that happens via that function is called interactively. So no: you don't need to resort to `turn-on-foo-mode` (these functions are relics from the time before I made this change). – Stefan Jun 11 '17 at 00:49
  • Thanks for clarifying, @Stefan. I wondered why qazwsx had trouble with using the minor mode function directly. I also didn't realize at first that this was a two-year-old thread :) – Jason Blevins Jun 11 '17 at 20:48
1

Base on Trebuchette's answer:

(add-hook 'markdown-mode-hook 'flyspell-mode)
(add-hook 'mediawiki-mode-hook 'flyspell-mode)

But if you want to turn on flyspell-mode while editing text files in various major-modes, you can just use:

(add-hook 'text-mode-hook 'flyspell-mode)

this will works because markdown-mode and mediawiki-mode are base on text-mode. Analogously, we use prog-mode-hook to setup common programming environment.

xuchunyang
  • 14,302
  • 1
  • 18
  • 39
  • Adding `(add-hook 'text-mode-hook 'flyspell-mode)` to .emacs has no effect when I restarted Emacs and opened a `.mdwn` file. The `.mdwn` file did triggered Markdown-mode though. – qazwsx Oct 10 '15 at 18:02
0

I was looking for the same and finally found a solution that works:

(dolist (hook '(markdown-mode-hook))
      (add-hook hook (lambda () (flyspell-mode 1))))

Tested in GNU Emacs 25.1


Not tested this but should also work for other modes like mediawiki:

(dolist (hook '(markdown-mode-hook mediawiki-mode-hook))
      (add-hook hook (lambda () (flyspell-mode 1))))
marcanuy
  • 798
  • 6
  • 20