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

function web_search() {
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
  emulate -L zsh

  # define search engine URLS
  typeset -A urls
  urls=(
    google      "https://www.google.com/search?q="
    bing        "https://www.bing.com/search?q="
    yahoo       "https://www.yahoo.com/search?q="
    duckduckgo  "https://www.duckduckgo.com/?q="
  )

  # define the open command
  case "$OSTYPE" in
    darwin*)  open_cmd="open" ;;
    linux*)   open_cmd="xdg-open" ;;
    *)        echo "Platform $OSTYPE not supported"
              return 1
              ;;
  esac
Hong Xu's avatar
Hong Xu committed
23
24

  # check whether the search engine is supported
25
  if [[ -z "$urls[$1]" ]]; then
Hong Xu's avatar
Hong Xu committed
26
27
28
29
    echo "Search engine $1 not supported."
    return 1
  fi

30
31
32
33
34
  # search or go to main page depending on number of arguments passed
  if [[ $# -gt 1 ]]; then
    # build search url:
    # join arguments passed with '+', then append to search engine URL
    url="${urls[$1]}${(j:+:)@[2,-1]}"
stibinator's avatar
stibinator committed
35
  else
36
37
38
    # build main page url:
    # split by '/', then rejoin protocol (1) and domain (2) parts with '//'
    url="${(j://:)${(s:/:)urls[$1]}[1,2]}"
stibinator's avatar
stibinator committed
39
  fi
Hong Xu's avatar
Hong Xu committed
40

41
  nohup $open_cmd "$url" >/dev/null 2&>1
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 \!'