Manage Bookmarks with fish, fzf, and awk.

Tags: fish fzf awk plain-text 

Contents

Introduction

For the past few years, I have managed my bookmarks using a simple fish shell script and plain text files.

I use this script on all my devices - computers and Android phones (with Termux).

The script relies on GNU Awk and fzf.

The script

The code for my bookmark.fish script given below:

function bookmark
  if test -d "$HOME/RESOURCES"
    set browser "librewolf"
    set dir ~/RESOURCES/Bookmarks/
    set bk_file ~/RESOURCES/Bookmarks/bookmark.txt
    set rl_file ~/RESOURCES/Bookmarks/readlater.txt
    set ar_file ~/RESOURCES/Bookmarks/archive-reads.txt
  else
    set browser "termux-open-url"
    set dir ~/storage/shared/Documents/COMPUTER/RESOURCES/Bookmarks/
    set bk_file ~/storage/shared/Documents/COMPUTER/RESOURCES/Bookmarks/bookmark.txt
    set rl_file ~/storage/shared/Documents/COMPUTER/RESOURCES/Bookmarks/readlater.txt
    set ar_file ~/storage/shared/Documents/COMPUTER/RESOURCES/Bookmarks/archive-reads.txt
  end

  # open links
  set enter "become($browser {-1})"
  set ctrl_o "execute-silent(open {-1} >/dev/null 2>&1 &)"

  # edit file
  set alt_e "execute(rg -l {-1} $dir | xargs $EDITOR)"

  # copy link
  set ctrl_y "execute(echo -n {-1} | fish_clipboard_copy)+abort"

  # change mode/file
  set ctrl_b "reload(awk '!/^(\$|#)/' $bk_file)+change-prompt(Bookmarks> )+change-preview-window(up,1)+unbind(tab,ctrl-b)+rebind(change,ctrl-z,ctrl-r)"
  set ctrl_r "reload(awk '!/^(\$|#)/' $rl_file)+change-prompt(Readlater> )+change-preview-window(hidden|)+unbind(change,ctrl-r)+rebind(ctrl-z,ctrl-b,tab)"
  set ctrl_z "reload(awk '!/^(\$|#)/' $ar_file)+change-prompt(Archive> )+change-preview-window(up,1)+unbind(tab,ctrl-z)+rebind(change,ctrl-b,ctrl-r)"

  # add to archive
  set alt_z "execute-silent(test -n {q} && date +'%F  {q}' >> $ar_file || date +'%F  {-1}' >> $ar_file )+reload(awk '!/^(\$|#)/' $ar_file)"

  # add to readlater
  set alt_r "execute-silent(test -n {q} && echo {q} >> $rl_file || echo {-1} >> $rl_file )+reload(awk '!/^(\$|#)/' $rl_file)"

  # note: I edit the main bookmark.txt file manually.

  awk '!/^($|#)/' $rl_file | fzf\
    -e --multi +s \
    --preview='echo {-1}'\
    --preview-window='hidden'\
    --query=(commandline) \
    --prompt="Readlater> "\
    --header 'Bookmarks | Readlater | Archive'\
    --bind="enter:$enter,ctrl-o:$ctrl_o"\
    --bind="alt-e:$alt_e"\
    --bind="ctrl-y:$ctrl_y"\
    --bind="ctrl-z:$ctrl_z,ctrl-b:$ctrl_b,ctrl-r:$ctrl_r"\
    --bind="alt-z:$alt_z,alt-r:$alt_r"\

  # clear commandline
  commandline -r -- $(echo "")
  commandline -f repaint
end

bind \e\cb bookmark

Breakdown

The script first checks whether its running on a computer or on a phone. It determines that by testing the $HOME directory structure.

Next, the script sets default variables for browser and bookmark path.

Before running fzf, the script assigns variables for keybindings.

Finally, the script uses awk to parse the content of the bookmark files and then pipes the output to fzf.

I bind the script to CTRL-ALT-B for quick execution.

Conclusion

This is a simple script that you can tweak to suit your needs.

The file is available in my dotfiles repo.

Reply to this post with email.

Go to Top
Return to Posts
Stay updated with RSS