How to enable flyspell-mode in emacs for all files and all major modes? How to enable flyspell-mode in emacs for all files and all major modes? xml xml

How to enable flyspell-mode in emacs for all files and all major modes?


The answer from this question worked for me:

How to enable automatic spell check by default?

Furthermore, it appears to be more general compared to the other prior answers. Add the following lines to your .emacs or init.el.

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


Chances are, you don't really want flyspell-mode enabled for all modes, but instead want flyspell-mode enabled for modes that deal primarily with text (text-mode, message-mode, etc.), and flyspell-prog-mode for the programming modes (C/C++, Java, Ruby, Lisp, etc.). The difference between the two modes is that the first checks all words, whereas the flyspell-prog-mode only checks words in comments and strings (thereby avoiding checking the code, which generally isn't words).

Either way, there is no single place to enable flyspell in all files/buffers because it has been written to always be buffer local. A close approximation would be

(defun turn-on-flyspell () (flyspell-mode 1))(add-hook 'find-file-hooks 'turn-on-flyspell)

That doesn't cover buffers which don't have associated files, and I don't advise using it because it doesn't distinguish between the programming modes and non-programming modes - which I think is useful.

Because there is no way to know whether certain modes are programming modes or not, you need to manually add customizations for all the programming modes you care about, with something like:

(mapcar (lambda (mode-hook) (add-hook mode-hook 'flyspell-prog-mode))        '(c-mode-common-hook tcl-mode-hook emacs-lisp-mode-hook           ruby-mode-hook java-mode-hook))

Note: the two chunks of code probably don't play well together.

And, regarding the XML, flyspell already has customizations for sgml-mode, html-mode, and nxml-mode to not spell check the tags (as of Emacs 23.2). If you're using an older version of Emacs (back to 21.1), you should be able to add this to your .emacs to get the support for nxml-mode:

(put 'nxml-mode 'flyspell-mode-predicate 'sgml-mode-flyspell-verify)


You can add the following to your Emacs init file:

(flyspell-all-modes)

The function description states:

Use Flyspell in all major modes. Applies both to existing buffers and buffers that you subsequently create. Turns off `flyspell-text-modes' if on.

EDIT: Apparently the above function is only included in the version of flyspell that is in Emacs 24. If you can't use that version, you should instead use the solution suggested by Trey to "semi-globally" enable flyspell. To disable XML tag checking with NXML, you can add the following line to your Emacs init file:

(put 'nxml-mode 'flyspell-mode-predicate 'sgml-mode-flyspell-verify)

Note: This line is already in the flyspell.el included in Emacs 24.