web-search.plugin.zsh 785 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. # web_search from terminal
  2. function web_search() {
  3. # get the open command
  4. local open_cmd
  5. if [[ $(uname -s) == 'Darwin' ]]; then
  6. open_cmd='open'
  7. else
  8. open_cmd='xdg-open'
  9. fi
  10. # check whether the search engine is supported
  11. if [[ ! $1 =~ '(google|bing|yahoo)' ]];
  12. then
  13. echo "Search engine $1 not supported."
  14. return 1
  15. fi
  16. local url="http://www.$1.com"
  17. # no keyword provided, simply open the search engine homepage
  18. if [[ $# -le 1 ]]; then
  19. $open_cmd "$url"
  20. return
  21. fi
  22. url="${url}/search?q="
  23. shift # shift out $1
  24. while [[ $# -gt 0 ]]; do
  25. url="${url}$1+"
  26. shift
  27. done
  28. url="${url%?}" # remove the last '+'
  29. $open_cmd "$url"
  30. }
  31. alias bing='web_search bing'
  32. alias google='web_search google'
  33. alias yahoo='web_search yahoo'