Vim Tips Wiki
m (Reverted edits by 139.194.131.22 (talk | block) to last version by Fritzophrenic)
 
(28 intermediate revisions by 9 users not shown)
Line 1: Line 1:
{{Duplicate|256|587|684|1015}}
 
{{review}}
 
 
{{TipImported
 
{{TipImported
 
|id=127
 
|id=127
 
|previous=126
 
|previous=126
 
|next=128
 
|next=128
|created=October 4, 2001
+
|created=2001
 
|complexity=basic
 
|complexity=basic
|author=Jamis Buck
+
|author=
 
|version=6.0
 
|version=6.0
 
|rating=4/12
 
|rating=4/12
Line 13: Line 11:
 
|category2=
 
|category2=
 
}}
 
}}
I've found while writing HTML files that it can become cumbersome when I have to switch to a web browser, load my page, and move back to VIM regularly to preview what I've written. I've come up with the following tricks.
+
This tip shows how to use Vim to view an html file, or a web page, in a web browser. If wanted, a text-based web browser can be used to read the text from an html page into a scratch buffer in Vim.
 
The first one requires that you have lynx (the text-based browser) installed on your computer (available from http://lynx.isc.org/release/). If your HTML page is primarily text, with few (if any) images, you can set up the following function and mapping:
 
   
  +
==Viewing text from an html file or a URL==
  +
A text-based web browser such as [[wikipedia:Elinks|<code>elinks</code>]] can extract a formatted view of the text from an html file or a web page. The following shows how to use elinks to read that text into a scratch buffer. You might do that for a quick preview, or to copy text from the displayed html page.
 
<pre>
 
<pre>
function PreviewHTML_TextOnly()
+
function! ViewHtmlText(url)
 
if !empty(a:url)
let l:fname = expand("%:p" )
 
new
+
new
set buftype=nofile nonumber
+
setlocal buftype=nofile bufhidden=hide noswapfile
exe "%!lynx " . l:fname . " -dump -nolist -underscore -width " . winwidth( 0 )
+
execute 'r !elinks ' . a:url . ' -dump -dump-width ' . winwidth(0)
  +
1d
 
endif
 
endfunction
 
endfunction
  +
" Save and view text for current html file.
map &lt;Leader&gt;pt :call PreviewHTML_TextOnly()&lt;CR&gt;
 
  +
nnoremap <Leader>H :update<Bar>call ViewHtmlText(expand('%:p'))<CR>
 
" View text for visually selected url.
 
vnoremap <Leader>h y:call ViewHtmlText(@@)<CR>
 
" View text for URL from clipboard.
  +
" On Linux, use @* for current selection or @+ for text in clipboard.
 
nnoremap <Leader>h :call ViewHtmlText(@+)<CR>
 
</pre>
 
</pre>
   
  +
After sourcing the above, and assuming the default backslash Leader key, you can:
This will open a new window and display your formatted HTML document in that window. Note that bold-face, italics, links, etc. will be lost -- all you will see is the text -- but the "-underscore" parameter to Lynx causes any text that would have been bold, italicized, or underlined to be displayed like _this_.
 
  +
*Edit an html file, then type <code>\H</code> to save and preview the file.
  +
*Visually select the full path of a local html file or a URL, then type <code>\h</code> to preview the file or web page.
  +
*Copy the full path of a local html file or a URL in another application, then type <code>\h</code> to preview the file or web page in Vim.
   
  +
==From 256 [[Opening current Vim file in your Windows browser]]==
The other trick requires that Vim be running on your current machine, and that you be running a GUI of some sort (X-Windows, Windows, etc.). You can cause vim to invoke your favorite browser and have it display the file, like this:
 
  +
*''TODO'': Finish cleaning following notes.
  +
On Windows, using <code><cWORD></code> to open the link at the cursor in a browser does not work when the URL is in an html tag surrounded by quotes. However, using <code><cfile></code> works for both situations: plain URL and URL between html tags.
   
 
<pre>
 
<pre>
  +
" On Windows, open URL under cursor.
function PreviewHTML_External()
 
  +
nnoremap <F8> :!start C:\progra~1\intern~1\iexplore.exe <cfile><CR>
exe "silent !mozilla -remote \"openurl(file://"; . expand( "%:p" ) . ")\""
 
  +
nnoremap <F8> :!start C:\progra~1\mozill~1\firefox.exe <cfile><CR>
endfunction
 
  +
" On Windows, open current file in Internet Explorer.
map &lt;Leader&gt;pp :call PreviewHTML_External()&lt;CR&gt;
 
  +
nnoremap <F5> :update<Bar>silent !start c:\progra~1\intern~1\iexplore.exe file://%:p<CR>
  +
" On Linux, open URL under cursor in Firefox.
  +
nnoremap <F8> :silent !firefox <cfile><CR>
 
</pre>
 
</pre>
   
  +
In the above, there is no space after <code>!</code> and the Vim command has the form <code>:!start program file</code> which causes Vim (on Windows) to run the specified program with the file as an argument. If the program is not in a PATH directory, the full path to the program must be specified. If the program path contains spaces, it is necessary to put the path in quotes (<code>"..."</code>).
If you don't use mozilla, you will need to modify the function to use your preferred browser.
 
   
 
----
==Comments==
 
  +
If using <cfile> or <cWORD> does not suit a particular requirement, it is possible to use a pattern to select what is needed. For example, the following takes the first match on the current line which starts with 'http' and continues with non-whitespace characters (it finishes at the first space or tab, or at the end of the line):
Better with dillo, a really fast an light browser... a graphical lynx in some sort.
 
  +
<pre>
See http://dillo.sourceforge.net or "apt-get install dillo" for debian users.
 
  +
nnoremap <F8> :execute 'silent ! start '.matchstr(getline('.'), 'http\S*')<CR>
  +
</pre>
   
 
----
 
----
  +
On Windows 7 this opens the current file in Chrome (fix the path by replacing "MyName" with what it should be on your system).
It gives me an error when I try it:
 
  +
<pre>
  +
nnoremap <silent> <F5> :update<Bar>silent !start "C:\Users\MyName\AppData\Local\Google\Chrome\Application\chrome.exe" "file://%:p"<CR>
  +
</pre>
  +
Or it might be here:
  +
<pre>
  +
nnoremap <silent> <F5> :update<Bar>silent !start "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" "file://%:p"<CR>
  +
</pre>
 
----
  +
On Linux, use <code>xdg-open</code> to open the file using its default application. The first of the following mappings saves the current file, then opens it; the second opens the file under the cursor:
  +
<pre>
  +
nnoremap <F5> :update<Bar>silent !xdg-open %:p &<CR>
  +
nnoremap <F8> :silent !xdg-open <cfile> &<CR>
  +
</pre>
   
  +
==From 587 [[Preview current file in Mozilla through localhost]]==
E15: Invalid expression: ; . expand( "%:p") . ")\""
 
  +
When editing an HTML file on Windows, enter the following to save the current file and open it in your default browser:
  +
<pre>
  +
:update|silent ! start %:p
  +
</pre>
   
  +
On Windows, the current file can be opened in its associated application using the <code>start</code> command of the <code>cmd.exe</code> shell. With this mapping, press F5 to save the current file and open it in its associated application:
----
 
  +
<pre>
You can also open visually selected url or clipboard content by modifying the first few line from
 
  +
nnoremap <silent> <F5> :update<Bar>silent ! start %:p<CR>
function PreviewHTML_TextOnly()
 
  +
</pre>
let l:fname = expand("%:p" )
 
   
  +
The space after <code>!</code> is required (on Windows, a command of the form <code>!start xxx</code> causes Vim to run <code>xxx</code> asynchronously; Vim does ''not'' run <code>start</code>, whereas <code>! start xxx</code> invokes the shell <code>cmd.exe</code> to run its <code>start</code> internal command operating on <code>xxx</code>). In the unlikely case that the associated application runs in a console (command prompt window), it would be better to add the <code>/b</code> option so the application runs in the same console as <code>start</code>. That is, change the command to <code>start /b %:p</code>.
to
 
function PreviewHTML_TextOnly(...)
 
let l:fname = a:1
 
   
  +
It is convenient to leave the browser window open: switch back to Vim to do any further editing required, save the file, then switch to the browser and tell it to reload the file with a key like Ctrl-R or F5.
Then add the following map to your .vimrc
 
   
  +
A Linux version of the above mapping would use a program like <code>xdg-open</code>:
 
<pre>
 
<pre>
  +
nnoremap <F5> :update<Bar>silent !xdg-open %:p &<CR>
"Open visually selected url
 
  +
nnoremap <F5> :silent update<Bar>silent !xdg-open %:p &<CR>
vmap ,h "ey:call ViewHTML(@e)&lt;CR&gt;
 
"Open url from Mac OS X clipboard
 
nmap ,h :r!pbpaste&lt;CR&gt;"ey$:call ViewHTML(@e)&lt;CR&gt;
 
"Open url from Windows clipboard
 
nmap ,h :call ViewHTML(@*)&lt;CR&gt;
 
 
</pre>
 
</pre>
   
  +
==From 684 [[Preview current HTML in browser on Mac OS X]]==
----
 
  +
*''TODO'': Clean following.
Proposition of merge this tip with [[Open content of website in new buffer (using elinks text web browser)]]<br /><br />
 
  +
There are a few tips on previewing current HTML documents in a Windows browser, but none I could find for Mac OS X. By studying the others, though, I stumbled on a mapping that works. The <CR> at the end anticipates the "Hit ENTER or type command to continue" message.
There is often very handy to load snapshot from text web-browser into Vim buffer (when you want to preview html file which you are currently writing or open some website with documentation/manual/tutorial, so you can easily read it and copy some code snapshot from it to vim). This function and mappings listed below allows you to do this very quickly (you can also use the very nice browser plugin {{script|id=1053}}, but it requires a <tt>+perl</tt> compilation of Vim).
 
 
Function is very simple and contains two variants, one using elinks and second using lynx, so you can choose which web browser you prefer:
 
   
 
<pre>
 
<pre>
  +
nnoremap <F5> :!open -a Safari %<CR><CR>
function ViewHTML(url)
 
if a:url == ""
 
return
 
endif
 
 
new
 
set buftype=nofile nonumber
 
"using lynx
 
"exe "%!lynx " . a:url . " -dump -nolist -underscore -width " . winwidth(0)
 
"usnig elinks
 
exe "%!elinks " . a:url . " -dump -dump-width " . winwidth(0)
 
endfunc
 
 
</pre>
 
</pre>
   
  +
;Comments
Proposed mappings are:
 
  +
To expand on this:
  +
  +
general -- "open x" will open "x" with the default application ... "open -a applicationName x" will open "x" with application "applicationName"
  +
  +
general -- "<D-aKey>" creates a shortcut using the Command (Apple) key
  +
  +
improved(?) -- "nnoremap <silent> <D-p>f :exe ':silent !open -a /Applications/Path/To/Firefox.app %'<CR>"
  +
  +
(prevents remapping, runs quietly, shortcut is now [Command+p,f] for "preview, firefox" ... helpful if you preview in multiple browsers)
  +
  +
improved(?) -- "nnoremap <silent> <D-p>p :exe ':silent !open %'<CR>"
  +
  +
(same, but creates a generic "preview" using default applications ... helpful if you only preview in that browser, or can use it on other file types)
  +
  +
other (open directory in Finder) -- "nnoremap <silent> <D-p>d :exe ':silent !open -a finder %:p:h'<CR>"
  +
  +
(%:p:h completely expands the file path and removes the file name so only directory is left)
  +
  +
other (apply same to netrw browser, ie, :Ex command) -- add to .vimrc/.gvimrc: "let g:netrw_browsex_viewer = 'open'" (this is not necessary starting with 7.2, netrw now uses "open" as the default for "x").
  +
  +
(when using netrw browser, hit "x" while cursor is on file name to open that file with the default application ... if the cursor is over a directory, it will open in Finder)
   
  +
Also, for Mac users, here is a way to save the current PHP file then preview it through localhost (''TODO'' see <code>%:p:s?pat?sub?</code> below for Vim procedure):
 
<pre>
 
<pre>
  +
nnoremap <F5> :update<Bar>!open -a Google\ Chrome `echo http://localhost/${PWD\#*/*/*/*/*/}/%`<CR>
"Preview currently edited html file
 
  +
</pre>
nmap <Leader>H :call ViewHtml(@%)<CR>
 
   
  +
==From 1015 [[Preview file on localhost]]==
"Open visually selected url
 
  +
Using this mapping will save the current file (if changed), then preview it on localhost. For example, if you are editing file <code>/var/www/html/one/two/my.html</code>, pressing F5 will use Firefox to open <code><nowiki>http://localhost/one/two/my.html</nowiki></code>.
vmap <Leader>h "ey:call ViewHTML(@e)&lt;CR&gt;
 
  +
<pre>
  +
nnoremap <F5> :silent update<Bar>silent !firefox %:p:s?\(.\{-}/\)\{4}?http://localhost/?<CR>
  +
</pre>
   
  +
The core command has the form <code>!firefox %:p:s?pat?sub?</code> which uses the shell to start Firefox to open the current file (<code>%</code>) with its name modified by forming the full path (<code>:p</code>) then substituting the pattern <code>pat</code> with <code>sub</code>.
"Open url from Linux or Windows clipboard
 
  +
nmap <Leader>h :call ViewHTML(@*)&lt;CR&gt;
 
  +
The pattern is <code>\(.\{-}/\)\{4}</code> which finds exactly four occurrences of any character (<code>.</code>) repeated as few times as possible (<code>\{-}</code>) up to and including the next slash (<code>/</code>). If the file being edited is <code>my.html</code> in directory <code>/var/www/html/one/two</code>, the pattern matches <code>/var/www/html/</code> (the first occurrence of four slashes, with any characters between).
"Open url from Mac OS X clipboard
 
  +
nmap <Leader>h :r!pbpaste&lt;CR&gt;"ey$:call ViewHTML(@e)&lt;CR&gt;
 
  +
==From 201110 [[Preview current HTML in browser on Linux]]==
  +
This mapping allows you to use a browser to preview the HTML file currently being edited.
  +
<pre>
  +
nnoremap <F5> :silent update<Bar>silent !firefox %:p &<CR>
 
</pre>
 
</pre>
   
  +
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".
[[User:Paluh|Paluh]] 10:33, 7 May 2008 (UTC)
 
  +
  +
==Open an HTML file in a browser==
  +
:''TODO: Put a link somewhere in this tip to the result of merging all the stuff on opening HTML files.''
  +
  +
==See also==
  +
'''Open web browser to display URL under cursor'''
  +
*[[VimTip306|306 Open a web-browser with the URL in the current line]]
  +
*[[VimTip555|555 Vim as bookmark manager]]
  +
*[[VimTip732|732 Quick launch html and other Windows documents]]
  +
*[[VimTip1549|1549 Execute external programs asynchronously under Windows]] launch browser or document in associated app
  +
*[[VimTip1656|1656 Automatically refresh display of html on saving file]] uses MozRepl addon for Firefox
  +
  +
'''Display information for word under cursor in a web browser'''
  +
*[[VimTip394|394 Internet search for the current word]]
  +
*[[VimTip598|598 PHP online help]]
  +
*[[VimTip896|896 Lookup the city and state of a given US Zip code using TCL]]
  +
*[[VimTip922|922 Launch lynx to get info for the current word]]
  +
*[[VimTip926|926 QT Help from Vim]]
  +
*[[VimTip933|933 Search the web for text selected in Vim]]
  +
*[[VimTip1200|1200 View PHP documentation for current word]]
  +
*[[VimTip1354|1354 Online documentation for word under cursor]]
  +
*[[VimTip1377|1377 Context sensitive access to MSDN help]]
  +
  +
'''Configure so "view source" opens in Vim'''
  +
*[[VimTip118|118 Use gvim to view page source in Internet Explorer]]
  +
*[[VimTip134|134 View Source in IE6 using VIM]]
  +
*[[VimTip581|581 Using vim to view source and edit textarea in mozilla/firebird]]
  +
*[[VimTip805|805 Use gvim as an external editor for Windows apps]]
  +
*[[VimTip1156|1156 View Source from Internet Explorer in gvim]]
  +
  +
'''Other'''
  +
*[[VimTip317|317 Vim key bindings for Firefox]]
  +
*[[VimTip691|691 Use gf to open a file via its URL]]
  +
*[[VimTip305|305 Best Vim Tips]] has a launching section
  +
*[[VimTip1440|1440 Launch files in new tabs under Windows]]
  +
*[[VimTip1552|1552 Launch files in new tabs under Unix]]
  +
*[[VimTip1652|1652 Launch remote batch jobs and view results in Vim]]
  +
*[[VimTip299|299 Open file under cursor]]
  +
*[[VimTip485|485 Open a window with the man page for the word under the cursor]]
  +
*[[VimTip506|506 Open Windows Help files on a specific topic]]
  +
*[[VimTip557|557 Opening several files in vim via ListFile]]
  +
*[[VimTip1356|1356 Open PDF files]]
  +
 
==Comments==
  +
I have removed the "Related plugins" section as the only plugin listed is obsolete and relies on Perl to do what can be done in plain Vim. [[User:JohnBeckett|JohnBeckett]] 10:16, April 18, 2012 (UTC)
  +
----
  +
I am working on merging a lot of tips related to opening HTML files in browsers, and documents in associated applications. I don't think anything more will be merged in to here. I will soon clean all this up and see how large the result is (some of it might then be moved elsewhere). [[User:JohnBeckett|JohnBeckett]] 11:21, April 26, 2012 (UTC)
  +
----
  +
In mappings, I am using F5 to open the current file and F8 to open the file under the cursor. Might rethink that after all merging complete. [[User:JohnBeckett|JohnBeckett]] 23:56, April 26, 2012 (UTC)
  +
----
  +
''TODO'': Probably need <code>shellescape()</code> or at least quotes in some of the mappings to handle embedded spaces. I think the Linux mappings also need <code>&</code> appended. [[User:JohnBeckett|JohnBeckett]] 05:01, April 27, 2012 (UTC)

Latest revision as of 08:14, 23 May 2015

Tip 127 Printable Monobook Previous Next

created 2001 · complexity basic · version 6.0


This tip shows how to use Vim to view an html file, or a web page, in a web browser. If wanted, a text-based web browser can be used to read the text from an html page into a scratch buffer in Vim.

Viewing text from an html file or a URL[]

A text-based web browser such as elinks can extract a formatted view of the text from an html file or a web page. The following shows how to use elinks to read that text into a scratch buffer. You might do that for a quick preview, or to copy text from the displayed html page.

function! ViewHtmlText(url)
  if !empty(a:url)
    new
    setlocal buftype=nofile bufhidden=hide noswapfile
    execute 'r !elinks ' . a:url . ' -dump -dump-width ' . winwidth(0)
    1d
  endif
endfunction
" Save and view text for current html file.
nnoremap <Leader>H :update<Bar>call ViewHtmlText(expand('%:p'))<CR>
" View text for visually selected url.
vnoremap <Leader>h y:call ViewHtmlText(@@)<CR>
" View text for URL from clipboard.
" On Linux, use @* for current selection or @+ for text in clipboard.
nnoremap <Leader>h :call ViewHtmlText(@+)<CR>

After sourcing the above, and assuming the default backslash Leader key, you can:

  • Edit an html file, then type \H to save and preview the file.
  • Visually select the full path of a local html file or a URL, then type \h to preview the file or web page.
  • Copy the full path of a local html file or a URL in another application, then type \h to preview the file or web page in Vim.

From 256 Opening current Vim file in your Windows browser[]

  • TODO: Finish cleaning following notes.

On Windows, using <cWORD> to open the link at the cursor in a browser does not work when the URL is in an html tag surrounded by quotes. However, using <cfile> works for both situations: plain URL and URL between html tags.

" On Windows, open URL under cursor.
nnoremap <F8> :!start C:\progra~1\intern~1\iexplore.exe <cfile><CR>
nnoremap <F8> :!start C:\progra~1\mozill~1\firefox.exe <cfile><CR>
" On Windows, open current file in Internet Explorer.
nnoremap <F5> :update<Bar>silent !start c:\progra~1\intern~1\iexplore.exe file://%:p<CR>
" On Linux, open URL under cursor in Firefox.
nnoremap <F8> :silent !firefox <cfile><CR>

In the above, there is no space after ! and the Vim command has the form :!start program file which causes Vim (on Windows) to run the specified program with the file as an argument. If the program is not in a PATH directory, the full path to the program must be specified. If the program path contains spaces, it is necessary to put the path in quotes ("...").


If using <cfile> or <cWORD> does not suit a particular requirement, it is possible to use a pattern to select what is needed. For example, the following takes the first match on the current line which starts with 'http' and continues with non-whitespace characters (it finishes at the first space or tab, or at the end of the line):

nnoremap <F8> :execute 'silent ! start '.matchstr(getline('.'), 'http\S*')<CR>

On Windows 7 this opens the current file in Chrome (fix the path by replacing "MyName" with what it should be on your system).

nnoremap <silent> <F5> :update<Bar>silent !start "C:\Users\MyName\AppData\Local\Google\Chrome\Application\chrome.exe" "file://%:p"<CR>

Or it might be here:

nnoremap <silent> <F5> :update<Bar>silent !start "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" "file://%:p"<CR>

On Linux, use xdg-open to open the file using its default application. The first of the following mappings saves the current file, then opens it; the second opens the file under the cursor:

nnoremap <F5> :update<Bar>silent !xdg-open %:p &<CR>
nnoremap <F8> :silent !xdg-open <cfile> &<CR>

From 587 Preview current file in Mozilla through localhost[]

When editing an HTML file on Windows, enter the following to save the current file and open it in your default browser:

:update|silent ! start %:p

On Windows, the current file can be opened in its associated application using the start command of the cmd.exe shell. With this mapping, press F5 to save the current file and open it in its associated application:

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

The space after ! is required (on Windows, a command of the form !start xxx causes Vim to run xxx asynchronously; Vim does not run start, whereas ! start xxx invokes the shell cmd.exe to run its start internal command operating on xxx). In the unlikely case that the associated application runs in a console (command prompt window), it would be better to add the /b option so the application runs in the same console as start. That is, change the command to start /b %:p.

It is convenient to leave the browser window open: switch back to Vim to do any further editing required, save the file, then switch to the browser and tell it to reload the file with a key like Ctrl-R or F5.

A Linux version of the above mapping would use a program like xdg-open:

nnoremap <F5> :update<Bar>silent !xdg-open %:p &<CR>
nnoremap <F5> :silent update<Bar>silent !xdg-open %:p &<CR>

From 684 Preview current HTML in browser on Mac OS X[]

  • TODO: Clean following.

There are a few tips on previewing current HTML documents in a Windows browser, but none I could find for Mac OS X. By studying the others, though, I stumbled on a mapping that works. The <CR> at the end anticipates the "Hit ENTER or type command to continue" message.

nnoremap <F5> :!open -a Safari %<CR><CR>
Comments

To expand on this:

general -- "open x" will open "x" with the default application ... "open -a applicationName x" will open "x" with application "applicationName"

general -- "<D-aKey>" creates a shortcut using the Command (Apple) key

improved(?) -- "nnoremap <silent> <D-p>f :exe ':silent !open -a /Applications/Path/To/Firefox.app %'<CR>"

(prevents remapping, runs quietly, shortcut is now [Command+p,f] for "preview, firefox" ... helpful if you preview in multiple browsers)

improved(?) -- "nnoremap <silent> <D-p>p :exe ':silent !open %'<CR>"

(same, but creates a generic "preview" using default applications ... helpful if you only preview in that browser, or can use it on other file types)

other (open directory in Finder) -- "nnoremap <silent> <D-p>d :exe ':silent !open -a finder %:p:h'<CR>"

(%:p:h completely expands the file path and removes the file name so only directory is left)

other (apply same to netrw browser, ie, :Ex command) -- add to .vimrc/.gvimrc: "let g:netrw_browsex_viewer = 'open'" (this is not necessary starting with 7.2, netrw now uses "open" as the default for "x").

(when using netrw browser, hit "x" while cursor is on file name to open that file with the default application ... if the cursor is over a directory, it will open in Finder)

Also, for Mac users, here is a way to save the current PHP file then preview it through localhost (TODO see %:p:s?pat?sub? below for Vim procedure):

nnoremap <F5> :update<Bar>!open -a Google\ Chrome `echo http://localhost/${PWD\#*/*/*/*/*/}/%`<CR>

From 1015 Preview file on localhost[]

Using this mapping will save the current file (if changed), then preview it on localhost. For example, if you are editing file /var/www/html/one/two/my.html, pressing F5 will use Firefox to open http://localhost/one/two/my.html.

nnoremap <F5> :silent update<Bar>silent !firefox %:p:s?\(.\{-}/\)\{4}?http://localhost/?<CR>

The core command has the form !firefox %:p:s?pat?sub? which uses the shell to start Firefox to open the current file (%) with its name modified by forming the full path (:p) then substituting the pattern pat with sub.

The pattern is \(.\{-}/\)\{4} which finds exactly four occurrences of any character (.) repeated as few times as possible (\{-}) up to and including the next slash (/). If the file being edited is my.html in directory /var/www/html/one/two, the pattern matches /var/www/html/ (the first occurrence of four slashes, with any characters between).

From 201110 Preview current HTML in browser on Linux[]

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".

Open an HTML file in a browser[]

TODO: Put a link somewhere in this tip to the result of merging all the stuff on opening HTML files.

See also[]

Open web browser to display URL under cursor

Display information for word under cursor in a web browser

Configure so "view source" opens in Vim

Other

Comments[]

I have removed the "Related plugins" section as the only plugin listed is obsolete and relies on Perl to do what can be done in plain Vim. JohnBeckett 10:16, April 18, 2012 (UTC)


I am working on merging a lot of tips related to opening HTML files in browsers, and documents in associated applications. I don't think anything more will be merged in to here. I will soon clean all this up and see how large the result is (some of it might then be moved elsewhere). JohnBeckett 11:21, April 26, 2012 (UTC)


In mappings, I am using F5 to open the current file and F8 to open the file under the cursor. Might rethink that after all merging complete. JohnBeckett 23:56, April 26, 2012 (UTC)


TODO: Probably need shellescape() or at least quotes in some of the mappings to handle embedded spaces. I think the Linux mappings also need & appended. JohnBeckett 05:01, April 27, 2012 (UTC)