0

I'm often switching between files with a unix encoding and those with a dos encoding. I find that when I start with a unix encoded file, and then open a dos encoded file, vim attempts to use the settings from the first file, and thus can't read the modelines in the latter file to get the proper encoding.

Of course I get :E518: Unknown option: ^M, because yeah, if it doesn't read the encoding properly it'll see a ^M.

I know modelines have their [security] problems, but is there any way around this problem? Right now I've been just re-opening the file (:e) which loads the file properly and executes the modelines, but not having to do this all the time would be great.

  • 1
    You shouldn’t need to use modelines at all to get Vim to detect Unix/Windows line endings correctly. What’s the output of :verbose set ffs?? How about :verbose set ff? when run in a file that has its line endings incorrectly detected? And how are you opening the files? – Rich Feb 4 at 20:49
  • 1
    Most of my files contain modelines, so my first file will open and set ffs, then when I open a second file (usually with tabe or something) it then gets the encoding wrong. If I were to open a dos file before a unix file, then ffs will be dos, if I reverse the order then it'll be unix. And when I don't explicitly write the mode lines, I inevitably (quite quickly too) accidentally change the encoding of files on save. – Matt Feb 4 at 22:19
3

I think you might be misunderstanding how the 'fileformats' (plural) setting is used. This setting is a global list of file formats that Vim tries when opening any file in order to see which matches the format of the file. When it has detected the format, Vim sets the per-file 'fileformat' (singular) option for that individual file.

If you are setting 'fileformats' to e.g. dos in a modeline, you are instructing Vim to consider all files to have dos line endings, regardless of the actual content of the file, so when you subsequently open a file with unix endings, Vim is still forced to treat it as a dos file.

Note that this only affects files that you open later on, as when you opened the first file 'fileformats' was set to the default value of unix,dos (or dos,unix): Vim is correctly detecting the format of the first file, and then reading the modeline which changes the value used for detection from that point on.

Note also that when you open the second file, its modeline is being executed (which you can see if you run the command :verbose set ffs?), but, like before, the updated 'fileformats' doesn’t take effect until the next time you read a file, which you are doing by manually invoking :e.

If you remove all the ffs settings from your modelines, Vim will use the default 'fileformats' value of unix,dos (or dos,unix) and correctly set the per-file 'fileformat' based on the content of the file.

If, however, you are dead set on hard-coding your file formats into your modelines, you should at the very least change your ffs modelines into ff modelines. In this way, Vim will still detect the file format based on the content of the file on opening, but will then immediately override the file format (changing the line endings if necessary and marking the file as modified), so if you somehow accidentally change the 'fileformat' before writing (which you mention doing in a comment), this will be corrected the next time you open the file in Vim.

| improve this answer | |
  • I've been having this issue for 5-6+ years, and the "s" never occured to me. I just ran sed through one of my repos to change ffs to ff, and yeah, files now open perfectly. Thanks! – Matt Feb 5 at 13:35
  • To add to why I feel the need to set it all the time, when I don't set it I notice encodings accidentaly changing all the time, especially older files which often have a mix of line endings. Anyways, thanks again. – Matt Feb 5 at 13:37
  • 1
    @Matt Glad I could help! Regarding the root issue, are you certain that when you don't set the file format you have removed all such modelines? Because a stray ffs modeline seems like the most likely candidate for something that could frequently cause an accidental line ending change (in Vim). FWIW, for me, Vim's line ending detection has been completely rock solid in all the years I've used it (working on all manner of files, including ones with mixed or even Mac line endings), and I've never heard of anyone else complaining about it, either. – Rich Feb 5 at 15:30
  • Not entirely. I first discovered modelines around 2014, they solved some problems I thought I had (but I probably had set ffs in my vimrc by then too), so I continued to propagate them. And whenever I encountered line ending issues, I'd issue a set ffs command.. So, it's possible that I've just been accidentally propagating the need to adjust line endings all this time. I'll remove ffs everywhere I see it now (or at least change it to ff) and my problem might evaporate. I think my original problem was other people's QtCreator/VS always switching to dos, ruining the git diff – Matt Feb 5 at 16:24
  • @Matt Yeah, I suspect it will (and I hope it does!) – Rich Feb 5 at 16:27

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.