27

When using ReSharper in Visual Studio symbols can be auto-completed using fuzzy matching instead of prefix matching. For example if I'm looking for a function named DoSomethingAmazing I could write DoSoAm and it would try to expand my query to match all symbols containing Do, So and Am somewhere in their name.

Another example can be found in the below gif. You can see several expansions where non-prefix strings are expanded to the correct symbol. (Ignore the snippet expansion as in this question I'm only concerned with fuzzy matching). A gif showing several examples of fuzzy matching in action

Another example can be seen in the YouCompleteMe plugin for Vim: A gif showing more examples of fuzzy matching in action in vim

Autocomplete and Company Mode both seem to have prefix matching but I can't find a way to enable fuzzy matching.

Ideally the solution would be language agnostic and apply to whatever symbol completion package I'm using but I'm also interested in language-specific solutions. I've looked at Helm but haven't found a way to apply it to code completion.

I have considered alternatives such as having a keybinding to activate symbol completion using a Helm buffer but I'm hoping there's a way to do this using popup-style completion.

I'm also open to doing manual configuration on a per-language basis since some languages separate symbols with dashes while others use underscores or dashes which I assume would affect how the matching would be done.

19

Emacs's builtin completion functionality offers similar fuzzy matching (tho by default it's used for "do-so-am" rather than for "DoSoAm") and IIUC recent versions of company-mode do use this at least in some cases.

E.g. if you open an Elisp buffer using Emacs-24.4 and the latest company-mode, if you type (wi-sy company will pop up a completion menu showing window-system, window-system-for-display and with-syntax-table.

  • 2
    With Emacs 24.4 company-mode supports completion-styles, this post suggests a possible setting for trying it out. – wasamasa Oct 27 '14 at 7:23
  • After installing 24.4 I've found that this is exactly what I was looking for. Unfortunately it seems to be language specific and will require changes to company backends to get it working for other languages. However out of the answers it's the closest I've found and hopefully with more time it'll apply to more languages. Thanks! – Jake Woods Nov 5 '14 at 10:29
  • company-ycmd is another backend that supports fuzzy completion. – Dmitry Nov 23 '14 at 1:38
9

With company-mode, you can use C-s to search from available candidates that it display. The search query is displayed in the modeline (not really easy to spot, I know. I filed an issue here).

If you want to use Helm for completion, consider using helm-company or ac-helm. Haven't tried with ac-helm, but with helm-company, you can directly bind it to a key to replace the standard interface of company-complete.

  • This was close to what I was looking for. I was hoping to get completion without having to invoke a keypress but I've found that using it in combination with prefix matching presents a suitable alternative. Thanks! – Jake Woods Nov 5 '14 at 10:30
  • If you use company-mode UI, you can press C-s to search for a candidate from a list of available candidates company presented to you. – Tu Do Nov 5 '14 at 11:07
  • Yep I understand that and like I said in the above comment that's close to what I was looking for. The ideal solution (for me) wouldn't require C-s and would search for the candidates inline in the popup window as I type. But the C-s approach is certainly interesting and I intend to integrate it into my workflow. – Jake Woods Nov 5 '14 at 23:06
  • While using company, I find C-M-s is way better than C-s. With meta, it will filter candidates instead of just searching. – nanny Nov 23 '14 at 1:04
  • @nanny great to know that. – Tu Do Nov 23 '14 at 5:03
5

As was mentioned in a comment earlier, ycmd supports fuzzy matching. You can using it via company-ycmd. Configured properly, you can get language-specific completion as well as simple identifier-based completion in a pretty seamless experience.

  • it would be nice to show what "properly" means here. – Jason Hu Jul 16 '17 at 13:32
2

There are several packages that offer fuzzy completion of one kind or another. I imagine that Helm does, for example (since you mention it).

If you use library Icicles then you can choose (on the fly) among several fuzzy-completion methods - more choice than I've seen offered elsewhere. (I am not particularly a fan of fuzzy completion, myself, but I offer it.)

Icicles completion (hence fuzzy completion, if you choose that) applies pretty much everywhere, but there are not a whole lot of Icicles-specific code-completion features. This page of the Icicles doc speaks about such buffer-text completion.


With Icicles, using Stefan's example of completing (wi-sy in Emacs Lisp mode:

  1. With Icicles vanilla completion (what Emacs provides, respecting completion-styles), you get the same symbols that Stefan mentions.

  2. With Icicles Swank fuzzy symbol completion (from library el-swank-fuzzy.el) as the current TAB-completion method, you get these completion candidates:

    window-system                
    with-syntax-table
    window-system-for-display
    widget-coding-system-action
    widget-symbol-prompt-internal
    widget-coding-system-prompt-value
    with-swfy-struct
    widget-specify-doc
    with-swfy-timedout
    widget-specify-field
    widget-specify-active
    widget-specify-button
    widget-specify-insert
    widget-specify-sample
    widget-specify-secret
    widget-specify-inactive
    window-safely-shrinkable-p
    widget-types-copy
    window-resize-apply
    window--resize-root-window-vertically
    window-display-table
    window--display-buffer
    window--resize-apply-p
    window-resize-apply-total
    widgetp-display-Completions
    window-redisplay-end-trigger
    with-displayed-buffer-window
    widget-visibility-value-create
    widget-emacs-library-link-action
    widget-editable-list-entry-create
    widget-emacs-commentary-link-action

The first 3 candidates are, again, those that Stefan mentions.

  1. With plain fuzzy-match completion (library fuzzy-match.el), the only candidate is with-syntax-table.

  2. With Icicles scatter fuzzy completion, you get all of the candidates that Swank fuzzy completion offers, plus the following:

    ange-ftp-wait-not-busy
    dired-into-dir-with-symlinks
    ediff-choose-window-setup-function-automatically
    ediff-make-wide-display
    ediff-toggle-wide-display
    ediff-window-display-p
    ediff-window-ok-for-display
    ediff-with-syntax-table
    find-new-buffer-file-coding-system
    gnus-frame-or-window-display-name
    help-window-display-message
    icicle-choose-window-for-buffer-display
    revert-buffer-with-coding-system
    set-w32-system-coding-system
    set-window-display-table
    set-window-redisplay-end-trigger
    shell-write-history-on-exit
    split-window-sensibly
    tree-widget--locate-sub-directory
    w32-find-file-not-found-set-buffer-file-coding-system
    w32-initialize-window-system
  1. With Icicles Levenshtein (1) fuzzy substring completion (Levenshtein distance of 1), you get only these completions:
    help-follow-symbol
    message-check-news-syntax
    w32-initialize-window-system window-system
    window-system-for-display
  1. With a Levenshtein distance of 2 instead of 1, there are more than 300 completion candidates.

  2. With strict Levenshtein (1 or 2) completion there are no candidates. Likewise, for Icicles Jaro-Winkler fuzzy completion.

  • 1
    I agree there is some potential API overlap for fuzzy completion, but code completion generally requires mode specific compiled or runtime inspection. As you state yourself, Icicles provides little in the way of code-completion features, so it doesn't seem relevant to the question at hand. – dgtized Oct 27 '14 at 1:32
  • @dgtized: Well, I disagree. Have you tried completion.el? (Likewise, dabbrev and hippie-expand - all 3 are included in GNU Emacs.) These things are quite helpful with code completion, even though, as you suggest, they have no code-specific knowledge. You might be surprised how helpful they can be (with or without fuzzy matching). And like these, any code-specific completion that uses the minibuffer to complete when there are multiple candidates automatically benefits from Icicles fuzzy completion. – Drew Oct 27 '14 at 1:40
2

if you use ivy,in the recent updates ivy supports completion at point functionality in an overlay. Ivy supports fuzzy matching with the help of flx. Checkout this video on how it works :)

All you got to do is, press the key binding for complete-symbol which is by default bound to C-M-i and an overlay will pop up and you use fuzzy matching to get the completion candidate.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.