• +43 660 1453541
  • contact@germaniumhq.com

Vim Ultimate Editing: Multiple File Types


Vim Ultimate Editing: Multiple File Types

As I mentioned in my previous article, were multiple reasons why I chose Vim over Sublime. I wanted to focus on UltiSnips, but I realized that some ground work needs to be set first. So as the title implies, we’re looking today at Vim and multiple file types.

In Vim, unlike any other editor that I know of, a buffer (an edited file) can have multiple file types. In the "UI" of Vim you see at the bottom the filetype. Usually this is a single word such as: groovy, java, or markdown. What most people don’t know is that a file can have multiple types.

So you can have a file with a filetype such as: X_Jenkinsfile.Jenkinsfile.groovy. This is a file that has 3 types: X_Jenkinsfile, Jenkinsfile and groovy. If there are file type plugins configured for those file types, all those three plugins would be loaded. The last file type is used for syntax highlighting.

You can actually simply change the filetype for the current buffer by just writing:

:set filetype=a.b.c.d.yaml

Now your file has 5 distinct file types: a, b, c, d, and yaml.

A thing that I like to do, for things such as yaml files is to define commands that toggle the filetype by adding the specific type that I’m editing:

function! Ansible()
    if &filetype =~ 'X_Ansible'
        let l:newType = substitute(&filetype, 'X_Ansible\.', '', '')
    else
        let l:newType =  'X_Ansible.' . &filetype
    endif

    execute 'set filetype=' . l:newType
endfunction
command Ansible call Ansible()

So if I edit an ansible playbook or role, I can just toggle it by calling:

:Ansible

The command will prepend the filetype with the X_Ansible type, so now my buffer (currently edited file) will become: X_Ansible.yaml. So it will have two types: X_Ansible, and yaml. All the other opened files in Vim will still have just the basic yaml type.

Conclusion

You can use multiple file types to declare specific types of content in the overall markup that is used for a container. For example bootstrap3.jquery.html or purecss.html allow you to differentiate on the content, even if both of them are in html files. Plugins can use the multiple file types to enable/disable features in the editing.