2

I want to be able to use a hotkey to enable/disable anaconda linting. Its really inconvenient to have to open the settings whenever I had to use it. I'm new to Sublime Text but from what I see at the Keybindings, you can pass a variable with the args. For example:

[{"keys": ["ctrl+q"], "command": "toggle_comment", "args": {"block": false}}]

So, I was thinking, maybe there's a command to change package "settings - user" and pass a var to set ["anaconda_linting": false,] into true or false?

MattDMo
  • 100,794
  • 21
  • 241
  • 231
Aeiddius
  • 328
  • 1
  • 3
  • 12

1 Answers1

3

You can do this with a custom plugin and keybinding. Select Tools → Developer → New Plugin… and set the contents of the file that opens to this:

import sublime
import sublime_plugin


class ToggleAnacondaLintingCommand(sublime_plugin.ApplicationCommand):
    def run(self):
        s = sublime.load_settings("Anaconda.sublime-settings")
        current = s.get("anaconda_linting")
        new = not current
        s.set("anaconda_linting", new)
        sublime.save_settings("Anaconda.sublime-settings")
        sublime.active_window().run_command('save')

Hit CtrlS to save, and your Packages/User folder should open up. Save the file as toggle_anaconda_linting.py.

Now, open up your keybindings and add the following between the [ ] characters (choosing whatever shortcut you want):

{"keys": ["ctrl+alt+shift+l"], "command": "toggle_anaconda_linting"},

Now, whenever you hit the shortcut, "anaconda_linting" will be toggled for all files.

MattDMo
  • 100,794
  • 21
  • 241
  • 231
  • Thank you for the answer! I'm really grateful it work. Though two things to note. Initially it wasn't working so I removed the "edit" var after realizing it wasn't used. And second, the linting only refresh when you "save" the file. Do you know what function to add so you can hit save together with the plugin code? – Aeiddius Oct 10 '20 at 06:40
  • @Aeiddius see my edit above. Adding `self.view.run_command("save")` at the end should do the trick. The `edit` was left over from a previous version, I just forgot to remove it when I changed the parent class I subclassed from. – MattDMo Oct 10 '20 at 17:31
  • `self.view` isn't available in an `ApplicationCommand`; you need something like `sublime.active_window().run_command('save')` instead. – OdatNurd Oct 11 '20 at 06:32
  • Thank you MattDMo and OdatNurd for the answer and suggestion. It works perfectly now! :) :) – Aeiddius Oct 11 '20 at 06:37