Fuzzy Logic and Goto Anything pane in Sublime Text 3

This is just a brief memo on how fuzzy logic and how “Goto Anything” (Ctrl+P) pane works in Sublime Text 3.

Please, visit Stuart Herbert blog for more detailed explanation along with a few videos on this subject.

Fuzzy logic

In general, fuzzy logic used in Sublime text is a search-and-find technology, that is actually trying to guess, what you’re trying to find.

It does it, by analysing:

  • your previous searches,
  • movements between files
  • search keyword itself of course
  • and some more meta-data

and it does its job perfectly.

Yes, I mean it — it does analyse your previous movements, by actually checking how popular particular file is (how often it was opened by you) and gives you more popular files higher on results list.

Fuzzy logic generally mixes two factors:

  • what you wrote, as search input (match quality — how much particular result matches your current search query),
  • what you done before (match popularity — how often particular file was opened by you previously).

As you’ll see in the examples below, even first point isn’t something you may find in other editors. Because there’s much powerful search engine behind search functionality in Sublime Text 3. And second point — it is an absolute gem among all the search engines, editors and IDEs, I’ve came so far.

Fuzzy logic is one of new software features, that you wouldn’t expect to need (when using old software), until you discovered it.

Believe me, or not, give yourself a week try and you won’t be able to do anything without it.

Goto anything

“Goto Anything” pane is a search-anything functionality in Sublime Text, that utilizes fuzzy logic.

To open this pane, press Ctrl+P shortcut and start typing anything. List of results will appear below. Scroll between them using up and down keys. As you highlight any search result, it is loaded in background in preview mode.

With first letters you’ll see it just like ordinary search mechanism, that is found everywhere.

But, when you type mccnf and see, that myCamelCaseNamedFunction is found as well, you may start to wonder, if you ever seen something similar. Probably, not. That is, how fuzzy logic works here.

Numbers on the left side of search results describes the match relevance:

  • the more you type (the more result matches your search) and
  • the more particular file is “popular” (see above)

the higher number (rank) it will give. Keep in mind, that this is not 0-100 (percentage) scale.

Note, that “Goto anything” searches only files and folders added to sidebar. Which is quite obvious. And even, if you add few large projects to your sidebar, it is super-fast in searching! Read more about it in unofficial docs.

More pro usage

These are modifications that you can use in “Goto Anything” pane:

Search and find the line (:)

It lets you select line of code:

  1. If you use it in the beginning of search, it will find and highlight that line in currently opened file.
  2. If you use it after entering some characters, it will find given line in currently highlighted search result.

You can use Ctrl+G shortcut to bring “Goto Anything” pane with : character already entered, letting you search for a line in current file at once.

Search and find a named symbol (@)

Works exactly as above, but finds named symbols in current file or highlighted search result — matching functions, methods and classes:

  1. If entered as single character (without any text following) causes “Goto Anything” pane to list all methods in current file and let you browse through them with up and down keys.
  2. If you start anything once you entered @ it will narrow search results.

You can use Ctrl+R shortcut to bring “Goto Anything” pane with @ character already entered.

Search and… search (#)

Same as above, but this time let’s you browse all words that are in file, ordered by number of occurrences.

It works just, as you would use Ctrl+F search box. As you browse through list, it enters highlighted word into search box.

Don’t get confused. List is ordered by number of occurrences with “whole word only” option turned on, while usually search box has this option turned off and searches for partial matches as well. That is why, you may find word with higher number of occurrences placed lower on list than other words will less number of occurrences.

You can use Ctrl+; shortcut to bring “Goto Anything” pane with # character inside.

Command Palette

Fuzzy logic is not limited to “Goto Anything” pane. It is used in “Command Palette” (Ctrl+Shift+P) as well.

It is used to browser all “commands” available in Sublime Text 3. That is:

  • menus
  • shortcuts
  • snippets
  • plugins
  • macros etc.

Yes, you heard it — even menus. ST3 is the only known to me program, that allows you to select any menu item, using just the keyboard:

  • without need of pressing Alt+something combinations and
  • staring your eyes to see those damn small underlines below single letters.

For example… How much time would an average developer need to select View > Indentation > Convert Indentation to Spaces menu item? To execute this third-level menu item in Sublime Text 3 you only need to press Ctrl+Shift+P, write icts and hit Enter! Voila

Fuzzy logic works in “Command Palette” exactly as in above examples:

  1. You can write just vtt, to have View > Toggle Tabs menu item selected.
  2. It uses “popularity” analysis to bring you more often used elements to the top of search results.
  3. For example after using View > Toggle Tabs few times, you’ll only have to write v to get it searched and selected/

Really cool, isn’t that?

I know, where you did that

Sublime Text 3 offers you a standard undo (Ctrl+Z) and redo (Ctrl+Y) functionality, as most editors. In addition, it not only remembers, what you did, but where you did that. You can use Alt+- and Alt+Shift+- shortcuts to navigate through places of your previous edits.

Using Alt+1 through Alt+9 shortcuts you can very quickly change active tab to desired one. Standard Ctrl+Tab and Ctrl+Shift+Tab also works. But again — it is an enhanced version of this mechanism that also remembers which tabs where visited by you and when.

As you use these shortcut, Sublime Text 3 browsers all tabs not from left to right (or backward), as in most IDEs, but with full respect to the order, in which you visited all currently opened tabs. This might be little bit confusing for people, that are not used to this, but after some time it turns out to be extremely handy.

At last

As you can see, fuzzy logic and “Goto Anything” / “Command Palette” panes really kicks ass!

I’ve been using it for about few hours and I still can’t stop wondering, how did I managed to survive my previous five or more years with Netbeans and without it? :]

Leave a Reply