Fast Note Searching

I’ve been messing around with some different solutions for saving notes. My typical solution is just markdown documents organized into folders on dropbox. One fairly neat solution that I came up with for searching my notes was just to use some simple unix pipes and a couple command line tools.

Tools

Function

fv() {
    vim $(pt "" * | fzf | awk {'print $1'} | sed 's/\(.*\):\(.*\):.*/\1 \+\2/')
}

Explanation:

This is probably not the most sophisticated thing in the world, but here’s a quick explanation.

First, whatever is returned from our subshell $() will be sent to vim as arguments. What we are hoping for is something like vim file-name.md +59 where 59 is whatever line number we want to jump to.

The first two pipes are pretty simple, and can be used on their own elsewhere. I am simply piping an open ended pt (Platinum Searcher) search for the current directory into fzf. That gives us the ability to type out text and have live (fuzzy) matches returned and displayed.

Next, we run that through awk to pull out just the first column of the output from fzf. This should contain the filename and the line number.

Lastly, we run this through sed to separate the filename and line number (and add a ‘+’).

Execution

Throw the fv function above in your .zshrc (or .bashrc) file. You can use this function for other applications besides your notes if you would like, but my primary application is for notes.

I use Alfred as my OSX launcher. Create a simple workflow that hooks a keyword trigger (I chose ‘ns’ for Notes Search) to a terminal command action. In the terminal command, just use cd ~/Dropbox/Notes && fv.

That’s it. Open Alfred, type ‘ns’ hit return and your are searching your notes. Nothing earth shattering, but still kind of cool.