web-search.plugin.zsh 1.5 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
  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="
11
    yahoo       "https://search.yahoo.com/search?p="
12
    duckduckgo  "https://www.duckduckgo.com/?q="
13
    yandex      "https://yandex.ru/yandsearch?text="
14
    github      "https://github.com/search?q="
15
    baidu       "https://www.baidu.com/s?wd="
16
17
  )

Hong Xu's avatar
Hong Xu committed
18
  # check whether the search engine is supported
19
  if [[ -z "$urls[$1]" ]]; then
Hong Xu's avatar
Hong Xu committed
20
21
22
23
    echo "Search engine $1 not supported."
    return 1
  fi

24
25
26
27
28
  # 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
29
  else
30
31
32
    # 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
33
  fi
Hong Xu's avatar
Hong Xu committed
34

35
  open_command "$url"
Hong Xu's avatar
Hong Xu committed
36
37
}

stibinator's avatar
stibinator committed
38

Hong Xu's avatar
Hong Xu committed
39
40
41
alias bing='web_search bing'
alias google='web_search google'
alias yahoo='web_search yahoo'
stibinator's avatar
stibinator committed
42
alias ddg='web_search duckduckgo'
43
alias yandex='web_search yandex'
44
alias github='web_search github'
45
alias baidu='web_search baidu'
46

stibinator's avatar
stibinator committed
47
48
49
50
51
52
#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
53
alias ducky='web_search duckduckgo \!'