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

Viewed 8684

How do you enable flyspell-mode to be automatically used for every file and every major mode as soon as Emacs is started?

Also, is there an XML dictionary that does not mark XML tags as misspelled words?

5 Answers

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)

I couldn't say when, but flyspell-mode now does a pretty good job of knowing what mode it is in and reacting accordingly. Here is my use-package implementation with an interface to company-completion.

 (use-package flyspell :demand t
   :config
   (use-package 
     flyspell-correct-helm) 
   (defun flyspellCompletion() 
     (flyspell-mode 1) 
     (set (make-local-variable 'company-backends) 
          (copy-tree company-backends)) 
     (add-to-list 'company-backends 'company-ispell)) 
   (defun flyspell-most-modes() 
     (add-hook 'text-mode-hook 'flyspellCompletion) 
     (add-hook 'prog-mode-hook 'flyspellCompletion)
     (dolist (hook '(change-log-mode-hook log-edit-mode-hook)) 
       (add-hook hook (lambda () 
                        (flyspell-mode -1))))) 
   (flyspell-most-modes) 
   :bind (:map flyspell-mode-map
               ("C-." . flyspell-correct-wrapper)))

I am using Emacs 27.1 on Debian 11.1 in 2021 October. I wrote a function that does the right thing, based on the the type of the buffer's major mode, and hooked that to find-file-hook, which runs for every file visited. I offer this as an supplement to other answers, not a replacement. Rationale:

  • I found flyspell-mode (whether invoked by key binding, or as a mode hook), did not automatically recognize the type of mode/buffer/file, contrary to answer by @RichieHH.
  • I found flyspell-all-modes does not exist, as implied by @zev.
  • I found the suggestion of adding to text-mode-hook and prog-mode-hook, per @b4hand, worked as far as it went, but left Flyspell off for the many random files I have that open in Fundamental mode.

One potential drawback is, this will not run for buffers not associated with any file. There is apparently no good way to hook "creation of any buffer". However, one should be able to add to multiple hooks with no ill effects -- if called multiple times, the function should find Flyspell already enabled, and do nothing. So add hooks to text-mode-hook and prog-mode-hook as well, if you like.

(add-hook 'find-file-hook 'flyspell-on-for-buffer-type)

(defun flyspell-on-for-buffer-type ()
  (interactive)
  ;; if flyspell mode is not already on, turn it on
  (if (not (symbol-value flyspell-mode))
      (if (derived-mode-p 'prog-mode)
      (progn
        (message "Flyspell on (code)")
        (flyspell-prog-mode))
    (progn
      (message "Flyspell on (text)")
      (flyspell-mode 1)))))
Related