Using Vim's autocmd to Set a Custom File Type for Specific Files
This comes in handy if you want a specific type of syntax highlighting or snippets for files like requirements.txt or an aliases file.
Quick Jump: Demo Video
In this video we’ll go over 2 examples. One where we make sure an
~/.aliases file gets shell script syntax highlighting and snippet support and another where a
requirements.txt file gets set as a Python file to easier read comments and version numbers.
We’ll also go over how the
file command can give us hints on what a file type is.
The autocmd configuration in Vim to make this work
" Make sure all types of requirements.txt files get syntax highlighting. autocmd BufNewFile,BufRead requirements*.txt set ft=python " Make sure .aliases, .bash_aliases and similar files get syntax highlighting. autocmd BufNewFile,BufRead .*aliases set ft=sh
- 0:09 – Making sure an
.aliasesfile gets set as a shell script according to Vim
- 0:38 – Setting a
requirements.txtfile as type Python
- 1:19 – Maybe not doing this for certain files like
- 2:30 – Using the
filecommand to gain insights about a specific file
- 3:38 – Taking a look at 2 autocmd config lines in my vimrc file
- 4:27 – You can also set just the syntax highlighting instead of the full file type
Which file types do you have special autocmd rules for? Let me know below.