Question

I'm trying to get vim to send an html file to the browser with a function key. There are examples around including here on SO, but none of the variations I tried worked. Finally, just as I write this question, I remove the filetype condition from the code below and it works. So we know where the problem is, although I don't see how. And I still want to want the key to work differently for different types of files.

autocmd FileType html noremap <F5> :update<Bar>silent !start "C:\Program Files (x86)\Opera\opera.exe" "file://%:p"<cr>
Was it helpful?

Solution

If you define a mapping based on filetype, you should use noremap <buffer> ...; otherwise, it will be defined globally, and also be available in non-HTML buffers afterwards.

The mapping itself looks correct; verify that the filetype is indeed properly detected; :setl ft? should yield html.

Instead of the autocmd, I'd rather use Vim's abstractions and put the file in ~/.vim/after/ftplugin/html.vim. This will then also define the mapping for HTML-derived filetypes (like e.g. htmldjango, which source the filetype settings). But that's a matter of preference.

Note that there are plugins that can do this (in a more robust and more general way), e.g. shell for the current buffer and openbrowser for arbitrary URLs.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top