Question

I am learning html, want to edit a html file in vim ,
when i finished the html file, press F4 to run it with firefox or chrome,
how can i write a line in my _vimrc configuration file to do so ?
in my _vimrc :

:map <F4> :w !c:\Program Files\Mozilla Firefox\firefox.exe<cr>   
:map <F5> :w !firefox<cr>   

i have set my environment path
c:\Program Files\Mozilla Firefox\firefox.exe
neither of them can run ,i got can't find command.

when i revise the configuration line into :

:map <F4> :w !"c:\Program Files\Mozilla Firefox\firefox.exe"<cr> 

pressing F4 can make firefox open ,but the firefox don't open my current editing html file.

  "c:\Program Files\Mozilla Firefox\firefox.exe"    file:\\c:\workspace\test.html      

can open the test.html ,how can i set it in my _vimrc file?

Was it helpful?

Solution

If you are only using vi(m) in the console, this will work but you will have to hit enter once to get back to the html document within the editor:

nnoremap <F4> :exe '!"c:\Program Files\Mozilla Firefox\firefox.exe" %'<CR>

If you are using gvim, this will avoid having to hit enter that extra time:

nnoremap <F4> :exe ':silent !"c:\Program Files\Mozilla Firefox\firefox.exe" %'<CR>

OTHER TIPS

Your mapping:

:map <F4> :w !"c:\Program Files\Mozilla Firefox\firefox.exe"<cr> 

translates to (english): "write the content of this file to the stdin of this firefox program" which is most likely not what you wanted to do here.

What you want to do is:

  1. Save the current file.
  2. Run firefox with the name of the current file as its argument.

The path to firefox and the file name must both be shellescape()ed to deal with the possible spaces in the path names.

This mapping should do what you want:

:map <F4> :w\|exec '!' shellescape("c:/Program Files/Mozilla Firefox/firefox.exe") shellescape(expand(%))<cr>

The question you are looking for:

Open current file in web browser in Vim

This is exactly your issue.

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