Rust - fzf Alternative with Skim

08/29/2021, Sun
Categories: #rust #shell

Using an interactive CLI search tool can be a big productivity booster when you are uncertain of which files contain the term you are trying to seek out. You will end up performing multiple searches at a time and may jump into multiple files in doing so. GUI tools will be able to perform such a task, but using Skim offers you the ability to perform this task much faster since it is a keyboard focus tool with instant feedback in mind.

Skim by itself is a not an extensive search tool, and you will need a dedicated search CLI tool such as grep or rg to search within files to assist Skim in what you need to find. Skim's API is very similar to fzf as Skim is heavily inspired by it. You might want to use Skim when you prefer to only use Rust applications, and this library has the added convenience of being able to be used as a library too.

In addition to getting Skim onto your system, you will also need the "preview.sh" from the fzf.vi project because this will provide the functionality to get a glimpse into which line contains your search term on the right side of the terminal window.

This example will use the home directory to store the helper, "preview.sh" file, but you may go into a directory of your choice to store the "preview.sh" file.

Make it script executable

chmod a+x

The first example shown will use skim as a one-time search event in interactive mode like so

sk --ansi -i -c 'rg -l <search-term>'

The previous example shows the use of rg with a fixed search term, but this lacks flexibility in interactive mode as you can not type in a new term to update your search.

Now, to overcome this issue and get a sense of the real-time power of Skim, rg can be adjusted using the example below.

sk --ansi -i -c 'rg --color=always -l "{}"'

However, there is still something to be desired with using this command because you do not know exactly what lines contain the term within the file when only the filenames are display. We have downloaded the "preview.sh" file from before, but now is the time to use it to get more information from our searches.

sk --ansi -i -c 'rg --color=always -l "{}"' --preview "~/preview.sh {}"

Scroll through the preview window using the shift + Up Arrow or shift + Down Arrow to navigate the preview window.

You also have the option to pass in the filename into another external program using a keybinding. This command will open up VsCode and close Skim when an item is selected.

sk --bind 'f1:execute(code {}),ctrl-y)+abort'

Now to combine all basic features together for a real-time search and preview with hotkey binding to open a file with an external program

sk --ansi -i -c 'rg --color=always -l "{}"' --preview "~/preview.sh {}" --bind 'f1:execute-silent(code {}),ctrl-y)+abort'

Typing all the above out is tedious, and it will simplify your life when you create a shell function to wrap the above command in your startup shell script.

skc() {
  sk --ansi -i -c 'rg --color=always -l "{}"' --preview "~/preview.sh {}" --bind 'f1:execute-silent(code {}),ctrl-y)+abort'
}