web-search.plugin.zsh 1.6 KB

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