4

In my ftplugin/plaintext.vim script, I have the formatoptions set for editing mostly prose:

setlocal formatoptions=tcroqan1j

However, in very specific instances -- particularly, Python's idiomatic requirements.txt file -- I want the format options relaxed. I tried this within the same ftplugin script:

" Remove wrapping for Python "requirements.txt" files
autocmd BufRead,BufNewFile requirements.txt setlocal formatoptions=croj

...but it doesn't seem to work.

Is it possible to have local settings for specific files (based on their name) and, if so, how would I go about it?

0

1 Answer 1

5

The code in ftplugin/plaintext.vim is run when the file type is set, which occurs after the file has been opened.

So you are setting up your autocommand after the file has already been opened. Thus, the autocommand never fires. If you were to close and reopen the file in the same Vim session you'd find formatoptions set correctly.

Easiest fix is to move your line to set up the autocommand into your vimrc file.

Edit: It occurs to me there might be an additional problem even when the autocommand does fire: the value it sets might then be overwritten by the code in your ftplugin file: if simply moving the autocommand doesn't fix the issue, you could also try checking the file name in your ftplugin file:

" Don't overwrite value set by autocommand
if expand('%:t') != 'requirements.txt'
  setlocal formatoptions=tcroqan1j
endif

Edit 2: Of course, with the conditional in place, there's not much point in having a separate autocommand!

if expand('%:t') == 'requirements.txt'
  setlocal formatoptions=croj
else
  setlocal formatoptions=tcroqan1j
endif

I have a nagging feeling there's a more elegant way to implement this, but right now what that solution might be eludes me. Perhaps someone else will provide an alternative answer later on.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.