web-search.plugin.zsh 1.21 KB
Newer Older
Hong Xu's avatar
Hong Xu committed
1
2
3
4
5
# web_search from terminal

function web_search() {
  # get the open command
  local open_cmd
6
  if [[ "$OSTYPE" = darwin* ]]; then
Hong Xu's avatar
Hong Xu committed
7
8
9
10
11
12
    open_cmd='open'
  else
    open_cmd='xdg-open'
  fi

  # check whether the search engine is supported
stibinator's avatar
stibinator committed
13
  if [[ ! $1 =~ '(google|bing|yahoo|duckduckgo)' ]];
Hong Xu's avatar
Hong Xu committed
14
15
16
17
18
19
20
21
22
23
24
25
  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
26
27
28
29
30
31
  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
32
33
34
35
36
37
38
39
  shift   # shift out $1

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

  url="${url%?}" # remove the last '+'
40
41
  nohup $open_cmd "$url" 
 	rm nohup.out	
Hong Xu's avatar
Hong Xu committed
42
43
}

stibinator's avatar
stibinator committed
44

Hong Xu's avatar
Hong Xu committed
45
46
47
alias bing='web_search bing'
alias google='web_search google'
alias yahoo='web_search yahoo'
stibinator's avatar
stibinator committed
48
alias ddg='web_search duckduckgo'
stibinator's avatar
stibinator committed
49
50
51
52
53
54
#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
55
alias ducky='web_search duckduckgo \!'