Alex Rivera | Logout

Open current file in web browser in Vim

Asked 2012-01-03T05:05:00.250
20

I'd like to be able to send the HTML file I'm currently editing in vim to my web browser, Chrome. I'm running Linux.

This gets me very close:

nmap <silent> <leader>w :!google-chrome % &

The browser opens and displays the correct file. However, the command isn't running in the background, which is what the & is supposed to do. Instead vim drops to the background and I get some text output to my terminal window. I then have to do some clicking and Enter pressing to get my vim to come back to the foreground.

Am I missing something?

UPDATE: Thanks for all the suggestions. The issue seems to be that the focus seems to shift away from my terminal window when the browser window opens. Not sure if there's anything I can do about this.

vim
Edit
Report

2 Answers

10

From my .vimrc:

nnoremap <F12>f :exe ':silent !firefox %'<CR>
nnoremap <F12>c :exe ':silent !chromium-browser %'<CR>
nnoremap <F12>o :exe ':silent !opera %'<CR>

These three commands open the current file in the chosen browser without side effects. The use of <F12> makes it GVim-only, though.

answered 2012-01-03T07:02:38.300
3

You can exec chrome silently:

nmap <silent> <leader>w :exec 'silent !google-chrome % &'

:exe[cute] {expr1} .. Executes the string that results from the evaluation of {expr1} as an Ex command.

answered 2012-01-03T05:09:52.263

Your Answer