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?
views:
531answers:
6You 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.
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))))
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))))
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.
browse-url-of-file is an interactive compiled Lisp function in `browse-url.el'.
It is bound to , 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 function
browse-url-file-url'. Pass the URL to a browser using the
browse-url' function then run `browse-url-of-file-hook'.
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.