11

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

alexchenco
  • 53,565
  • 76
  • 241
  • 413

4 Answers4

11

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.

RedBlueThing
  • 42,006
  • 17
  • 96
  • 122
  • 3
    thanks it worked: start "http://localhost/index.php" "file:///"%:p" (opened my current file in localhost) – alexchenco Jan 11 '10 at 05:00
7

on mac you can

:!open http://localhost/index.php
Peter
  • 127,331
  • 53
  • 180
  • 211
2

On Unix/Linux, use

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

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

Sam Post
  • 3,721
  • 3
  • 17
  • 14
1

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.

Community
  • 1
  • 1
Michał Marczyk
  • 83,634
  • 13
  • 201
  • 212
  • Also, you can use `%:t` in ex commands (the commands which begin with a colon) to signify "the last path component of the file being edited", meaning the basename of the file (like `index.html` in `C:\path\to\index.html`). – Michał Marczyk Jan 11 '10 at 04:37