Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Open file from zathura history with rofi, checking if files exist. (Bash>=4.4 for array functions)
#!/bin/sh
# see https://unix.stackexchange.com/questions/467524/open-file-from-history-in-zathura
# save filepaths in history to array, reverse order
readarray -t Data < <(grep -o '/.*\.[[:alpha:]]\{,3\}' ~/.local/share/zathura/history | tac)
# loop and unset not existing files
for i in "${!Data[@]}"
do
if ! [ -e "${Data[$i]}" ]; then
# echo "${Data[$i]} does not exist."
unset 'Data[$i]'
# else
## Add substitition $HOME with ~ , really slow
# Data[$i]=$(echo ${Data[$i]} | sed "s#$HOME#~#")
# # echo "${Data[$i]} exists."
fi
done
selected=$( ( IFS=$'\n'; echo "${Data[*]}" ) | rofi -width 95 -dmenu -i -markup-rows)
# exit if nothing is selected
[[ -z $selected ]] && exit
# prevent opening not existing file
if [ -f "$selected" ]; then
# echo "$selected exists."
zathura "$selected"
else
# echo "$selected does not exist."
exit
fi
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment