如何在 vim 或 emacs 等编辑器中打开网络浏览器(例如 Firefox)中的文件?

发布于 2024-08-17 07:16:44 字数 127 浏览 1 评论 0 原文

如何在 vim 或 emacs 等编辑器中打开浏览器(例如 Firefox)中的文件? Notepad++ 按 Ctrl+Shift+Alt+X (Firefox) 在浏览器中打开文件。有没有办法在 gVim 或 Emacs 中做到这一点?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

携余温的黄昏 2024-08-24 07:16:44

browser-url-of-file 是一个交互式编译的 Lisp 函数
`浏览-url.el'。

它绑定到 <在浏览器中加载此缓冲区>,抄送
茨诉。

(浏览文件 URL 和可选文件)

要求 WWW 浏览器显示文件。
如果文件为零或被调用,则显示当前缓冲区的文件
交互地。使用函数将文件名转换为 URL
浏览 url-文件-url。使用以下命令将 URL 传递到浏览器
browse-url 函数然后运行 ​​browse-url-of-file-hook

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 function
browse-url-file-url. Pass the URL to a browser using the
browse-url function then run browse-url-of-file-hook.

她说她爱他 2024-08-24 07:16:44

在 emacs 中,我不认为这是内置的,我可能是错的,但如果不是,这里有一个函数可以做到这一点:

(defun open-in-browser()
  (interactive)
  (let ((filename (buffer-file-name)))
    (browse-url (concat "file://" filename))))

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))))
别想她 2024-08-24 07:16:44

无论出于何种原因,我在 WinXP 上安装的 EmacsW32 不断向 shell 发送浏览 url 指令,其中包含“仅打开 file://,但效果不太好*。将其从膝盖处切掉,并修改 贾斯汀的原创< /a> 如下对我有用:

(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 "\"")))))

需要一些改进,以及替换您最喜欢的浏览器,硬编码

* 我认为问题在于 browser-url-default- 中的系统类型检查。 windows浏览器,但不积极。

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.

染年凉城似染瑾 2024-08-24 07:16:44

在 gVim 中:

:!start cmd /c "C:\Users\pierre\AppData\Local\Google\Chrome\Application\chrome.exe" file:///"%:p ""

您需要 file:// URI 来指示它来自文件系统,这适用于所有浏览器 %:p 生成当前文件的完整文件路径。 。

如果您已将 shell 设置为 bash 或其他内容,则只需将其映射到您选择的任何内容

即可 (引用 Justinhj):
<代码>

(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))))

巴黎盛开的樱花 2024-08-24 07:16:44

您的意思是您想在网络浏览器中打开当前正在编辑的文件?

在 Vim 中,使用类似 :!firefox % 的内容。

编辑:事实上,您可以使用nmap :!firefox % 使 Vim 的行为非常像 Notepad++(尽管此映射不会关心您是否按 Shift)。

请注意,当在命令行上给出文件名时,并非每个浏览器都会实际呈现文件的内容;例如,Google Chrome 浏览器将打开“另存为”对话框,就好像您正在下载相关文件一样。如有疑问,请查阅浏览器的文档。不过,Firefox 会“正常工作”。

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.

对风讲故事 2024-08-24 07:16:44

根据你想用它做什么,你可以考虑 Emacs + MozRepl,它基本上可以让你通过 telnet 向 Firefox 发送 javascript 命令。不幸的是,我似乎无法编写 elisp 来完成这项工作,但是 Sard< 显示了从 emacs 内重新加载网页的相关技巧/a> 在 你的 .emacs 中有什么?。有关集成 emacs 和 mozrepl 的更多信息,请参阅原始来源以及这里有一个很酷的技巧,可以将浏览器中的页面更新为您在 emacs 缓冲区中输入内容 - 在使用 html 时获得即时反馈非常好。

我认为同样的事情也适用于 vim,但我只在 emacs 中使用过它。

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.

淡淡绿茶香 2024-08-24 07:16:44

我使用 shell 命令 xdg-open 执行 Elisp 函数。
然后我在 html 模式下定义一个键来调用该函数。
您必须能够轻松地将内容添加到 .emacs 文件中。

(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)))
)

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)))
)
宣告ˉ结束 2024-08-24 07:16:44

此答案基于 Emacs 26.2

Emacs 具有在内置浏览器中打开文件的功能,但不同平台上的行为可能有所不同。通过输入 Ch f browser-url-of-file 查看 browse-url-of-file 的源代码和文档,您会看到变量 browser-url-browse-function 确定使用哪个浏览器。然后,您可以通过选择 browse-url-chrome 自定义此变量以使用 Chrome,然后应用并保存更改。要访问自定义页面,请输入 Ch f browser-url-browser-function,然后选择 customize 超链接,或 Mxcustomize 然后搜索浏览 url-浏览器功能

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文