Vim Tips Wiki
(Copy text from tips 256, 587, 684, 1015, 201110 (merge in), links to source pages included.)
m (Reverted edits by 139.194.131.22 (talk | block) to last version by Fritzophrenic)
 
(14 intermediate revisions by 8 users not shown)
Line 14: Line 14:
   
 
==Viewing text from an html file or a URL==
 
==Viewing text from an html file or a URL==
A text-based web browser such as [[wikipedia:Elinks|<tt>elinks</tt>]] 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.
+
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! ViewHtmlText(url)
 
function! ViewHtmlText(url)
Line 25: Line 25:
 
endfunction
 
endfunction
 
" Save and view text for current html file.
 
" Save and view text for current html file.
nnoremap <Leader>H :update<Bar>:call ViewHtmlText(expand('%:p'))<CR>
+
nnoremap <Leader>H :update<Bar>call ViewHtmlText(expand('%:p'))<CR>
 
" View text for visually selected url.
 
" View text for visually selected url.
 
vnoremap <Leader>h y:call ViewHtmlText(@@)<CR>
 
vnoremap <Leader>h y:call ViewHtmlText(@@)<CR>
Line 34: Line 34:
   
 
After sourcing the above, and assuming the default backslash Leader key, you can:
 
After sourcing the above, and assuming the default backslash Leader key, you can:
*Edit an html file, then type <tt>\H</tt> to save and preview the file.
+
*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 <tt>\h</tt> to preview the file or web page.
+
*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 <tt>\h</tt> to preview the file or web page in Vim.
+
*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.
   
==Copy from 256 [[Opening current Vim file in your Windows browser]]==
+
==From 256 [[Opening current Vim file in your Windows browser]]==
  +
*''TODO'': Finish cleaning following notes.
{{TipImported
 
 
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.
|id=256
 
|previous=255
 
|next=258
 
|created=June 6, 2002
 
|complexity=basic
 
|author=David John Rayner (zzapper)
 
|version=5.7
 
|rating=8/5
 
|category1=
 
|category2=
 
}}
 
To open the current file in your browser:
 
map ,f :update<CR>:silent !start c:\progra~1\intern~1\iexplore.exe file://%:p<CR>
 
 
To open http link under cursor in your browser
 
map ,i :update<CR>: !start c:\progra~1\intern~1\iexplore.exe <cWORD><CR>
 
 
Note use of cWORD (not cword) meaning OUTER Word.
 
 
;Comments
 
If your windows browser is set up correctly, you will probably be able to get away with these instead.
 
   
 
<pre>
 
<pre>
  +
" On Windows, open URL under cursor.
map ,f :update<CR>:silent !start %:p<CR>
 
map ,i :update<CR>: !start <cWORD><CR>
+
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>
 
</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>).
This way, it should open in whatever the current browser is, whether it be Nutscape, Internet Exploder, Bloatzilla, etc.
 
   
 
----
 
----
  +
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):
With gvim 6.0 on XP, I am getting the error msg: E371: Command not found.
 
 
It seems to be complaining about the !Start command.
 
 
----
 
Anyone know how to make the file open in a currently open browser?
 
 
----
 
On Windows, using cWORD to open a link in a browser window does not work when the URL is in an html tag surrounded by quotes. But using cfile instead works for both situations - plain URL and html tag URL ...
 
 
map ,i :update<CR>: !start c:\progra~1\intern~1\iexplore.exe <cfile><CR>
 
 
----
 
Using gVim 7.3 on Windows 7, I had to modify the map above slightly to get it to work without a "Command not found" being returned. I used
 
 
<pre>
 
<pre>
map <silent> ,f :update<CR>:silent ! start /B %:p<CR>
+
nnoremap <F8> :execute 'silent ! start '.matchstr(getline('.'), 'http\S*')<CR>
 
</pre>
 
</pre>
The <silent> keeps the command from being echoed on the Vim status line, the spaces around the ! seem needed to avoid the Command not found, and the /B argument to start opens the browser in the background instead of opening a shell that you then have to close.
 
   
 
----
 
----
  +
On Windows 7 this opens the current file in Chrome (fix the path by replacing "MyName" with what it should be on your system).
For gVim 7.3 in Windows 7, this is what I had to do to get gVim to open Chrome (took me forever to figure out).
 
 
 
<pre>
 
<pre>
map <silent><leader><C-o> :update<CR>:silent ! start "C:\Users\{useraccount}\AppData\Local\Google\Chrome\Application\chrome.exe" "file://%:p" & exit<cr>
+
nnoremap <silent> <F5> :update<Bar>silent !start "C:\Users\MyName\AppData\Local\Google\Chrome\Application\chrome.exe" "file://%:p"<CR>
 
</pre>
 
</pre>
  +
Or it might be here:
 
This opens the current file in a new window and the '& exit' bit runs another command to autoclose the command prompt for you.
 
 
----
 
I did some research on the above just-added comment "Using gVim 7.3 on Windows 7". The comment about the space after "!" also applies to Windows XP. My findings so far are:
 
 
Suppose you edit an html file, say, <tt>C:\path\to\file\example.html</tt> and you enter:
 
 
<pre>
 
<pre>
  +
nnoremap <silent> <F5> :update<Bar>silent !start "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" "file://%:p"<CR>
:!start %:p
 
 
</pre>
 
</pre>
 
That attempts to run "start C:\path\to\file\example.html" which relies on a file association. The result is an error in Vim: "E371: Command not found".
 
 
On a Windows system, special code in Vim detects that the command starts with "start " (including a space). That special code also handles arguments "/min" and "/b" (i.e. they are handled by Vim, and are never seen by Windows). In file src/os_win32.c, function <tt>mch_call_shell()</tt> invokes <tt>CreateProcess()</tt> to execute the command "<tt>C:\path\to\file\example.html</tt>" (the "start " has been stripped out by Vim). That attempt fails because the specified file is not an executable program.
 
 
However, if you enter (space after "!"):
 
<pre>
 
:! start %:p
 
</pre>
 
 
<tt>mch_call_shell()</tt> calls <tt>mch_system(newcmd, options)</tt> where <tt>newcmd</tt> is (on Windows XP):
 
<pre>
 
C:\WINDOWS\system32\cmd.exe /c start C:\path\to\file\example.html
 
</pre>
 
 
That works, and the shell (<tt>cmd.exe</tt>) executes the internal "start" command which uses a file association to open a web browser showing the html file. The Vim docs mention the special handling at {{help|:!start}}; also see {{help|:!}}
 
 
I'll have to leave cleaning the tip for another time, but I wanted to show these preliminary results which explain how the space after the bang makes a big difference. [[User:JohnBeckett|JohnBeckett]] 08:17, August 4, 2011 (UTC)
 
 
 
----
 
----
  +
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:
Something corresponding for unix ppl:
 
 
 
<pre>
 
<pre>
  +
nnoremap <F5> :update<Bar>silent !xdg-open %:p &<CR>
function! Browser ()
 
  +
nnoremap <F8> :silent !xdg-open <cfile> &<CR>
let line = getline (".")
 
let line = matchstr (line, "http[^ ]*")
 
execute "!opera ".line
 
endfunction
 
map <Leader>w :call Browser ()<CR>
 
 
</pre>
 
</pre>
   
 
==From 587 [[Preview current file in Mozilla through localhost]]==
This needs a browser that can send URLs from the commandline to an existing process.
 
  +
When editing an HTML file on Windows, enter the following to save the current file and open it in your default browser:
:You can use <tt>xdg-open</tt> on many unix/linux systems to open a file with the default program (just like <tt>start</tt> on Windows):
 
:<pre>map ,f :update<CR>:silent !xdg-open <cfile><CR></pre> (or replace <tt><cfile></tt> with <tt>%:p</tt> to open the current file.)
 
:--[[User:Pydave|Pydave]] 16:58, August 9, 2011 (UTC)
 
----
 
Oops, the above function for unix will open a browser with the URL in the current line. Not quite what this tip is about...
 
 
==Copy from 587 [[Preview current file in Mozilla through localhost]]==
 
{{TipImported
 
|id=587
 
|previous=586
 
|next=588
 
|created=October 14, 2003
 
|complexity=intermediate
 
|author=Mark Woodward
 
|version=5.7
 
|rating=7/7
 
|category1=
 
|category2=
 
}}
 
This is something I 'discovered' whilst trying to preview html or php files in mozilla using the apache server locally.
 
 
Put the path as the first line of a file wrapped in the appropriate comments.
 
 
For example, for php:
 
 
<pre>
 
<pre>
 
:update|silent ! start %:p
<?php // http://localhost/examples/chapter02/vieworders2.php ?>
 
 
</pre>
 
</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:
or for html:
 
 
<pre>
 
<pre>
  +
nnoremap <silent> <F5> :update<Bar>silent ! start %:p<CR>
<!-- http://localhost/examples/chapter02/orderform.html -->
 
 
</pre>
 
</pre>
   
  +
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>.
Place the following mappings in your vimrc file.
 
   
  +
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.
<pre>
 
" Typing &lt;S-F12> (Shift-F12) will open the file in moz through the server.
 
nmap &lt;S-F12> :sil! !start mozilla "<cfile>"<CR>
 
imap &lt;S-F12> <Esc>:sil! !start mozilla "<cfile>"<CR>i
 
</pre>
 
 
As I'm working through Wellings PHP and MySQL Web Development I can use my tokens plugin to speed creating the first line as follows:
 
 
Add these as the string parts of the array:
 
   
  +
A Linux version of the above mapping would use a program like <code>xdg-open</code>:
For php:
 
 
<pre>
 
<pre>
  +
nnoremap <F5> :update<Bar>silent !xdg-open %:p &<CR>
"phpf <?php // http://localhost/examples/chapter¤« chapter? »/¤« file name ».php ?>"
 
  +
nnoremap <F5> :silent update<Bar>silent !xdg-open %:p &<CR>
 
</pre>
 
</pre>
   
 
==From 684 [[Preview current HTML in browser on Mac OS X]]==
For html:
 
  +
*''TODO'': Clean following.
<pre>
 
"htmlf <!-- http://localhost/examples/chapter¤« chapter? »/¤« file name ».html -->"
 
</pre>
 
 
then on the first line of the file if I type phpf <Leader>et I'll only have to enter the chapter number and the filename without the extension. eg 04<CR> orders<CR>
 
 
This will insert:
 
<pre>
 
<?php // http://localhost/examples/chapter04/orders.php ?>
 
</pre>
 
 
Place the cursor anywhere on the file path, &lt;S-F12> and the file will load (through apache) in moz.
 
 
;Comments
 
How open in a new tab in moz rather than a new window?
 
 
Try something like this:
 
<pre>
 
mozilla -remote 'openURL(http://www.vim.org, new-tab)'
 
</pre>
 
 
----
 
A related piece of info for Windows:
 
<pre>
 
start filename.ext
 
</pre>
 
 
(in the shell) "opens" the named file with its default action. This means, for instance, that
 
<pre>
 
:w | ! start %
 
</pre>
 
 
(in Vim with the cursor on an html file) will save the current file, then open it in your default browser. On my system, it opens a new Dos Box and a new Netscape 7 window. The Dos box must be closed by pressing Enter to return to Vim. You may leave the browser window open if you like (for instance, after you next make some changes and save the file, click "Reload" or press Ctrl-R in the browser to see the new version).
 
 
Beware: The space between ! and start is necessary (if I leave it out, I get Vim error E371).
 
 
==Copy from 684 [[Preview current HTML in browser on Mac OS X]]==
 
{{TipImported
 
|id=684
 
|previous=683
 
|next=685
 
|created=2004
 
|complexity=basic
 
|author=Robert Roberts
 
|version=6.0
 
|rating=8/7
 
|category1=Mac OS X
 
|category2=
 
}}
 
 
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 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.
   
 
<pre>
 
<pre>
:map <Leader>p :!open -a Safari %<CR><CR>
+
nnoremap <F5> :!open -a Safari %<CR><CR>
 
</pre>
 
</pre>
   
Line 250: Line 112:
 
general -- "<D-aKey>" creates a shortcut using the Command (Apple) key
 
general -- "<D-aKey>" creates a shortcut using the Command (Apple) key
   
improved(?) -- "noremap <silent> <D-p>f :exe ':silent !open -a /Applications/Path/To/Firefox.app %'<CR>"
+
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)
 
(prevents remapping, runs quietly, shortcut is now [Command+p,f] for "preview, firefox" ... helpful if you preview in multiple browsers)
   
improved(?) -- "noremap <silent> <D-p>p :exe ':silent !open %'<CR>"
+
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)
 
(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) -- "noremap <silent> <D-p>d :exe ':silent !open -a finder %:p:h'<CR>"
+
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)
 
(%:p:h completely expands the file path and removes the file name so only directory is left)
Line 266: Line 128:
 
(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)
 
(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 write a PHP file then preview it through localhost:
+
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>
 
nnoremap <F5> :update<Bar>!open -a Google\ Chrome `echo http://localhost/${PWD\#*/*/*/*/*/}/%`<CR>
 
</pre>
   
 
==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 <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>.
 
<pre>
 
<pre>
map <F1> :w<CR>:!open -a Google\ Chrome `echo http://localhost/${PWD\#*/*/*/*/*/}/%`<CR>
+
nnoremap <F5> :silent update<Bar>silent !firefox %:p:s?\(.\{-}/\)\{4}?http://localhost/?<CR>
 
</pre>
 
</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>.
==Copy from 1015 [[Preview file on localhost]]==
 
{{TipImported
 
|id=1015
 
|previous=1014
 
|next=1016
 
|created=October 8, 2005
 
|complexity=basic
 
|author=JA
 
|version=5.7
 
|rating=-2/7
 
|category1=
 
|category2=
 
}}
 
Try this mapping in your vimrc to
 
*Save the current file.
 
*Preview it on localhost.
 
   
  +
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).
Edit the number of */ to fit your localhost installation, that is, how much of the path to strip from the left.
 
   
 
==From 201110 [[Preview current HTML in browser on Linux]]==
It also assumes the file you are editing is within your localhost installation. I have only tested it in bash.
 
 
<pre>
 
map <F8> :w^M:!mozilla `echo http://localhost/${PWD\#*/*/*/*/}/%`^M
 
</pre>
 
 
==Copy from 201110 [[Preview current HTML in browser on Linux]]==
 
{{TipProposed
 
|id=0
 
|previous=0
 
|next=0
 
|created=October 22, 2011
 
|complexity=basic
 
|author=
 
|version=7.0
 
|subpage=/201110
 
|category1=
 
|category2=
 
}}
 
 
This mapping allows you to use a browser to preview the HTML file currently being edited.
 
This mapping allows you to use a browser to preview the HTML file currently being edited.
 
<pre>
 
<pre>
nnoremap <F8> :silent update<Bar>silent !firefox %:p &<CR>
+
nnoremap <F5> :silent update<Bar>silent !firefox %:p &<CR>
 
</pre>
 
</pre>
   
Line 321: Line 155:
   
 
==See also==
 
==See also==
'''Preview current html file in a web browser'''
 
*127 Preview HTML files quickly &nbsp;&nbsp; ''(this tip)''
 
*[[VimTip256|256 Opening current Vim file in your Windows browser]]
 
*[[VimTip587|587 Preview current file in Mozilla through localhost]]
 
*[[VimTip684|684 Preview current HTML in browser on Mac OS X]]
 
*[[VimTip1015|1015 Preview file on localhost]]
 
*[[Preview current HTML in browser on Linux]] proposed new tip from [[Vim Tips Wiki:New tips/201110|201110]]
 
 
 
'''Open web browser to display URL under cursor'''
 
'''Open web browser to display URL under cursor'''
 
*[[VimTip306|306 Open a web-browser with the URL in the current line]]
 
*[[VimTip306|306 Open a web-browser with the URL in the current line]]
Line 371: Line 197:
 
----
 
----
 
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)
 
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)