Search code examples
sublimetextsublimetext3sublime-text-plugin

Comment out code by highlighting and simultaneously right clicking in Sublime Text


Rather than taking my hand of the mouse to use Ctrl + Shift + /, I'd like to be able to comment code by highlighting it, and then while keeping the left mouse button held down, simultaneously right click and have it comment out - though I don't know if that's possible with Sublime Text keybinds.

If not, I'll just use a custom keyboard shortcut, but I'd prefer the mouse method.


Solution

  • You can customize mousebinds by creating a file named Default (Windows).sublime-mousemap, Default (OSX).sublime-mousemap, Default (Linux).sublime-mousemap or (for any OS) Default.sublime-mousemap in your settings folder \Packages\User and then creating your custom bind inside that file
    You can read more here (it's actually about keybinds but the process of creating mousebinds is the same.)

    I came up with something similar to what you are trying to achieve but could not bind mouse1 as modifier so the snippet below is working different. After you've made a selection, click and hold right mouse button and then click left mouse button to toggle comment block on selected area.

    [
      {
         "button": "button1", "count": 1, "modifiers": ["button2"],
         "command": "toggle_comment", "args": {"block": true},
         "press_command": "drag_select_callback"
      }
    ]
    

    This might be annoying if you comment something by accident, so you can change click to double click, by changing count value to 2 or 3 for triple click etc.

    Block comment is defined by {"block": true}, if you would like line comments just change it to false

    [
      {
         "button": "button1", "count": 1, "modifiers": ["button2"],
         "command": "toggle_comment", "args": {"block": false},
         "press_command": "drag_select_callback"
      }
    ]