How to open files in browsers (e.g Firefox) within editors like vim or emacs? Notepad++ open files in browsers by pressing Ctrl+Shift+Alt+X (Firefox). Is there a way to do this in gVim or Emacs?
-
I've put the solutions for gVim and emacs into one post that you can select as the correct answer. – Pierre-Antoine LaFayette Jan 12 '10 at 11:52
8 Answers
browse-url-of-file is an interactive compiled Lisp function in `browse-url.el'.
It is bound to <menu-bar> <HTML> <Load this Buffer in Browser>
, C-c
C-z v.
(browse-url-of-file &optional file)
Ask a WWW browser to display file.
Display the current buffer's file if file is nil or if called interactively. Turn the filename into a URL with functionbrowse-url-file-url
. Pass the URL to a browser using thebrowse-url
function then runbrowse-url-of-file-hook
.

- 39,593
- 22
- 116
- 167

- 394
- 2
- 3
-
2
-
6
-
I do not understand how (a) this is an answer (b) it is the accepted answer. Has nothing to do with GVim or Emacs, but apparently requires Lisp to be installed ?!? – Stewart May 08 '16 at 17:14
-
2Emacs is scripted with its own Lisp, and this function comes with Emacs. – Nick McCurdy Oct 02 '16 at 06:04
-
-
There's also `browse-url-of-buffer` which seems to work if the buffer isn't saved to a file yet. – Nick McCurdy Aug 28 '17 at 23:30
In emacs I don't think this is built in, I may be wrong, but if not here is a function to do it:
(defun open-in-browser()
(interactive)
(let ((filename (buffer-file-name)))
(browse-url (concat "file://" filename))))

- 11,147
- 11
- 58
- 104
-
1
-
3
-
2For some reason, for me this opens the buffer contents in the default _html editor_ not default web browser. I'm using 24.5 on OSX Capitan. Any ideas why this could be occurring? I checked that the `open` program is running fine from terminal. – Sophia Gold Aug 09 '16 at 01:30
For whatever reason, my EmacsW32 on WinXP install kept sending browse-url directives to shell with "open file:// alone, and that didn't work so well*. Cutting it off at the knees, and modifying justin's original as below worked for me:
(defun open-in-browser()
"open buffer in browser, unless it is not a file. Then fail silently (ouch)."
(interactive)
(if (buffer-file-name)
(let ((filename (buffer-file-name)))
(shell-command (concat "start firefox.exe \"file://" filename "\"")))))
Needs some improvement. As well as replacement of your favorite browser. d**n you, hard-coding.
* I think the problem was the system-type check in browse-url-default-windows-browser, but not positive.

- 1
- 1

- 9,020
- 5
- 48
- 68
In gVim:
:!start cmd /c "C:\Users\pierre\AppData\Local\Google\Chrome\Application\chrome.exe" file:///"%:p""
You need the file:// URI to indicate that it is from the file system, this will work with all browsers. %:p
produces the full file path for the current file. The quotes are necessary.
Simply map that to whatever you choose. You may need to do set shell=cmd.exe
if you've set your shell to bash or something else.
In emacs (quoting justinhj):
(defun open-in-browser()
(interactive)
(let ((filename (buffer-file-name)))
(browse-url (concat "file://" filename))))

- 24,222
- 8
- 54
- 58
You mean you'd like to open the file currently being edited in a web browser?
In Vim, use something like :!firefox %
.
Edit: You could, in fact, use nmap <silent> <C-M-X> :!firefox %<CR>
to cause Vim to act very much like Notepad++ (though this mapping won't care whether you press shift or not).
Note that not every browser will actually render the file's contents when given the filename on the command line; e.g. Google Chrome will open a "save as" dialogue instead, as if you were downloading the file in question. Look up your browser's docs if in doubt. Firefox will 'just work', though.

- 83,634
- 13
- 201
- 212
-
2If you mean the filename under the cursor use
in place of %. – Laurence Gonsalves Jan 10 '10 at 01:27 -
@janoChen: Well, it should definately work if you've got the `firefox` executable on your path. This would not necessarily be the case if you're on Windows; in that case, replace `firefox` with something like `C:/path/to/firefox.exe`. Generally speaking, you'll need to figure out what command to use to open a file with your browser **on the command line** (that's `firefox name-of-the-file` on my box) and use that instead of `firefox`, with % (or
, following Laurence's suggestion) in place of the actual filename. Let me know if this doesn't help. – Michał Marczyk Jan 10 '10 at 01:40 -
should my path look something like this? 'nmap
:!D:\Program Files\Mozilla Firefox 3.6 Beta 5\firefox.exe' -
= ctrl+alt+x, yes, but nothing *will* happen until you execute that `nmap ...` command I suggested in the answer. Also, `nmap` makes the mapping only work in normal mode, which is normally the mode you're in when you start Vim and which you can normally get back into by pressing Esc. Use `:help map` for information on other mapping commands (`map`, `imap` etc.) which produce mappings accessible from other modes. Finally, it won't work if the basic `:!firefox %` doesn't work, you need to figure out the correct command first, then use it in the mapping. – Michał Marczyk Jan 10 '10 at 01:48
-
Yeah, that would be what your path should look like. Does it work now? – Michał Marczyk Jan 10 '10 at 01:49
-
Thanks for the help but still not working. I placed "nmap
:!D:\Program Files\Mozilla Firefox 3.6 Beta 5\firefox.exe" in my _vimrc. Tried to execute it with Crtl + Alt + X in normal mode. Nothing happened. (I also tried "nmap :D:\Program Files\Mozilla Firefox 3.6 Beta 5\firefox.exe" (without the !) -
You need the `!`, because that's what tells Vim you want to execute an external programme. Also, skip the mapping for now; try `:!D:\Program Files\Mozilla Firefox 3.6 Beta 5\firefox.exe %` and let me know what goes wrong. Maybe I can help then. – Michał Marczyk Jan 10 '10 at 01:55
-
Yes I just tried ":!D:\Program Files\Mozilla Firefox 3.6 Beta 5\firefox.exe %" in normal mode and it opened the cmd showing me the path of firefox and the file (index.html). Shell return 1. Hit any key to close this window. – alexchenco Jan 10 '10 at 01:59
-
Well, I haven't got a Windows box at hand to check this, but I understand that if you open a cmd.exe window and type in `D:\Program Files\Mozilla Firefox 3.6 Beta 5\firefox.exe your_file.html`, then Firefox opens your file? (I wonder if this question perhaps belongs on Superuser, rather than here... oh well.) – Michał Marczyk Jan 10 '10 at 02:08
-
"D:\Program Files\Mozilla Firefox 3.6 Beta 5\firefox.exe" _< this open firefox. "D:\Program Files\Mozilla Firefox 3.6 Beta 5\firefox.exe your_file.html" <- this won't open my file. I think it can't find the file path. Maybe doesn't work on Windows. – alexchenco Jan 10 '10 at 02:17
-
OK, in that case, try using `:exec "!D:\Program Files\Mozilla Firefox 3.6 Beta 5\firefox.exe " . expand("%:p")`. If this doesn't work (although it really, really should), try using `@%` instead of the `expand("%:p")` bit. Note there's a space after firefox.exe, before the closing double quote. – Michał Marczyk Jan 10 '10 at 02:36
Depending what you want to do with this, you might consider Emacs + MozRepl, which basically lets you send javascript commands to Firefox via telnet. Unfortunately I can't seem to write the elisp to make this work, but a related trick for reloading webpages from within emacs is shown by Sard in What's in your .emacs?. More information on integrating emacs and mozrepl from the original source and also here for a cool trick that updates the page in the browser as you type in the emacs buffer - it's pretty nice for getting instant feedback when working with html.
I reckon the same thing would work with vim, but I've only used it in emacs.
I do it an Elisp function using shell command xdg-open.
Then I define a key in html-mode to call the function.
You've gotta be comfortable adding stuff to your .emacs file.
(defun open-html()
"Get the HTML file path & open it"
(interactive)
(let (html-file-path)
(setq html-file-path (buffer-file-name))
(shell-command (format "xdg-open '%s'" html-file-path)))
)

- 2,202
- 1
- 19
- 13
This answer is based on Emacs 26.2
Emacs has the functions for opening a file in a browser built in but the behavior on different platforms may be different. Looking into the source code and documentation of browse-url-of-file
by entering C-h f browse-url-of-file
you'll see that the variable browse-url-browse-function
determines which browser is used. You can then customize this variable to use, say, Chrome, by choosing browse-url-chrome
and then apply and save the change. To access the customization page either entering C-h f browse-url-browser-function
and then selecting the customize
hyperlink, or M-x customize
then searching for browse-url-browser-function
.

- 814
- 14
- 26