Skip to content

Instantly share code, notes, and snippets.

@kidpixo
Last active March 4, 2021 16:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kidpixo/6faaf4404260be21040951cdfca97dcb to your computer and use it in GitHub Desktop.
Save kidpixo/6faaf4404260be21040951cdfca97dcb to your computer and use it in GitHub Desktop.
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