4

Is there a way to set (and have vim remember) settings that are specific to an individual file? Specifically, I would like to turn spell checking on or off for specific files and have vim remember that setting for each.

In my particular case, I maintain a fairly large set of note pages written in markdown. All of these files end in the .md suffix. Some of these files predominantly contain text and others contain mostly code and/or large and unwieldy URLs. I'd like vim to remember which files have spell checking turned on and which have it turned off. As it stands, they all have spell checking turned on by default, and I need to manually turn it off for individual files after opening them.

Is this possible when using either vim or gvim?

3
  • I believe the only way is to use a session. I realize that does not perfectly cover the demand. Jun 21, 2022 at 13:33
  • 2
    I hesitate to offer this as an answer because I'm not knowledgeable in this particular area. Please check out vim modelines and refer to this question. It may get you what you want.
    – MDeBusk
    Jun 21, 2022 at 16:26
  • Wouldn't it be better to add a conditional in after/ftplugin/markdown.vim to set spell or nospell depending on the contents of the file? For example, since you don't want to have spellcheck on when there are URLs in your markdown files, then if search("https://") != 0 | set nospell | endif. Alternatively, you can check whether there are common tokens in your markdown notes that indicate such files should have spellcheck enabled. Or count the number of URLs in a given file and if this is a large number, set nospell.
    – r_31415
    Jun 21, 2022 at 17:12

1 Answer 1

2
  • You can use views if viewoptions contains options or localoptions, but views are really meant to store window information (which happens to include the current buffer).
  • Sessions similarly are more about the overall vim state, but if sessionoptions contains options than, like views, global and local options will be restored. (This is probably [not something to be desired very often if you use sessions to save editing state and then modify, say, a vimrc or ftplugin, since options and maps can be mutilated.)
  • You may be interested in something like a local vimrc (for which google produces many, many results). Then your local vimrc for a project can specify.
  • A lighterweight solution is a modeline like vim: spell, if you/your team doesn't mind them.
  • ftplugins as mentioned are another option, though you have to do something more interesting than just remembering.
  • Yet another option is loading a list of files (define a list variable, or read a file, or whatever you want) and (dynamically) creating autocommands based on that. Those autocommands could even update the file's inclusion in the "spell list" whenever the spell option was changed by modifying the source of the list.

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.