Open current file in web browser in Vim

前端 未结 9 540
自闭症患者
自闭症患者 2020-12-24 15:01

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:

相关标签:
9条回答
  • 2020-12-24 15:33

    Here are various tips on previewing files in your browser from Linux, Windows, and Mac OS X. Preview current HTML file

    This mapping allows you to use a browser to preview the HTML file currently being edited.

     nnoremap <F5> :silent update<Bar>silent !firefox %:p &<CR>
    

    If necessary, the current file is saved, then Firefox is used to open the file. Replace "firefox" with the name of another browser if wanted, such as "chromium-browser".

    So as the question states that Chrome is the desired browser, then add this to your vimrc:

    nnoremap <F5> :silent update<Bar>silent !google-chrome %:p &<CR>
    

    As well as answer the OP, I wanted to share this link for those that reached this question in search of Windows and Mac OS X support.

    0 讨论(0)
  • 2020-12-24 15:35

    For windows7 x64, - F2 to open current file, - F3 to lookup current cword under cursor.

    for g:chrome_exe in [
        \'C:\Progra~2\Google\Chrome\Application\chrome.exe',
        \'C:\Progra~1\Google\Chrome\Application\chrome.exe',
        \'C:\Progra~1\Google\Chrome\Applic~1\chrome.exe',
        \'C:\Users\'.$USERNAME.'\AppData\Local\Google\Chrome\Application\chrome.exe' 
        \]
        if !filereadable(g:chrome_exe)
            continue
        endif
        let g:google_url='https://www.google.com/search?oq=&gws_rd=cr&dcr=0&ei=0&q='
        :nmap   <F2> :exe ":!start ".g:chrome_exe." --user-data-dir=%TEMP%/chrome-vim-sandbox file:///".expand("%:p")<CR>
        :nmap   <F3> :exe ":!start ".g:chrome_exe." --user-data-dir=%TEMP%/chrome-vim-sandbox ".g:google_url.expand("<cword>")<CR>
        break
    endfor 
    
    0 讨论(0)
  • 2020-12-24 15:37

    For Mac, you might have to type something that looks more like this:

    nnoremap <F12>f :exe ':silent !open -a /Applications/Firefox.app %'<CR>
    nnoremap <F12>c :exe ':silent !open -a /Applications/Google\ Chrome.app %'<CR>
    nnoremap <F12>g :exe ':silent !open -a /Applications/Google\ Chrome.app %'<CR>
    nnoremap <F12>s :exe ':silent !open /Applications/Safari.app %'<CR>
    

    Notce it's Safari.app on the end...it'll save you a few minutes. :)

    0 讨论(0)
提交回复
热议问题