Stuff in my vimrc: 02 File Extensions

In my previous post I wrote about the general settings in my vimrc. I continue to explore my file, hosted here. In this post, I’ll be talking about File Extensions.

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:

syntax enable

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 python, a JavaScript file will be javascript, and so on.

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:

set ft=plaintext

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:

set ft=prolog

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 BufNewFile or BufRead, for buffers which end in .pl. The automatic command which is executed is exactly what we saw before:

set ft=prolog

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.

In a personal project I’ve made two years ago, I’ve used ejs, Embedded JavaScript Templating. This is a Node.js templating library which I used to generate static HTML pages for a simple web application. The library is very similar to other templating libraries, such as Django Templates, Go Temples, Java Spring Templates, and others.

I want to see the ejs files as HTML, despite the fact that some of its code (the actual template code) is JavaScript. When I worked with this, there was no way of having syntax highlighting for HTML and JavaScript at the same time in an 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 Dockerfile.

<- Prev – 01 General Options | Next – 03 Folds and why I don’t use them ->

Tags:

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: