2

I have a tool opens files and forces a specific filetype. It launches vim with arguments along the lines of:

vim -c 'set filetype=foo' [file1 [file2 [...]]

This works fine for the first file, but just as you'd expect, this only works against the first file since it's the equivalent of changing the filetype once after launch. Every subsequent file on the command line picks up vim's default / autodetected filetype and not foo. I can fix this with:

vim -c 'au BufRead * set filetype=foo"'

but this overrides all files, including ones I subsequently open, for example via gf. Ideally I just want the identified files to be set to foo, and nothing else. I'm not aware of any easy heuristic to detect the filetype by name or content, either: it's context sensitive (note I can't add a hint to the files, though they are usually constrained to one directory, if that helps).

Is there a way to set a default filetype for just the specified files? Failing that, is there another reasonably clean (i.e. I still want to be able to use vim normally in another window) way to approach it, for example some hack to override ftdetect behaviour for one session?

1

In principle, using argdo, which runs the command for each argument (bufdo would also work here), works. As a bonus, prepend silent to avoid running into the annoying "Press ENTER" prompt.

  • Short version

    vim +'sil argdo se ft=foo' file1 file2...
    
  • Long version

    vim -c 'silent argdo set filetype=foo' file1 file2...
    

In practice, syntax highlighting won't be enabled on subsequent files (see Syntax highlighting is not turned on in vim when opening multiple files using argdo). This may be fixed with:

vim -c 'silent argdo set eventignore-=Syntax | set ft=foo' file1 file2...

However, there are reports in the chat that the workaround doesn't work for everyone. It works on Vim builds 8.1 p1-2269 and 8.2 p1-2367.

1
  • 1
    @gra My flag to migrate this post to Vim Stack has been declined. I have also done more research but could not get it 100% working for me. So let's hope this is indeed fixed in newer versions.
    – Quasímodo
    Jan 20 '21 at 16:20

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.