2

I want an autocmd for downloaded log files. These files are called log.txt or log (1).txt (where 1 could be any number). (On Firefox/Windows, when you download a file with the same name as another file, it adds a number to separate.)

So far I've figured out three things that tripped me up:

  • setfiletype doesn't work if something else (like vim's ftdetect) already set a filetype.
  • I need to escape spaces.
  • * works very differently from other vim regex (:help {aupat})

I have this, but it seems very awkward:

au BufRead log[\ ()0-9]\\\{0,10\}.txt  set filetype=log
1
  • Could you tell us more what do you dislike in your solution? Does it match the files you want? Does it match too much? Sep 23, 2023 at 9:25

1 Answer 1

1

setfiletype doesn't work if something else (like vim's ftdetect) already set a filetype

That's the whole purpose of this command and is documented on the relevant help page. When multiple patterns match only the first one will matter. Use :set command otherwise.

it seems very awkward

You don't have to match in aupat strictly. For example,

autocmd BufRead *.txt
    \    if expand("<afile>") =~? 'log...'
    \ |      set ft=log
    \ |  endif

Or you can even move that whole mess into separate function.

Your Answer

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

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