web-search.plugin.zsh 1.4 KB

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