Learn Docker With My Newest Course

Dive into Docker takes you from "What is Docker?" to confidently applying Docker to your own projects. It's packed with best practices and examples. Start Learning Docker →

Using Vim's autocmd to Set a Custom File Type for Specific Files

blog/cards/using-vims-autocmd-to-set-a-custom-file-for-specific-files.jpg

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.

Demo Video

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

Timestamps

  • 0:09 – Making sure an .aliases file gets set as a shell script according to Vim
  • 0:38 – Setting a requirements.txt file as type Python
  • 1:19 – Maybe not doing this for certain files like .env vs .env.example
  • 2:30 – Using the file command 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.

Never Miss a Tip, Trick or Tutorial

Like you, I'm super protective of my inbox, so don't worry about getting spammed. You can expect a few emails per month (at most), and you can 1-click unsubscribe at any time. See what else you'll get too.



Comments