In this post, I am going to implement an interactive bookmarks menu using fzf in bash Terminal.
Requirements
- Linux (I'm using Elementary OS, a Ubuntu-based distro, try it!)
- fzf command-line fuzzy finder
- Remember to add execution permissions to the scripts through
chmod +x
Demo
As you see, every time I call listBookmarks
the following paths are listed interactively:
- /home/cangulo/repos/cangulo-tf
- /home/cangulo/repos/cangulo-nuke
I can move through them using the up/down keys, and navigate into by pressing Enter.
On the other hand, in case similar paths are listed, I can filter by typing keywords.
Implementation
#!/bin/bash
listBookmarks() {
local bookmarksFile=./bookmarks.json
local pathSelected=$(cat $bookmarksFile |
jq -r '.[]' |
fzf)
if [[ -n "$pathSelected" ]]; then
cd $pathSelected
else
echo "no bookmark selected"
fi
}
listBookmarks function
Let me define the basic structure:
- Read the paths (i.e. bookmarks)
- List them in an interactive way
- Once a bookmark is selected, navigate to it
Now let's dive into the details:
1. Read the bookmarks
First, in order to make this extensible, the paths are listed in a JSON file:
[
"/home/cangulo/repos/cangulo-tf",
"/home/cangulo/repos/cangulo-nuke"
]
bookmarks
We can print the file content using cat
, and then use jq
to query the array items.
cat $bookmarksFile | jq -r '.[]'
Why I'm using a JSON file instead of a simple text file. Click here for the answer:
It is because in the article Interactive bookmarks menu v2 I will add new features with more advanced bookmarks ๐
2. List the bookmarks
Here is where fzf
comes up. Anything that fzf
receives is listed interactively. In this case, we pipe the jq
result to fzf
, so the bookmarks are listed as shown in the demo section.
cat $bookmarksFile | jq -r '.[]' | fzf
3. Once a bookmark is selected, navigate to it
fzf
returns the path selected, we save it to a variable.
local pathSelected=$(cat $bookmarksFile | jq -r '.[]' | fzf)
Last, we check if it is not empty (-n
) before moving into:
if [[ -n "$pathSelected" ]]; then
cd $pathSelected
else
echo "no bookmark selected"
fi
Bonus: execute this every time you open a terminal
You have to add this in your bash or zsh profile (.bashrc, .zshrc ). Just append the listBookmarks
implementation at the end, update the bookmarksFile
variable to be a full path, and call the function.
listBookmarks() {
local bookmarksFile='YOUR_PATH/bookmarks.json'
local pathSelected=$(cat $bookmarksFile |
jq -r '.[]' |
fzf)
if [[ -n "$pathSelected" ]]; then
cd $pathSelected
else
echo "no bookmark selected"
fi
}
listBookmarks
And that is all! I hope this saves you some time using the terminal. Do you find this useful? Do you have similar functions? Let me know in the comments below.
About me
I'm a Software Engineer with experience as Developer and DevOps. The technologies I have worked with are DotNet, Terraform and AWS. For the last one, I have the Developer Associate certification. I define myself as a challenge-seeker person and team player. I simply give it all to deliver high-quality solutions. On the other hand, I like to analyze and improve processes, promote productivity and document implementations (yes, I'm a developer that likes to document ๐งโ๐ป).
You can check my experience here.
Personal Blog - cangulo.github.io
GitHub - Carlos Angulo Mascarell - cangulo
LinkedIn - Carlos Angulo Mascarell
Twitter - @AnguloMascarell