web-search.plugin.zsh 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. google "https://www.google.com/search?q="
  8. bing "https://www.bing.com/search?q="
  9. yahoo "https://search.yahoo.com/search?p="
  10. duckduckgo "https://www.duckduckgo.com/?q="
  11. startpage "https://www.startpage.com/do/search?q="
  12. yandex "https://yandex.ru/yandsearch?text="
  13. github "https://github.com/search?q="
  14. baidu "https://www.baidu.com/s?wd="
  15. ecosia "https://www.ecosia.org/search?q="
  16. goodreads "https://www.goodreads.com/search?q="
  17. qwant "https://www.qwant.com/?q="
  18. givero "https://www.givero.com/search?q="
  19. stackoverflow "https://stackoverflow.com/search?q="
  20. )
  21. # check whether the search engine is supported
  22. if [[ -z "$urls[$1]" ]]; then
  23. echo "Search engine $1 not supported."
  24. return 1
  25. fi
  26. # search or go to main page depending on number of arguments passed
  27. if [[ $# -gt 1 ]]; then
  28. # build search url:
  29. # join arguments passed with '+', then append to search engine URL
  30. url="${urls[$1]}${(j:+:)@[2,-1]}"
  31. else
  32. # build main page url:
  33. # split by '/', then rejoin protocol (1) and domain (2) parts with '//'
  34. url="${(j://:)${(s:/:)urls[$1]}[1,2]}"
  35. fi
  36. open_command "$url"
  37. }
  38. alias bing='web_search bing'
  39. alias google='web_search google'
  40. alias yahoo='web_search yahoo'
  41. alias ddg='web_search duckduckgo'
  42. alias sp='web_search startpage'
  43. alias yandex='web_search yandex'
  44. alias github='web_search github'
  45. alias baidu='web_search baidu'
  46. alias ecosia='web_search ecosia'
  47. alias goodreads='web_search goodreads'
  48. alias qwant='web_search qwant'
  49. alias givero='web_search givero'
  50. alias stackoverflow='web_search stackoverflow'
  51. #add your own !bang searches here
  52. alias wiki='web_search duckduckgo \!w'
  53. alias news='web_search duckduckgo \!n'
  54. alias youtube='web_search duckduckgo \!yt'
  55. alias map='web_search duckduckgo \!m'
  56. alias image='web_search duckduckgo \!i'
  57. alias ducky='web_search duckduckgo \!'