2

I'm trying to use autocmd to get some filename-specific mappings. I followed some instructions online and write the following thing in my vimrc:

autocmd BufRead,BufNewFile special.txt (some mappings here)

It works fine when I enter a file or create a new buffer with the name special.txt, but not when I create an empty buffer (for example, when starting vim with no argument) and save it with a name special.txt.

Even a simple echomsg won't work:

autocmd BufRead,BufNewFile special.txt echomsg test

and there's no text test in message history.

But in the documentation of BufRead I see

Also triggered for the filetypedetect group when executing ":filetype detect" and when writing an unnamed buffer in a way that the buffer gets a name.

When I save an empty buffer with the name special.txt, the buffer gets a name and BufRead should be triggered. But in fact it is not. Is there anything I'm missing, or I just have to use another autocommand event?

And in case it is useful, I'm using command line version of vim in the vim-gtk package from Ubuntu repository.

1

You have to interpret the help text like this:

Also triggered [only] for the filetypedetect group when (executing ":filetype detect" [or] when writing an unnamed buffer in a way that the buffer gets a name).

So, this one works:

:autocmd filetypedetect BufRead,BufNewFile special.txt echomsg "ftdetect test"
  • It seems that filetypedetect is only used for file type detect commands, and I'm not adding a new file type. Should I put those mappings into filetypedetect group, or create a new file type for special.txt and write the mappings as filetype plugins? – infmagic2047 Oct 17 '14 at 14:46
  • If it's just a few tweaks and mappings, I wouldn't create a new filetype for that; use your judgement. For clarity, I would put those into ~/.vim/filetype.vim, not ~/.vimrc. – Ingo Karkat Oct 17 '14 at 17:31

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.