繁体   English   中英

在 Vim 中打开 URL 的最佳方法

[英]Best way to open an URL in Vim

假设您在文本文件的网页上有 URL 或链接。 用户如何在 Vim 中打开该文件的最简单方法是什么?

  • 右键单击并将链接另存为?
  • 使用默认为 Vim 的特定文件扩展名?
  • 命令行命令?

根据您的 vim 二进制文件的构建方式,您可以只为 vim 提供 url:

vim http://www.google.com/

Vim 生成 curl 并抓取文件,然后打开它。

假设您只想在 vim 中打开一个链接,那么如何:

curl http://www.google.com | vim -

编辑以简化此命令,您始终可以使用您选择的浏览器的“复制链接地址”选项。

编辑考虑到@speshak 的答案和我自己的答案,我会说“最简单”的方法是选项 3,“命令行命令”。

解决方案1:使用命令

" gvimrc

for g:chrome_exe in [
    \'C:\...\Google\Chrome\Application\chrome.exe',
    \]
    if !filereadable(g:chrome_exe)
        continue
    endif
    command -nargs=+ URL :exe "silent !start ".g:chrome_exe." <args>"
    break
endfor

现在,当您输入: :URL https://news.google.com/topstories?hl=en-US&gl=US&ceid=US:en它将打开谷歌新闻


方案二:使用函数

或者如果你有一个记录了很多网址的文件,你想用热键打开它,那么你可以试试这种方式

" .gvimrc
let g:chrome_exe = 'C:/.../Google/Chrome/Application/chrome.exe'

function OpenURL()
    normal "*yy
    " let result = getreg("x")
    " return result
    :execute "silent !start ".g:chrome_exe2." ".getreg("*")
endfunction

map ,url :call OpenURL()<CR>

然后,您可以使用,url打开它

" test.txt
https://www.google.com/
https://news.google.com/topstories?hl=en-US&gl=US&ceid=US:en

命令说明

URL是一个名称,由您选择。 (记住用户定义的命令必须以大写字母开头)

命令是什么

command -nargs=+ Say :echo "<args>"

现在当你输入:Say Hello World

Vim 回应“Hello World”。

nargs

  • -nargs=0 无参数
  • -nargs=1 一个参数
  • -nargs=* 任意数量的参数
  • -nargs=? 零个或一个参数
  • -nargs=+ 一个或多个参数

从 RedHat 时代起,我就使用过links 命令将是

links http://www.google.com

如果未安装links ,您可以在 Ubuntu 14.04 LTS 上执行sudo apt-get install links进行安装。

希望能帮助到你。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM