2
0

web-search.plugin.zsh 3.7 KB

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