web-search.plugin.zsh 1.8 KB

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