2

I would like to set set spell whenever I open a tex file.

My first attempt is

autocmd FileType tex setlocal spell

I am using setlocal here because I may open other files in new tabs after opening a tex file and I don't want spell in the new files.

This works well, but I notice that if I open another tex file in new tab, the spell is not enabled.

I tried

autocmd TabEnter,FileType tex setlocal spell

but it does not work.

  • Better idea: ftplugin. Will write a detailed answer later. – D. Ben Knoble Feb 23 '18 at 13:48
  • Note that it would be similar to my answer here – D. Ben Knoble Feb 23 '18 at 13:49
  • @DavidBenKnoble That looks like what I need, will try in out later. Thanks in advance! – hklel Feb 23 '18 at 14:02
4

Frame Challenge : Using Vim "Properly"1

autocmd is NOT the solution to filetype-dependent settings. It might seem like a good candidate at first, but Vim was built after the Unix philosophy of placing files in defined directory structure to affect behavior. Most vim users learn about vimrc first. But there are more (see :help rtp).

What this means for the OP is as follows : Tex filetype-dependent settings, mappings, &c. should be placed in

~/.vim/ftplugin/tex.vim

E.g., create ~/.vim/ftplugin/tex.vim with the contents

setlocal spell

to have Tex files get spell on. Note I used setlocal to make it buffer specific.

If you find that something in these files conflicts with other settings, you should place the offending line in ~/.vim/after/ftplugin/tex.vim to have it override any other settings.

Finally, make sure you at least enable filetype plugins with filetype plugin on.

Further Resources

  • :help filetype
  • :help ft-tex-syntax
  • :help ft-tex-ftplugin

  1. I put "properly" in quotes because at times, when speaking about the Vim philosophy, I've been told its powerful configurability means there is no "right" way to use it. I disagree—I admit there are lots of creative ways to use a saw, but it was originally meant to cut things (wood mostly).
  2. The original version of this answer came from my own post here. I do not consider them explicit duplicates, however.
  • There are those (not me!) who argue that they prefer autocommands because they like to have all their configuration in a single file. It's not a view I subscribe to (ftplugin just seems more elegant, and I believe is very slightly more efficient), but I think it's a reasonable position to take. – Rich Feb 26 '18 at 10:37
  • Fair enough @Rich —I’d say there are some things autocmds work for (running complex scripts for certain events, e.g.) but for filetype settings, we already have a dedicated mechanism, hence the answers – D. Ben Knoble Feb 26 '18 at 11:02
0

I've got the desired behavior using

autocmd BufReadPre *.tex setlocal spell

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.