Building a Customized Bookmark Workflow with fzf

Efficient Access to Web Sites via the Command Line

5 min readAug 16, 2023

--

https://pragprog.com/newsletter/

Many developers use the fuzzy finder program fzf to increase their productivity on the command line. You can use fzf to, for example, switch between different git branches, or find files and directories on your machine.

When you have a lot of bookmarked web sites, fzf can be a time-saving tool since it allows you to search on partial words or phrases — helpful if you don’t remember the exact name of what you’re looking for. Its appearance and functionality are also customizable.

In this how-to, you’ll use fzf to create a customized workflow for viewing, searching, and opening bookmarks in your web browser. Let’s jump in.

Selecting A Bookmark From a List with fzf

Let’s say you have a file named bookmarks.txt that contains your top 3 bookmarks, one url and one description per line:

https://pragprog.com The Pragmatic Bookshelf
https://example.com Example Domain
https://pragprog.com/titles/dmpython/intuitive-python/ Intuitive Python

Once you’ve installed fzf, you can pipe this bookmarks.txt file into the stdin of fzf to launch a list that you can interactively search and select from:

cat bookmarks.txt | fzf

When you select an entry using Enter, fzf will print the selected line back out. For example, if you pick the first entry, fzf prints:

https://pragprog.com The Pragmatic Bookshelf
cat bookmarks.txt | fzf

That’s helpful, we selected one line from our bookmarks.txt file. Let’s take it one step further and figure out how to alter the text displayed by fzf.

Altering the Text Displayed By fzf

When we’re searching through our bookmarks, it can be helpful to search through only our human friendly descriptions (and not the raw URL text).

Let’s adjust fzf so that it lists + searches only our bookmark descriptions, but displays the raw URL off to the side in a preview window:

cat bookmarks.txt | fzf --layout reverse --with-nth 2.. --preview='echo {1}'
cat bookmarks.txt | fzf — layout reverse — with-nth 2.. — preview=’echo {1}’

We added a few options to fzf: the --layout reverse option is a cosmetic adjustment that makes the interactive window render in a “top-down” style vs the default “bottom-up” style.

The --with-nth 2.. option controls the text that fzf displays and searches in its interactive window. 2.. means choose the second index in the text, and all remaining elements. The indexes, themselves, areawk-style: text is split on a space character and accessed using 1-based indices.

If we take each line in bookmarks.txt as examples, they are split into multiple indexed pieces like this:

1                    2   3         4
https://pragprog.com The Pragmatic Bookshelf
1                                                      2         3
https://pragprog.com/titles/dmpython/intuitive-python/ Intuitive Python
1                   2       3
https://example.com Example Domain

By saying --with-nth 2.., you instruct fzf to display the bookmark description: the text starting at position 2 of each line until the end of the line ...

The --preview command has access to the same awk-style string split as --with-nth. --preview='echo {1}' tells fzf to echo out the string at the first index (aka our url) and display it in the preview window.

💡 Optional enhancement: line wrapping. Right now, the preview window truncates text that is too long (for example,https://pragprog.com/title/dmpython/intuitive-python/ in the preceding gif is cut off). Pass --preview-window wrap to fzf so that preview text wraps onto the next line instead of truncating.

You’ve adjusted how fzf renders and displays the list of bookmarks, now let’s make the command more useful by automatically opening the chosen URL in your web browser.

Opening the URL in Your Web Browser

To open the bookmark in our web browser, we need our command to omit the chosen description like The Pragmatic Bookshelf and only output the chosen URL like https://pragprog.com. Let’s pipe the output of fzf to awk so we can use the same awk-style string split that fzfemploys to output only our chosen url:

cat bookmarks.txt \
| fzf --layout reverse --with-nth 2.. --preview='echo {1}' \
| awk '{print $1}'

The raw output of fzf is a line from our bookmarks.txt file like https://pragprog.com The Pragmatic Bookshelf. Passing that output to awk '{print $1}' allows us to employ the same string-splitting behavior we used in fzf so we can print out only index 1: the chosen url.

📝 The \ continuation characters in the preceding code sample allow us to split the command onto multiple lines for easier reading. They do not change the results of our command.

If you run the above command and select the first bookmark, the only output printed on your terminal will be the URL:

https://pragprog.com

For the last step, you need to run a command to automatically open the chosen URL in your web browser. Depending on your operating system, you might have a few options for doing this. One cross-platform option, however, is Python 3’s built-in webbrowser module:

cat bookmarks.txt \
| fzf --layout reverse --with-nth 2.. --preview='echo {1}' \
| awk '{print $1}' \
| xargs python3 -m webbrowser

Calling xargs converts the output of awk '{print $1}' (the chosen url) into an argument for the python3 -m webbrowser command — roughly like this: python3 -m webbrowser <chosen-url> . The webbrowser module opens the chosen URL in your default web browser.

So, for example, you can run your command, type book, and hit Enter to open https://pragprog.com:

fzf open in browser gif

At this point, you’ve put it all together: you can view and search your bookmarks in fzf with a customized display, and automatically open the chosen bookmark in your default web browser.

David Muller is the author of Intuitive Python:

Join the conversation about the book on DevTalk, where you will also find a promo code that saves you 35 percent on the ebook.

--

--

Professional software engineer, and author of Intuitive Python.