How to enable flyspell-mode in emacs for all files and all major modes?
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?
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.
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
andprog-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)))))
精彩评论