August 22, 2022
I was looking for a command line utility to take notes from my Linux terminal. I was looking for a simple thing and, although I found several, even the simplest were overkill for my needs. Many MB of disk, weird dependencies (like databases, or dev libraries, or git for some reason).
This note browsing system had to be navigatable. I also looked at
some wiki engines. Super overkill for my goal. But something like
hyperlinks was necessary. I pondered using html. Eventually, I chose the
Vim gf
, or “go file”, built-in command. It has basically
the same functionality as html.
So I wrote this BASH script:
#!/bin/bash
_FILE=$(mktemp)
_LASTSEARCH="/tmp/lastsearch.txt"
generate_list_of_files () {
grep -lir "$1" *.* > "$_FILE"
cp "$_FILE" "$_LASTSEARCH"
}
case $1 in
"")
_FILE="$_LASTSEARCH";;
*)
generate_list_of_files "$1";;
esac
vim "$_FILE"
rm "$_FILE"
Just copy that chunk into a file, save it, and make it executable. I
named it browse.sh
, I put it in my ~/bin
and
made it executable with chmod +x ~/bin/browse.sh
. You do
something like this.
The script can be invoked from anywhere in the file system, but it
only works with files in the pwd
. Keep this in mind. So,
you have to have your notes in independent, separate text files (I use
plain text and markdown for this), all in the pwd
. Then
invoke it like this:
$ browse.sh "and the winner is"
That is, only one argument in quotes, or without quotes if it is one-word.
If you don’t provide an argument, the script will retrieve the last performed search to work with.
The script will next look for all the files in the pwd
that contain that string of text, and create a text file with the names
of those files. Then, it will open it in Vim for you.
Here is when Vim proves itself excellent for the task of note-taking.
Just navigate through the lines with file names with the regular
j
and k
keys, and select the desired file to
edit by pressing gf
, which stands for “go to file”. This
Vim functionality just emulates hyperlinks. Vim will open the selected
file in a new buffer. Just go back to the buffer with the file names and
select a different one, untill you get the note you were looking for.
Vim makes note-viewing very easy, because viewing a file in Vim is also
having the ability to editing it, so go to town.
All the credit goes to Vim. This BASH script would be useless without the ability to “go to files” from inside Vim.
Feel free to email me your comments at gasconheart@sdf.org.