I am actively using flyspell on all text files under text-mode.

For example: Under requirements.txt text file I am keeping my python packages where I do not need to enable flyspell. But since it is a text file flyspell keep does its checks. So I want to disable flyspell for specific file names like: requirements.txt and so on. It also enable in yaml-mode even I did not enable it.

My setup:

(flyspell-mode 1)
(setq flyspell-issue-message-flag nil
      ispell-local-dictionary "en_US"
      ispell-program-name "aspell"
      ispell-extra-args '("--sug-mode=ultra"))

(dolist (hook '(text-mode-hook))
     (add-hook hook (lambda () (flyspell-mode 1))))
    (dolist (hook '(change-log-mode-hook log-edit-mode-hook))
      (add-hook hook (lambda () (flyspell-mode -1))))
  • yaml-mode is derived from text-mode, so it runs the text-mode-hook. – NickD Jun 12 '20 at 0:54
  • Do you really want that first line in the code? – NickD Jun 12 '20 at 1:20
  • Actually I don't. I took first line from a documentation and it remain as it is. Should I delete it? – alper Jun 12 '20 at 8:32
  • I think so: you don't want to turn it on globally; that's why you turn it on (and off) in all the hooks. – NickD Jun 12 '20 at 12:04

You can write a function that turns off the mode if the filename matches a list and add the function to the find-file-hook. Something like this:

(defvar no-flyspell-list '("requirements.txt"))

(defun turn-off-flyspell-if-match ()
  (if (member (file-name-nondirectory (buffer-file-name)) no-flyspell-list)
      (flyspell-mode -1)))

(add-hook 'find-file-hook #'turn-off-flyspell-if-match)

As I mentioned in a comment, yaml-mode is derived from text-mode so the text-mode-hook is run and it turns on flyspell-mode. But the derived mode hook, yaml-mode-hook is run after the parent mode hook, so you can add a turn-off function to it:

(add-hook `yaml-mode-hook (lambda () (flyspell-mode -1)))

Or add it to your list of other modes (change-log-mode-hook, log-edit-mode-hook) where you turn off flyspell-mode.

  • Is order of calling the add-hook important? To be safe I put end of the file. – alper Jun 12 '20 at 8:31
  • 1
    No, the only constraint is that the hook variable is defined, so the package where it is defined must be loaded already. That is not a problem for find-file-hook: that is done before your init file is loaded. – NickD Jun 12 '20 at 12:03

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.