1

When using spell checking in Sublime Text 3, it is possible to right click on a misspelled/underlined word and from the context menu, add it to the dictionary. Is there a way to do this using the keyboard without involving the mouse?

context menu spelling suggestions

The official documentation mentions the add_word command, but there is no default keybinding that uses it, and it requires that the word to add be specified as an argument - i.e. it doesn't add the word under the caret.

1

A simple answer would be to open the context menu with the keyboard (some keyboards have a dedicated button for it, for example between the Alt Gr and right Ctrl keys. Alternatively, you can press Shift+F10 on Windows. Then it is just a case of using the arrow keys to select the Add word menu item and pressing Enter.


This could obviously be done with less keypresses using a custom keybinding, which is where the add_word command mentioned in the documentation would come in handy.

However, as keybindings can't use the text under the cursor or the selection as an argument, a small Python plugin would need to be created for this. (There are exceptions where the command being executed supports it, like insert_snippet supporting a $SELECTION placeholder in the contents argument.)

Here is how to create a Python script that will add the words at the cursors to the dictionary:

  1. From the Tools menu -> Developer -> New Plugin
  2. Paste in the following:

    import sublime
    import sublime_plugin
    
    class AddWordsAtCursorsToDictionaryCommand(sublime_plugin.TextCommand):
        def run(self, edit):
            for sel in self.view.sel():
                region = None
                if sel.empty():
                    region = self.view.word(sel.begin())
                else:
                    region = sel
                word = self.view.substr(region)
                self.view.run_command('add_word', { 'word': word })
    
  3. Save it, in the default folder it suggests, as something like add_word.py - the file extension is very important, the base name itself isn't.

  4. Goto Preferences -> Key Bindings - User or Key Bindings depending on which version of ST3 you have.
  5. Enter the following:

    { "keys": ["f8"], "command": "add_words_at_cursors_to_dictionary" }
    
  6. If the file was previously empty, ensure the text above is surrounded by square brackets. i.e. [ and ]. Basically, it needs to be a valid JSON file, containing an array of keybindings.

  7. Save it
  8. Press F8 to add the word at the text caret(s) to the dictionary. (Obviously, you can change this to whatever key you desire in the relevant step.)

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.