0

In a project I need to set fileformat=dos for all *.html files. Is it possible to do with autocmd?

My fileformats=unix,dos by default, since I'm on Linux.

I've tried the simplest way:

autocmd BufRead *.html setlocal fileformat=dos
autocmd FileType html setlocal fileformat=dos

Nothing worked even in this simplified way.


What I've been trying to set up is actually this (s:config is defined etc.):

if has_key(s:config, 'fileformats')
  for key in keys(s:config.fileformats)
    execute ':autocmd FileType ' . key . ' setlocal fileformat=' . s:config.fileformats[key]
  endfor
endif

After some additional research, I found that fileformat is set automatically if binary is off, and if all lines end with \r\n and fileformats=unix,dos (in my case), then it's set to dos, otherwise unix. In my case the problem was (and probably is) that a file doesn't have a new line character at the end, that's why fileformat=unix always.


Currenty I've resorted to manually setting such malformed file to fileformat=dos

Could anyone help me please to automate it?

1 Answer 1

1

There are in fact 4 different ways to do this. You can read all of them in :h new-filetype. I will only describe the simplest solution:

In your user runtime directory, create a new file in your ftdetect directory (e.g. ~/.vim/ftdetect) with the following line (or any other setting you want to apply)

au BufRead,BufNewFile *.html            set fileformat=dos

The name of the file is not important. Restart vim, open a new file with .html extension and check that its fileformat has been changed successfully.

1
  • 1
    Thank you, sorry, I didn't have a chance to try it yet, I hope I'll to in later during the week Jun 28 at 10:01

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.