web-search.plugin.zsh 1.19 KB
Newer Older
Hong Xu's avatar
Hong Xu committed
1
2
3
4
5
6
7
8
9
10
11
12
13
# web_search from terminal

function web_search() {

  # get the open command
  local open_cmd
  if [[ $(uname -s) == 'Darwin' ]]; then
    open_cmd='open'
  else
    open_cmd='xdg-open'
  fi

  # check whether the search engine is supported
stibinator's avatar
stibinator committed
14
  if [[ ! $1 =~ '(google|bing|yahoo|duckduckgo)' ]];
Hong Xu's avatar
Hong Xu committed
15
16
17
18
19
20
21
22
23
24
25
26
  then
    echo "Search engine $1 not supported."
    return 1
  fi

  local url="http://www.$1.com"

  # no keyword provided, simply open the search engine homepage
  if [[ $# -le 1 ]]; then
    $open_cmd "$url"
    return
  fi
stibinator's avatar
stibinator committed
27
28
29
30
31
32
  if [[ $1 == 'duckduckgo' ]]; then
  #slightly different search syntax for DDG
    url="${url}/?q="
  else
    url="${url}/search?q="
  fi
Hong Xu's avatar
Hong Xu committed
33
34
35
36
37
38
39
40
  shift   # shift out $1

  while [[ $# -gt 0 ]]; do
    url="${url}$1+"
    shift
  done

  url="${url%?}" # remove the last '+'
stibinator's avatar
stibinator committed
41
  
Hong Xu's avatar
Hong Xu committed
42
43
44
  $open_cmd "$url"
}

stibinator's avatar
stibinator committed
45

Hong Xu's avatar
Hong Xu committed
46
47
48
alias bing='web_search bing'
alias google='web_search google'
alias yahoo='web_search yahoo'
stibinator's avatar
stibinator committed
49
50
51
52
53
54
55
alias duck='web_search duckduckgo'
#add your own !bang searches here
alias wiki='web_search duckduckgo \!w'
alias news='web_search duckduckgo \!n'
alias youtube='web_search duckduckgo \!yt'
alias map='web_search duckduckgo \!m'
alias image='web_search duckduckgo \!i'
stibinator's avatar
stibinator committed
56
alias ducky='web_search duckduckgo \!'