HTML markup combines element tags with attributes that describe each element. Vim's built-in HTML omni completion suggests tag and attribute names through omnifunc, without a third-party plugin.

Vim keeps omnifunc settings separate for each buffer. Tying the HTML completion function to the HTML file type keeps it from replacing completion for other languages.

Omni completion runs on request in insert mode. The available suggestions depend on the cursor's position in the markup.

Step-by-step video guide:

Steps to configure HTML auto-completion in Vim:

  1. Open any HTML file in Vim.

    Use any existing or newly created `.html` file in Vim for testing. This helps ensure omnifunc is working correctly before modifying the configuration file.

  2. Manually set omnifunc to the HTML completion function.
    :set omnifunc=htmlcomplete#CompleteTags
  3. Enter insert mode.

    Press the `i` key to enter insert mode.

  4. Trigger omnifunc completion with <C-x><C-o>.
  5. Review and select the suggested completions.

    Use arrow keys to navigate the suggestions. Press `Enter` to select, or keep typing to refine the suggestions.

  6. If the completion works, open your vimrc configuration file.
    vim ~/.vimrc
  7. Add a new autocmd to enable omnifunc for HTML files.
    autocmd FileType html setlocal omnifunc=htmlcomplete#CompleteTags
  8. Save and close the vimrc file.

    To save changes, use `:wq` in Vim.

  9. Reopen any HTML file to verify the changes.

    Once reopened, omnifunc will automatically trigger for files with the `.html` extension.

  10. (Optional) Create a key mapping for easier triggering of omnifunc completion.
    inoremap <silent><expr> <Tab> pumvisible() ? "\<C-n>" : "\<C-x>\<C-o>"

    This key mapping allows you to use `<Tab>` to trigger omnifunc and navigate suggestions smoothly.

Share this guide