Textmate style word completion

Chris Leonello 7 years ago • updated by Philip Rosen 5 years ago 21
Implement word completion as is done in Textmate.  Explained best at the Textmate web site (http://manual.macromates.com/en/working_with_text#completion):

"TextMate has a simple yet effective completion function on ⎋ (escape). It will complete the current word based on matches in the current document. If there are multiple matches, you can cycle through these by pressing ⎋ continuously. It is also possible to cycle backwards using ⇧⎋.

The matches are sorted by distance from the caret, meaning candidates which are closer to the caret will be suggested before candidates farther away."

This is an improvement over the current tab-completion because:

  1. The tab key is also used for tabbing.
  2. It cycles continuously.
  3. It is based on nearest matches.

You can use Ctrl+Space to complete using words from the current file
Thanks, didn't realize that at first.  However, this is not Textmate style completion:

  • Doesn't complete the word in place (like tab completion does), but shows a list of completions.
  • Completion include many items that aren't really completions (e.g. "tar" will show completions like "toward and start").
  • Requires a extra keystroke (e.g. return) to apply the completion.

Yes! The current behaviour of the insert_best_completion command is driving me crazy.

For example, if (in Ruby code) I have a variable named "column", I typically type "col" and hit the completion key. Sublime completes it with "collect { |e| }"!

This conflation of snippets and completion means that the completion function is completely unpredictable to the user, and I have to remember every snippet that exists so I don't accidently trigger one. TextMate gets it right — it has a one key (tab) for snippets, and another (escape) for completion. That's brilliant.

Now, I don't use snippets at all, as I don't find them productive, so being offered snippets all the time is simply an annoyance.

Being able to turn off snippets is step one.

The next step is being able to cycle completions. Right now, Sublime's autocompletion will iteratively autocomplete stuff. So given the source code:

thing = 1

thing_factory = ...


If I type "th" and press the autocomplete key, it will first expand to "thing". But I wanted "thunk_method", so I hit the autocomplete key again. But now it will expand into "thing_factory", which is not what I wanted.

This is something TextMate is much better at.

Ctrl+space works better (although it still offers snippets), but it's not an efficient UI. I have to visually scan the list offered, which incurs a mental context switch.

That link didn't seem to work for me. Here is the correct link:

Yeah, UserEcho inserted a space into the URL, sorry.
This is the one thing I miss about Textmate so far.
Just install my plugin: https://github.com/alexstaubo/sublime_text_alternative_autocompletion
Just to note - The plugin isn't working for the current dev version of Sublime Text 2.
Good point, I will be fixing that shortly.
Hangn out for that patch Alexander :-)
Sorry about the delay. All it took was a change to the key bindings. I have updated the documentation: https://github.com/alexstaubo/sublime_text_alternative_autocompletion

Found a problem with the current key bindings, updated the instructions in the readme.

Actually, I've changed my stance on this.  Remapping tab to pull up the ctrl+space style autocomplete dropdown is nice solution to this.  A single keystroke followed by enter is quite adequate, I think.
Alright, so that was a bad solution as well since it broke tab indent/unindent functionality.  Messing with tab and escape functionality is too finicky for me to bother with.  I'm just gonna remove all the tab autocomplete associations for now and use ctrl+space instead.  I like Sublime's matching algorithm and its dropdown menu over Textmate's approach, but loved having a single-keypress autocomplete shortcut.  ctrl-space isn't too much more movement, however, and it doesn't break any preexisting tab or escape functionality.

Alexander S.,

thanks for making the sublime_text_alternative_autocompletion plugin available.

However, I don’t understand where to put the code to customize the key mapping, the stuff like

"{ "keys": ["escape"], "command": ..."

Sorry for asking stupidly, being new to ST2, coming from TextMate.

Thanks for helping me out.

Did you ever figure out where to put the code?  I have the same question.  thanks!


actually I did. The code is to be put into the file

"Default (OSX).sublime-keymap"

(lives in ~/Library/Application Support/Sublime Text 2/Packages/User/)

You can access it directly from within ST2 via:

Preferences/Key Bindings - User

It then works immediately, like a charm.

Take care.

Cool!  Thanks so much, I'll give it a shot.