Best way to open an URL in 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?
- Right click and save l开发者_StackOverflow社区ink as?
- Use a specific file extension that defaults to Vim?
- Command line command?
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.
精彩评论