Sublime Forum

How to autocomplete inside a word?

#1

When the cursor/caret is behind a space the autocomplete show/pop up automatically.

On this example:

  1. I type m the the auto complete show up and I press enter to accept.
  2. Later, I walk back until its first latter m and delete it.
  3. Then I type m again, however the autocomplete does not show up automatically.
  4. After repeating the steps 2 and 3 some times, I finally force to call autocomplete by Ctrl+Space
  5. Finally the word is completed.

However would be nice if the word was not doubled. I mean, if I type m on the beginning of a word, I would like to the autocomplete show up, and if the rest of the word is the same as the character input by the auto complete as main only, and not doubled as in mainain.

This seems to be a similar question:

  1. #10281 Autocomplete trigger: require space

Can a package force the autocompletion when the cursor/caret is inside a word and when the autocompletion is performed, just complete the missing character instead of double the repeated ones?

Initially I could think of hook on_selection_modified, however it would show up the autocompletion every time I change the cursor.

0 Likes

#2
2 Likes

#3

Thanks, this solves the autocompletion problem double word.

Now I only miss to the autocompletion to up automatically when I am inside a word and start typing. Would this be a feature request on the Sublime Text Core/issues?

0 Likes

#4

I managed to create this plugin:

import sublime
import sublime_plugin


class OpenAutoCompletionCommand(sublime_plugin.TextCommand):

    def run(self, edit, **kargs):
        # print( "kargs: ", str( kargs ) )

        view = self.view
        view.run_command("insert", {"characters": kargs["keystroke"]})

        window = view.window()
        window.run_command("auto_complete")

Which needs this keymap file Default.sublime-keymap:

[
    { "keys": ["a"], "command": "open_auto_completion", "args": {"keystroke": "a" } },
    { "keys": ["b"], "command": "open_auto_completion", "args": {"keystroke": "b" } },
    { "keys": ["c"], "command": "open_auto_completion", "args": {"keystroke": "c" } },
    { "keys": ["d"], "command": "open_auto_completion", "args": {"keystroke": "d" } },
    { "keys": ["e"], "command": "open_auto_completion", "args": {"keystroke": "e" } },
    { "keys": ["f"], "command": "open_auto_completion", "args": {"keystroke": "f" } },
    { "keys": ["g"], "command": "open_auto_completion", "args": {"keystroke": "g" } },
    { "keys": ["h"], "command": "open_auto_completion", "args": {"keystroke": "h" } },
    { "keys": ["i"], "command": "open_auto_completion", "args": {"keystroke": "i" } },
    { "keys": ["j"], "command": "open_auto_completion", "args": {"keystroke": "j" } },
    { "keys": ["k"], "command": "open_auto_completion", "args": {"keystroke": "k" } },
    { "keys": ["l"], "command": "open_auto_completion", "args": {"keystroke": "l" } },
    { "keys": ["m"], "command": "open_auto_completion", "args": {"keystroke": "m" } },
    { "keys": ["n"], "command": "open_auto_completion", "args": {"keystroke": "n" } },
    { "keys": ["o"], "command": "open_auto_completion", "args": {"keystroke": "o" } },
    { "keys": ["p"], "command": "open_auto_completion", "args": {"keystroke": "p" } },
    { "keys": ["q"], "command": "open_auto_completion", "args": {"keystroke": "q" } },
    { "keys": ["r"], "command": "open_auto_completion", "args": {"keystroke": "r" } },
    { "keys": ["s"], "command": "open_auto_completion", "args": {"keystroke": "s" } },
    { "keys": ["t"], "command": "open_auto_completion", "args": {"keystroke": "t" } },
    { "keys": ["u"], "command": "open_auto_completion", "args": {"keystroke": "u" } },
    { "keys": ["v"], "command": "open_auto_completion", "args": {"keystroke": "v" } },
    { "keys": ["w"], "command": "open_auto_completion", "args": {"keystroke": "w" } },
    { "keys": ["x"], "command": "open_auto_completion", "args": {"keystroke": "x" } },
    { "keys": ["y"], "command": "open_auto_completion", "args": {"keystroke": "y" } },
    { "keys": ["z"], "command": "open_auto_completion", "args": {"keystroke": "z" } },
]

However, Sublime text is inserting the completion automatically when there is only one option. How to turn this off?

Related threads:

  1. #13532 How to get typed key name?
  2. #1697 [BUG] Autocompletion doesn’t open the popup
  3. #25465 Tab trigger select first one from auto complete popup instead of triggering Emmet completion
0 Likes

#5

TL;DR: although there’s no preference to control this, you can just pass disable_auto_insert with a value of true to the auto_complete command - I override the default Ctrl+Space binding to do this.

1 Like

#6

Thanks! This is the new code:

import sublime
import sublime_plugin


class OpenAutoCompletionCommand(sublime_plugin.TextCommand):

    def run(self, edit, **kargs):
        # print( "kargs: ", str( kargs ) )

        view = self.view
        view.run_command("insert", {"characters": kargs["keystroke"]})

        window = view.window()
        window.run_command("auto_complete", {'disable_auto_insert': True, 'next_completion_if_showing': False})
0 Likes