web-search.plugin.zsh 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. # web_search from terminal
  2. function web_search() {
  3. emulate -L zsh
  4. # define search engine URLS
  5. typeset -A urls
  6. urls=(
  7. $ZSH_WEB_SEARCH_ENGINES
  8. google "https://www.google.com/search?q="
  9. bing "https://www.bing.com/search?q="
  10. yahoo "https://search.yahoo.com/search?p="
  11. duckduckgo "https://www.duckduckgo.com/?q="
  12. startpage "https://www.startpage.com/do/search?q="
  13. yandex "https://yandex.ru/yandsearch?text="
  14. github "https://github.com/search?q="
  15. baidu "https://www.baidu.com/s?wd="
  16. ecosia "https://www.ecosia.org/search?q="
  17. goodreads "https://www.goodreads.com/search?q="
  18. qwant "https://www.qwant.com/?q="
  19. givero "https://www.givero.com/search?q="
  20. stackoverflow "https://stackoverflow.com/search?q="
  21. wolframalpha "https://www.wolframalpha.com/input/?i="
  22. archive "https://web.archive.org/web/*/"
  23. scholar "https://scholar.google.com/scholar?q="
  24. )
  25. # check whether the search engine is supported
  26. if [[ -z "$urls[$1]" ]]; then
  27. echo "Search engine '$1' not supported."
  28. return 1
  29. fi
  30. # search or go to main page depending on number of arguments passed
  31. if [[ $# -gt 1 ]]; then
  32. # build search url:
  33. # join arguments passed with '+', then append to search engine URL
  34. url="${urls[$1]}${(j:+:)@[2,-1]}"
  35. else
  36. # build main page url:
  37. # split by '/', then rejoin protocol (1) and domain (2) parts with '//'
  38. url="${(j://:)${(s:/:)urls[$1]}[1,2]}"
  39. fi
  40. open_command "$url"
  41. }
  42. alias bing='web_search bing'
  43. alias google='web_search google'
  44. alias yahoo='web_search yahoo'
  45. alias ddg='web_search duckduckgo'
  46. alias sp='web_search startpage'
  47. alias yandex='web_search yandex'
  48. alias github='web_search github'
  49. alias baidu='web_search baidu'
  50. alias ecosia='web_search ecosia'
  51. alias goodreads='web_search goodreads'
  52. alias qwant='web_search qwant'
  53. alias givero='web_search givero'
  54. alias stackoverflow='web_search stackoverflow'
  55. alias wolframalpha='web_search wolframalpha'
  56. alias archive='web_search archive'
  57. alias scholar='web_search scholar'
  58. #add your own !bang searches here
  59. alias wiki='web_search duckduckgo \!w'
  60. alias news='web_search duckduckgo \!n'
  61. alias youtube='web_search duckduckgo \!yt'
  62. alias map='web_search duckduckgo \!m'
  63. alias image='web_search duckduckgo \!i'
  64. alias ducky='web_search duckduckgo \!'
  65. # other search engine aliases
  66. if [[ ${#ZSH_WEB_SEARCH_ENGINES} -gt 0 ]]; then
  67. typeset -A engines
  68. engines=($ZSH_WEB_SEARCH_ENGINES)
  69. for key in ${(k)engines}; do
  70. alias "$key"="web_search $key"
  71. done
  72. unset engines key
  73. fi