0

I added 4 following lines to my vimrc file to add shortcuts (to run current *.html buffer in browser and to run current *.js file in node):

autocmd filetype html map <C-S-F10> :sav %<CR>:! chromium-browser %<Enter>
autocmd filetype html inoremap <C-S-F10> <Esc>:sav %<CR>:! chromium-browser %<Enter>
autocmd filetype javascript map <C-S-F10> :sav %<CR>:! node %<Enter>
autocmd filetype javascript inoremap <C-S-F10> <Esc>:sav %<CR>:! node %<Enter>

It works fine when I have only html files or only JavaScript files open. But when I have any combination of these file types open in different buffers (using tabs) and want to open HTML file in browser and press Ctrl+Shift+F10 my HTML file goes to Node, which throws compilation error.

Progress:

When I type :set filetype it says html for *.html and javascript for *.js files. So the problem is not in improper file type.

I tried to set Alt+Shift+F10 shortcut for opening in browser and that somehow solves my problem. Now, when both filetypes plus files without filetype are open at the same time I can open any (html, js and plain text) filetype in browser with Alt+Shift+F10 and send it to Node to compile with Ctrl+Shift+F10. Why it happens?

Here my vimrc file. Vim version is 7.4

0

You're better off adding filetype plugins to the after section of your user vim runtime directory, and defining your mappings there without using autocmd.

For example, in ~/.vim/after/ftplugin/html.vim you would put your HTML mappings, and ~/.vim/after/ftplugin/javascript.vim for JavaScript mappings.

However, that won't solve your problem. You need to make your mappings buffer-local. Try this, for HTML buffers:

map <buffer> <C-S-F10> :sav %<CR>:! chromium-browser %<Enter>
inoremap <buffer> <C-S-F10> <Esc>:sav %<CR>:! chromium-browser %<Enter>

And this, for JavaScript buffers:

map <buffer> <C-S-F10> :sav %<CR>:! node %<Enter>
inoremap <buffer> <C-S-F10> <Esc>:sav %<CR>:! node %<Enter>

See:

:help :map-local
  • Thanks, your solution worked. "You're better off adding filetype plugins to the after section of your user vim runtime directory" - what advantages except not bloating vimrc file it gives? I thought that it kind of more convenient to have all my configuration in one place. – Alexander Jan 18 '16 at 19:39
  • Mainly you know for sure that your mappings will be defined after all the system stuff has been sourced, so yours can override the system stuff where necessary. If you use an autocmd there's no guarantee it will happen in that order (and, in fact, probably won't). – Heptite Jan 18 '16 at 19:53

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.