web-search.plugin.zsh 2.55 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
  emulate -L zsh

  # define search engine URLS
  typeset -A urls
  urls=(
9
    $ZSH_WEB_SEARCH_ENGINES
10
11
    google      "https://www.google.com/search?q="
    bing        "https://www.bing.com/search?q="
12
    yahoo       "https://search.yahoo.com/search?p="
13
    duckduckgo  "https://www.duckduckgo.com/?q="
14
    startpage   "https://www.startpage.com/do/search?q="
15
    yandex      "https://yandex.ru/yandsearch?text="
16
    github      "https://github.com/search?q="
17
    baidu       "https://www.baidu.com/s?wd="
18
    ecosia      "https://www.ecosia.org/search?q="
19
    goodreads   "https://www.goodreads.com/search?q="
20
    qwant       "https://www.qwant.com/?q="
21
    givero      "https://www.givero.com/search?q="
22
    stackoverflow  "https://stackoverflow.com/search?q="
23
    wolframalpha   "https://www.wolframalpha.com/input/?i="
24
    archive     "https://web.archive.org/web/*/"
25
    scholar        "https://scholar.google.com/scholar?q="
26
27
  )

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

34
35
36
37
38
  # 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
39
  else
40
41
42
    # 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
43
  fi
Hong Xu's avatar
Hong Xu committed
44

45
  open_command "$url"
Hong Xu's avatar
Hong Xu committed
46
47
}

stibinator's avatar
stibinator committed
48

Hong Xu's avatar
Hong Xu committed
49
50
51
alias bing='web_search bing'
alias google='web_search google'
alias yahoo='web_search yahoo'
stibinator's avatar
stibinator committed
52
alias ddg='web_search duckduckgo'
53
alias sp='web_search startpage'
54
alias yandex='web_search yandex'
55
alias github='web_search github'
56
alias baidu='web_search baidu'
57
alias ecosia='web_search ecosia'
58
alias goodreads='web_search goodreads'
59
alias qwant='web_search qwant'
60
alias givero='web_search givero'
61
alias stackoverflow='web_search stackoverflow'
62
alias wolframalpha='web_search wolframalpha'
63
alias archive='web_search archive'
64
alias scholar='web_search scholar'
65

stibinator's avatar
stibinator committed
66
67
68
69
70
71
#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
72
alias ducky='web_search duckduckgo \!'
73
74
75
76
77
78
79
80
81
82

# other search engine aliases
if [[ ${#ZSH_WEB_SEARCH_ENGINES} -gt 0 ]]; then
  typeset -A engines
  engines=($ZSH_WEB_SEARCH_ENGINES)
  for key in ${(k)engines}; do
    alias "$key"="web_search $key"
  done
  unset engines key
fi