During years of my using Linux, I have collected tiny useful scripts and in last 2 years learned a bit of bash and made few. I guess the same for other Linux terminal users, so it may be good idea to share some useful tiny scripts.
I will begin with my bookmark handler/ url opener script. I keep my bookmarks in a text file, and launch this script which gives a rofi (dmenu clone) menu with all bookmarks and I can do fuzzy selection by typing few words.
However that is not all. I can search on different sites straight from this script using keywords, for example "wiki big bang theory" will search big bang theory on wikipedia or I can type a valid url to open with browser.
To try this script you will need rofi and surfraw installed. There are plenty of keywords to try, you can find them in script after "case" line. For beginners to bash, you will need to save this script as a text file and make it executable ( chmod +x <script_name> ). It will be best if you save the script in your $PATH, ( /usr/local/bin ) I keep all my scripts in $HOME/bin and add that in $PATH from my .bashrc. I hope this thread will be useful to all, and many more useful scripts will be shared.
You can also run <script_name> -h for in-build help.
I will begin with my bookmark handler/ url opener script. I keep my bookmarks in a text file, and launch this script which gives a rofi (dmenu clone) menu with all bookmarks and I can do fuzzy selection by typing few words.
However that is not all. I can search on different sites straight from this script using keywords, for example "wiki big bang theory" will search big bang theory on wikipedia or I can type a valid url to open with browser.
To try this script you will need rofi and surfraw installed. There are plenty of keywords to try, you can find them in script after "case" line. For beginners to bash, you will need to save this script as a text file and make it executable ( chmod +x <script_name> ). It will be best if you save the script in your $PATH, ( /usr/local/bin ) I keep all my scripts in $HOME/bin and add that in $PATH from my .bashrc. I hope this thread will be useful to all, and many more useful scripts will be shared.
You can also run <script_name> -h for in-build help.
Bash:
#!/bin/bash
INFO() {
cat <<EOF>&2
### DEPENDS ON ###
"surfraw"-for keyword based search, you will need /usr/lib/surfraw in your path, so add below line to your .bashrc (uncommented)
PATH=/usr/lib/surfraw:${PATH}
"trans" and "w3m" for language translations, https://github.com/soimort/translate-shell
"sdcv" for dictionary use
### CONFIGURATION ###
place your browser in BROWSER=? line
place your bookmarks text file with 1 url/line, put it in "BOOKMARKS=?" field
default script uses rofi, to use dmenu uncomment the line starting with dmenu and comment out previous line starting with rofi
### USE ###
enter text in rofi/dmenu field, it will be matched against bookmarks, <enter> to open selected bookmark
to search specific sites use keyword as first argument ( like go for google, wi for wikipedia, aw for archwiki etc.) > read the script to find out all keywords
to find out route from delhi to mumbai with google map, "nav delhi mumbai" or to search a location only use "map <location>"
if no keyword or url is used, entire argument will be searched with duckduckgo
to search with input text only (not matching bookmark) use <C-enter> (rofi only)
to edit selected bookmark use <C-space> (rofi only)
EOF
}
while getopts h: help; do
case $help in
h|\?) INFO && exit ;;
esac
done
shopt -s lastpipe
## Add name of your browser here ( not text-browsers )
#BROWSER=qutebrowser
## add your plain text bookmarks here, you can add tag after the bookmark bookmart url e.g., "www.google.co.in search googlesearch", use "link-handler" for adding bookmarks
BOOKMARKS=~/.config/vimb/bookmark
export PATH=${HOME}/bin:${PATH}
export PATH=/usr/lib/surfraw:${PATH}
# use rofi to display bookmarks and select one
rofi -dmenu -i -matchin fuzzy -location 1 -l 10 -width 100 -font "Inconsolata 14" -p $BROWSER < "$BOOKMARKS" | read -a "url"
## use dmenu inplace of rofi
#/usr/bin/dmenu -l 10 -fn "Inconsolata 14" -p $BROWSER: < "$BOOKMARKS" | read -a url
[[ ! $url ]] && exit
duckimage() {
x=$@
$BROWSER "https://duckduckgo.com/?q=${x// /+}&ia=images&iax=1"
}
duckvideo() {
x=$@; $BROWSER "https://duckduckgo.com/?q=${x// /+}&ia=videos&iax=1"
}
DDG() {
x=$@; $BROWSER "https://duckduckgo.com/?q=${x// /+}"
}
MED() {
x=$@; $BROWSER "http://medical-dictionary.thefreedictionary.com/${x// /+}"
}
FLIPKART() {
x=$@; $BROWSER "https://www.flipkart.com/search?q=${x// /+}"
}
NAV() {
$BROWSER "https://maps.google.com/maps?saddr=$1&daddr=$2"
}
case "${url[0]}" in
*.*|*:*|*/*) $BROWSER "${url[0]}" ;;
aw|awiki) archwiki -browser=$BROWSER "${url[@]:1}" ;;
wi|wiki) wikipedia -browser=$BROWSER "${url[@]:1}" ;;
imdb) imdb -browser=$BROWSER "${url[@]:1}" ;;
aur) aur -browser=$BROWSER "${url[@]:1}" ;;
pkg) archpkg -browser=$BROWSER "${url[@]:1}" ;;
ddg|S|dd) DDG "${url[@]:1}" ;;
go|google) google -browser=$BROWSER "${url[@]:1}" ;;
map) google -m -browser=$BROWSER "${url[@]:1}" ;;
nav) NAV "${url[1]}" "${url[2]}" ;;
# image) google -i -browser=$BROWSER "${url[@]:1}" ;;
image) duckimage "${url[@]:1}" ;;
# video) google -v -browser=$BROWSER "${url[@]:1}" ;;
video) duckvideo "${url[@]:1}" ;;
news) google -n -browser=$BROWSER "${url[@]:1}" ;;
yt|youtube) youtube -browser=$BROWSER "${url[@]:1}" ;;
ebay) ebay -browser=$BROWSER "${url[@]:1}" ;;
fk|flipkart) FLIPKART "${url[@]:1}" ;;
pubmed) pubmed -browser=$BROWSER "${url[@]:1}" ;;
git|github) github -browser=$BROWSER "${url[@]:1}" ;;
## LOCAL DICTIONARIES
## to use def/guj/hin for dictionaries sdcv needs to setup with working dictionaries.
eng) notify-send "$( sdcv --data-dir ~/dic/english/ "${url[@]:1}" )" && exit ;;
guj) notify-send "$( sdcv --data-dir ~/dic/gujarati/ "${url[@]:1}" )" && exit ;;
hin) notify-send "$( sdcv --data-dir ~/dic/hindi/ "${url[@]:1}" )" && exit ;;
def) notify-send "$( sdcv --data-dir ~/dic/extra/ "${url[@]:1}" )" && exit ;;
## ONLINE TRANSLATION
## to use translation "trans" should be in path with w3m installed, translates to english
trans) notify-send "$( trans -brief "${url[@]:1}" )" && exit ;;
## translates to gujarati
trans_gu) notify-send "$( trans -brief :gu "${url[@]:1}" )" && exit ;;
## online medical dictionary
med) MED "${url[@]:1}" ;;
*) if [[ $BROWSER != dillo ]]; then
duckduckgo -j -browser=$BROWSER "${url[@]}"
else
google -browser=$BROWSER "${url[@]}"
fi ;;
esac
Last edited: