-
Notifications
You must be signed in to change notification settings - Fork 10
/
web_search.plugin.zsh
61 lines (50 loc) · 1.52 KB
/
web_search.plugin.zsh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
## depending of xdg-utils
## https://github.com/theskumar/dotfiles/blob/master/.zsh/plugins/web_search/web_search.plugin.zsh
function web_search() {
# get the open command
local open_cmd
[[ "$OSTYPE" = linux* ]] && open_cmd='xdg-open'
[[ "$OSTYPE" = darwin* ]] && open_cmd='open'
pattern='(google|duckduckgo|bing|yahoo|github|youtube)'
# check whether the search engine is supported
if [[ $1 =~ pattern ]];
then
echo "Search engine $1 not supported."
return 1
fi
local url
[[ "$1" == 'yahoo' ]] && url="https://search.yahoo.com" || url="https://www.$1.com"
# no keyword provided, simply open the search engine homepage
if [[ $# -le 1 ]]; then
$open_cmd "$url"
return
fi
typeset -A search_syntax=(
google "/search?q="
bing "/search?q="
github "/search?q="
duckduckgo "/?q="
yahoo "/search?p="
youtube "/results?search_query="
)
url="${url}${search_syntax[$1]}"
shift # shift out $1
while [[ $# -gt 0 ]]; do
url="${url}$1+"
shift
done
url="${url%?}" # remove the last '+'
nohup $open_cmd "$url" &> /dev/null
}
alias bing='web_search bing'
alias google='web_search google'
alias yahoo='web_search yahoo'
alias ddg='web_search duckduckgo'
alias github='web_search github'
alias youtube='web_search youtube'
#add your own !bang searches here
alias wiki='web_search duckduckgo \!w'
alias news='web_search duckduckgo \!n'
alias map='web_search duckduckgo \!m'
alias image='web_search duckduckgo \!i'
alias ducky='web_search duckduckgo \!'