web-search.plugin.zsh 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. )
  27. # check whether the search engine is supported
  28. if [[ -z "$urls[$1]" ]]; then
  29. echo "Search engine '$1' not supported."
  30. return 1
  31. fi
  32. # search or go to main page depending on number of arguments passed
  33. if [[ $# -gt 1 ]]; then
  34. # build search url:
  35. # join arguments passed with '+', then append to search engine URL
  36. url="${urls[$1]}$(omz_urlencode ${@[2,-1]})"
  37. else
  38. # build main page url:
  39. # split by '/', then rejoin protocol (1) and domain (2) parts with '//'
  40. url="${(j://:)${(s:/:)urls[$1]}[1,2]}"
  41. fi
  42. open_command "$url"
  43. }
  44. alias bing='web_search bing'
  45. alias brs='web_search brave'
  46. alias google='web_search google'
  47. alias yahoo='web_search yahoo'
  48. alias ddg='web_search duckduckgo'
  49. alias sp='web_search startpage'
  50. alias yandex='web_search yandex'
  51. alias github='web_search github'
  52. alias baidu='web_search baidu'
  53. alias ecosia='web_search ecosia'
  54. alias goodreads='web_search goodreads'
  55. alias qwant='web_search qwant'
  56. alias givero='web_search givero'
  57. alias stackoverflow='web_search stackoverflow'
  58. alias wolframalpha='web_search wolframalpha'
  59. alias archive='web_search archive'
  60. alias scholar='web_search scholar'
  61. alias ask='web_search ask'
  62. #add your own !bang searches here
  63. alias wiki='web_search duckduckgo \!w'
  64. alias news='web_search duckduckgo \!n'
  65. alias youtube='web_search duckduckgo \!yt'
  66. alias map='web_search duckduckgo \!m'
  67. alias image='web_search duckduckgo \!i'
  68. alias ducky='web_search duckduckgo \!'
  69. # other search engine aliases
  70. if [[ ${#ZSH_WEB_SEARCH_ENGINES} -gt 0 ]]; then
  71. typeset -A engines
  72. engines=($ZSH_WEB_SEARCH_ENGINES)
  73. for key in ${(k)engines}; do
  74. alias "$key"="web_search $key"
  75. done
  76. unset engines key
  77. fi