web-search.plugin.zsh 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. brave "https://search.brave.com/search?q="
  11. yahoo "https://search.yahoo.com/search?p="
  12. duckduckgo "https://www.duckduckgo.com/?q="
  13. startpage "https://www.startpage.com/do/search?q="
  14. yandex "https://yandex.ru/yandsearch?text="
  15. github "https://github.com/search?q="
  16. baidu "https://www.baidu.com/s?wd="
  17. ecosia "https://www.ecosia.org/search?q="
  18. goodreads "https://www.goodreads.com/search?q="
  19. qwant "https://www.qwant.com/?q="
  20. givero "https://www.givero.com/search?q="
  21. stackoverflow "https://stackoverflow.com/search?q="
  22. wolframalpha "https://www.wolframalpha.com/input/?i="
  23. archive "https://web.archive.org/web/*/"
  24. scholar "https://scholar.google.com/scholar?q="
  25. ask "https://www.ask.com/web?q="
  26. youtube "https://www.youtube.com/results?search_query="
  27. deepl "https://www.deepl.com/translator#auto/auto/"
  28. dockerhub "https://hub.docker.com/search?q="
  29. npmpkg "https://www.npmjs.com/search?q="
  30. packagist "https://packagist.org/?query="
  31. gopkg "https://pkg.go.dev/search?m=package&q="
  32. )
  33. # check whether the search engine is supported
  34. if [[ -z "$urls[$1]" ]]; then
  35. echo "Search engine '$1' not supported."
  36. return 1
  37. fi
  38. # search or go to main page depending on number of arguments passed
  39. if [[ $# -gt 1 ]]; then
  40. # if search goes in the query string ==> space as +, otherwise %20
  41. # see https://stackoverflow.com/questions/1634271/url-encoding-the-space-character-or-20
  42. local param="-P"
  43. [[ "$urls[$1]" == *\?*= ]] && param=""
  44. # build search url:
  45. # join arguments passed with '+', then append to search engine URL
  46. url="${urls[$1]}$(omz_urlencode $param ${@[2,-1]})"
  47. else
  48. # build main page url:
  49. # split by '/', then rejoin protocol (1) and domain (2) parts with '//'
  50. url="${(j://:)${(s:/:)urls[$1]}[1,2]}"
  51. fi
  52. open_command "$url"
  53. }
  54. alias bing='web_search bing'
  55. alias brs='web_search brave'
  56. alias google='web_search google'
  57. alias yahoo='web_search yahoo'
  58. alias ddg='web_search duckduckgo'
  59. alias sp='web_search startpage'
  60. alias yandex='web_search yandex'
  61. alias github='web_search github'
  62. alias baidu='web_search baidu'
  63. alias ecosia='web_search ecosia'
  64. alias goodreads='web_search goodreads'
  65. alias qwant='web_search qwant'
  66. alias givero='web_search givero'
  67. alias stackoverflow='web_search stackoverflow'
  68. alias wolframalpha='web_search wolframalpha'
  69. alias archive='web_search archive'
  70. alias scholar='web_search scholar'
  71. alias ask='web_search ask'
  72. alias youtube='web_search youtube'
  73. alias deepl='web_search deepl'
  74. alias dockerhub='web_search dockerhub'
  75. alias npmpkg='web_search npmpkg'
  76. alias packagist='web_search packagist'
  77. alias gopkg='web_search gopkg'
  78. #add your own !bang searches here
  79. alias wiki='web_search duckduckgo \!w'
  80. alias news='web_search duckduckgo \!n'
  81. alias map='web_search duckduckgo \!m'
  82. alias image='web_search duckduckgo \!i'
  83. alias ducky='web_search duckduckgo \!'
  84. # other search engine aliases
  85. if [[ ${#ZSH_WEB_SEARCH_ENGINES} -gt 0 ]]; then
  86. typeset -A engines
  87. engines=($ZSH_WEB_SEARCH_ENGINES)
  88. for key in ${(k)engines}; do
  89. alias "$key"="web_search $key"
  90. done
  91. unset engines key
  92. fi