Vim Autocomplete HTML Tags
Vim has a built in library for autocompleting HTML tags, but it needs to be enabled to work. To enable it, we just have to create a FileType AutoCommand in our .vimrc
file, telling Vim to enable the completion function when we are in an HTML file:
autocmd FileType html set omnifunc=htmlcomplete#CompleteTags
If you use other templating languages with different file types, you can add those after html
separated by commas. Run :set filetype
to see the filetype of a given file if you’re unsure. For example, .erb
files from Ruby on Rails are typically type eruby
so you’d want autocmd FileType html,eruby
.
Once that is enabled, Vim autocompletes HTML tags when you run <ctrl-x><ctrl-o>
after typing <
inside one of the chosen file types.
Vim will even auto complete HTML doctype strings, arguably the most useful HTML tag to autocomplete, because who wants to type all that? There is also some built in context detection. For example, inside an <html>
tag, Vim will show only body
and head
completion options.
In the video below I show a short demonstration of Vim HTML tag auto-completion. You can see that after I type <
I press <ctrl-x>
followed by <ctrl-o>
and Vim provides an autocomplete popup up with the valid HTML tags for that context.
Be sure to check out our post on Vim file templates using skeleton files if you want to have boilerplate HTML generated automatically.
Can Vim autocomplete HTML tags?
set omnifunc=htmlcomplete#CompleteTags
Why doesn't Vim autocomplete HTML by default?
Can I autocomplete other language keywords in Vim?
How do I enable Vim HTML autocomplete?
.vimrc
file: autocmd FileType html set omnifunc=htmlcomplete#CompleteTags