开发者

Best way to open an URL in Vim

开发者 https://www.devze.com 2023-01-05 02:58 出处:网络
Lets say that you have either URL or a link on a webpage that is a text file.How would be the easiest way for the user to be able to open that file in a Vim?

Lets say that you have either URL or a link on a webpage that is a text file. How would be the easiest way for the user to be able to open that file in a Vim?


Depending on how your vim binary was built you can just give vim the url:

vim http://www.google.com/

Vim spawns curl and grabs the file, then opens it.


Assuming you want to just open a link in vim, how about this:

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

EDIT to make this command easier you can always user your browser of choice's "Copy link address" option.

EDIT Given @speshak's answer and my own, I would say the "easiest" way would be option 3, "a command line command".


Solution 1: use command

" 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

Now when you type: :URL https://news.google.com/topstories?hl=en-US&gl=US&ceid=US:en it will open google news


Solution 2: use function

or if you have a file that records a lot of URLs, and you want to use hotkey to open it, then you can try in this way

" .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>

and then, you can open it with ,url

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

Explanation of command

URL is a name, choose by you. (remember User-defined commands must start with an uppercase letter)

what is the command

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

Now when you type :Say Hello World

Vim echoes "Hello World".

nargs

  • -nargs=0 No arguments
  • -nargs=1 One argument
  • -nargs=* Any number of arguments
  • -nargs=? Zero or one argument
  • -nargs=+ One or more arguments


I have used links before since RedHat days. The command would be

links http://www.google.com

If links is not installed, you can do sudo apt-get install links on Ubuntu 14.04 LTS to install it.

Hope it helps.

0

精彩评论

暂无评论...
验证码 换一张
取 消