wyc
wyc

Reputation: 55273

How do I open http://localhost in my web browser from Vim?

Basically, I want to tell Vim:

  1. Open my web browser (for instance, Firefox)
  2. Open this file (say index.php) in thought this address : http://localhost
  3. In other words: http://localhost/index.php

PS: Unfortunately I use Windows XP

Upvotes: 12

Views: 29081

Answers (4)

Sam Post
Sam Post

Reputation: 3821

On Unix/Linux, use

:! firefox http://localhost/%:p

%:p is the path and filename of the current buffer

Upvotes: 2

Michał Marczyk
Michał Marczyk

Reputation: 84331

You'll need to use a method appropriate to your environment to start the web browser, but you already asked a question about that.

So, use :!start cmd /c ..., !d:\path\to\firefox ... or whatever. The important bit: you'll want to use "http://localhost/" . expand("%:t") as the argument passed to the browser. So, do something like

:exec ":!start cmd /c ... " . "http://localhost/" . expand("%:t")
                         ^- leave a trailing space here

EDIT: A Clarification: expand("%:t") is a Vim script expression which expands to the last component of the current filename. On Windows this means that if the current filename is C:\a complicated path\to\index.html, expand("%:t") will return index.html.

HTH.

Upvotes: 1

Peter
Peter

Reputation: 132207

on mac you can

:!open http://localhost/index.php

Upvotes: 7

RedBlueThing
RedBlueThing

Reputation: 42522

If you are running Vim on windows, then this will work:

:! start http://localhost/index.php

This will use the default browser, if you wanted to start a specific browser then you would need the explicit path to the executable (instead of start).

From the Vim help cmd:

:!{cmd}

Execute {cmd} with the shell. See also the 'shell' and 'shelltype' option.

Obviously, if you are on some other system you just need to use the appopriate command to start the browser on that platform.

Upvotes: 11

Related Questions