1

Is the following a properly scoped autocommand to work on HTML files?

" Option-space as   in html
augroup HTML
    autocmd!
    autocmd FileType html,htmldjango inoremap <buffer> <leader><space> &nbsp;
augroup END

If not, what additional do I need to add. Not that this is currently in my vimrc.

  • Please define what you mean by "properly scoped", can you be more explicit on what it is that you'd like to confirm it's correct? – filbranden Jun 27 at 4:12
  • 1
    @filbranden basically that it won't apply to any non-html files after I open an html file one time. i.e., it won't pollute things afterwards. – David542 Jun 27 at 4:12
2

Yes, this block looks fine.

When you use autocmd's, put them in an augroup block with a unique name and start that block with an autocmd!, which will erase all autocmd's in that group. This way, if your Vimscript file is reloaded, the autocmd's will not be duplicated. (If you define them more than once, the commands will run multiple times.)

Regarding the mapping, you should use a <buffer> mapping in filetype autocmd's, so that these mappings apply only to files of that type. So that's correct too. It's also good that the mapping is non-recursive, you should typically write your mappings to be non-recursive, by default, except on the few cases where you really need them to be recursive.

It's a bit unusual to use <Leader> on an imap, since <Leader> is typically a printable character, \ by default, but often set to , or ; or even <Space>. This might make the insert-mode mapping a little awkward, particularly when you actually want to insert the <Leader> character, or some possible settings for <Leader> might make this mapping fully unusable... The map leader is typically meant for normal, visual or operator-pending mappings, not really for insert mode.

Your mapping will only be set on html and htmldjango filetypes... However, there's one case where they might "leak" into other filetypes, which is the case where you have a file take one of these filetypes, but then you switch the filetype to something else. In that case, the mapping will not be removed.

A better solution for adding a mapping for HTML filetypes is to create a ~/.vim/after/ftplugin/html.vim file and add the mapping there.

As a bonus, you don't need to worry about filetypes such as htmldjango, since ftplugin/htmldjango.vim sources ftplugin/html.vim and that will include your custom extension ftplugin/html.vim.

There are actually other languages which source HTML as well, so by defining your own ftplugin, all those others will be indirectly affected too. For example, php sources html too.

One final advantage of writing an ftplugin worth mentioning is that it offers a mechanism to undo the mapping when switching filetype of an existing buffer, through the b:undo_ftplugin variable.

Putting it all together, and using <C-Space> for the mapping (assuming that mapping works in your setup), you can handle this by creating file ~/.vim/after/ftplugin/html.vim with the following contents:

if !exists("b:undo_ftplugin")
  let b:undo_ftplugin = ""
endif
let b:undo_ftplugin .= "|iunmap <buffer> <C-Space>"
inoremap <buffer> <C-Space> &nbsp;
| improve this answer | |
  • 1
    PS markdown loads the html filetype too – D. Ben Knoble Jun 27 at 12:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.