Stuff in my vimrc: 02 File Extensions
When editing a file in Vim, you can choose which is its file type. Knowing the file type, Vim can load the correct syntax highlighting, and in some cases certain functionalities of plugins may be activated and used.
To have syntax highlighting, you need this setting:
After this, Vim will set the file type for any opened file automatically. So if you open a Python file, Vim will set the type to
The user can always change the current file type to anything else. A common case is setting the file type to
plaintext, which causes language specific syntax highlighting to go away but doesn’t shutdown the highlighting for other opened files:
File Extension Conflicts
Sometimes, a file may be opened in Vim, but the file type is interpreted incorrectly. In this case we have to set the correct type by hand.
Take the example of opening a Prolog file. The file extension of Prolog is
.pl. However, the extension for a Perl file is also
.pl. So when a
.pl file is open, regardless of it being a Prolog or Perl program, Vim assumes the Perl file type.
Vim assumes Perl simply because it is much more famous and highly used then Prolog. But for me personally, Prolog is more important. This is because I’ve made some development in Prolog in the passed, and have never actually used Perl for anything.
So every time I want to edit a Prolog file I would have to write:
But this would be extremely cumbersome to do. So I add an automatic command that executes every time a buffer which name ends in
.pl is created or read. That line looks like this:
autocmd BufNewFile,BufRead *.pl :set ft=prolog
The command is
autocmd, which is executed on the events
BufRead, for buffers which end in
.pl. The automatic command which is executed is exactly what we saw before:
Like this, Vim assumes file type Prolog for any
.pl file which I open.
File Extensions in my
At the time of writing, this section of my
vimrc looks like this:
" Make .pl files load like Prolog and not like Perl autocmd BufNewFile,BufRead *.pl :set ft=prolog " Make .tex files load like tex files. autocmd BufNewFile,BufRead *.tex :set ft=tex " Make .md files load like Markdown files. autocmd BufNewFile,BufRead *.md :set ft=markdown " Make .rl file load as Go autocmd BufNewFile,BufRead *.rl :set ft=go " Make Emakefile files load like Erlang files. autocmd BufNewFile,BufRead Emakefile :set ft=erlang " Make ejs file load as HTML autocmd BufNewFile,BufRead *.ejs :set ft=html " Make .docker files load as Dockefile autocmd BufNewFile,BufRead Dockerfile.* :set ft=dockerfile autocmd BufNewFile,BufRead *.docker :set ft=dockerfile
Without these rules,
.tex files would open as
plaintext instead of LaTeX, and
.md would also open as plain text instead of Markdown, although in recent versions of vim, they already open as Markdown out of the box.
The other rules I have are for specific library file types. We have the
.rl files which are from a Go library called Ragel. This library is used to make Flex typed Lexers (to build parsers or compilers). I never actually used this library in a big project, but I’ve made experiments with it. Despite having a specific file name, the code contained in a Ragel file is Go, so my Vim will open these files as Go files.
I’ve made some experiments with Erlang, and I like the language quite a lot. The
Emakefile is a specific type of
Makefile to compile an Erlang program to the Beam Virtual Machine. Like Ragel, the code in these files is simply Erlang, hence the rule.
I want to see the
ejs files, but since the templating part was so simple, it was also not needed.
Finally, the last part of this section is about Docker files. I’ve seen and worked on many projects that used files with names starting with
Dockerfile. and others ending with
.docker. I naturally have rules to make these files open with the file type