Contents

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.

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.

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):

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:

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

(%: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):

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.

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

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)