web-search.plugin.zsh 2.2 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
    startpage   "https://www.startpage.com/do/search?q="
14
    yandex      "https://yandex.ru/yandsearch?text="
15
    github      "https://github.com/search?q="
16
    baidu       "https://www.baidu.com/s?wd="
17
    ecosia      "https://www.ecosia.org/search?q="
18
    goodreads   "https://www.goodreads.com/search?q="
19
    qwant       "https://www.qwant.com/?q="
20
    givero      "https://www.givero.com/search?q="
21
    stackoverflow  "https://stackoverflow.com/search?q="
22
    wolframalpha   "https://www.wolframalpha.com/input/?i="
23
    archive     "https://web.archive.org/web/*/"
24
25
  )

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

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

43
  open_command "$url"
Hong Xu's avatar
Hong Xu committed
44
45
}

stibinator's avatar
stibinator committed
46

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

stibinator's avatar
stibinator committed
63
64
65
66
67
68
#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
69
alias ducky='web_search duckduckgo \!'