Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open current file in web browser in Vim

Tags:

vim

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.

like image 769
Josh Earl Avatar asked Jan 03 '12 05:01

Josh Earl


People also ask

How do I open a file in Vim terminal?

Launching Vim In order to launch Vim, open a terminal, and type the command vim . You can also open a file by specifying a name: vim foo. txt .

How do I open Explorer in Vim?

The command :Explore opens the file explorer window. Select a file or directory name and press Enter to open that file or directory. (For example :e /home/user displays the contents of that directory.) To return to the explorer window, press Ctrl-^ (usually Ctrl-6).


1 Answers

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.

like image 185
romainl Avatar answered Sep 30 '22 02:09

romainl