how to edit the _wimrc to open html file with firefox in vim?

自闭症网瘾萝莉.ら 提交于 2020-01-14 14:51:49

问题


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?


回答1:


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>



回答2:


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>



回答3:


The question you are looking for:

Open current file in web browser in Vim

This is exactly your issue.



来源:https://stackoverflow.com/questions/20941824/how-to-edit-the-wimrc-to-open-html-file-with-firefox-in-vim

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!