159

In Sublime Text 2, how do I enclose a selection in a comment?
Is there a keyboard shortcut for this action?

Yashu Mittal
  • 1,478
  • 3
  • 14
  • 32
user1419762
  • 3,571
  • 5
  • 20
  • 10
  • 8
    Don't forget to use the `` tag in your answers, guys. It adds the proper emphasis to the keyboard shortcuts you're sharing. See my answer for an example: http://stackoverflow.com/a/19389142/901641 - hit `edit` on my answer to see exactly how I used the tag. – ArtOfWarfare Oct 15 '13 at 19:10

15 Answers15

288

By default on Linux/Windows for an English keyboard the shortcut is Ctrl+Shift+/ to toggle a block comment, and Ctrl+/ to toggle a line comment.

If you go into Preferences->Key Bindings - Default, you can find all the shortcuts, below are the lines for commenting.

{ "keys": ["ctrl+/"], "command": "toggle_comment", "args": { "block": false } },
{ "keys": ["ctrl+shift+/"], "command": "toggle_comment", "args": { "block": true } },
Andrew Barrett
  • 19,721
  • 4
  • 47
  • 52
  • 8
    The default doesn't work for me on Windows. I'm using a laptop, that might be why, i dunno. This worked though, thanks. – qwerty Nov 23 '12 at 14:31
  • 14
    As Vadim notes, on Mac the commands are `cmd+option+/` and `cmd+/`, respectively. – dbn Dec 10 '12 at 00:54
  • 1
    So what about super, instead of command? I see the Linux syntax in my default key bindings, and I've successfully used it in my user key bindings. – Ace Feb 15 '13 at 17:33
  • 2
    If you're a mac user with spanish keyboard you would use this in your user custom key bindings: [ { "keys": ["alt+minus"], "command": "toggle_comment", "args": { "block": false } }, { "keys": ["alt+shift+minus"], "command": "toggle_comment", "args": { "block": true } } ] – Pablo Ezequiel Leone Feb 03 '14 at 10:28
  • So what is "Ctrl+Shift+/" supposed to mean? Don't i need "Shift+7" already to achieve "/"? How would "Ctrl+Shift+/" and "Ctrl+/" be any different from each other then? It's things like that, why shortcuts often don't work. For me, on a Windows 7 machine, answers suggesting "Ctrl+7" are correct. – Janos Jul 26 '14 at 10:35
  • 1
    @noshky - yes, my answer seems to be only valid for people using GB or US keyboards, I've updated it to be explicit about that. (The forward slash key "/" is it's own key on a GB/US keyboard) – Andrew Barrett Jul 28 '14 at 07:21
  • @AndrewBarrett That command doesn't seem to work for Cypher (CQL), but it has always worked for my other files. How can I change my settings such that Cypher knows to comment out lines as well with the same command? – NumenorForLife Jun 15 '15 at 17:28
89

In the "Preferences->Key Bindings - User"

[
   { "keys": ["ctrl+7"], "command": "toggle_comment", "args": { "block": false } },
   { "keys": ["ctrl+shift+7"], "command": "toggle_comment", "args": { "block": true } }
]

Just paste it, these are will work great !

Evren
  • 891
  • 6
  • 4
20

I'd like to add, that on my mac by default block comment toggle shortcut is cmd+alt+/

ArtOfWarfare
  • 20,617
  • 19
  • 137
  • 193
VadimRostok
  • 201
  • 2
  • 2
14

For German keyboards use ctrl+shift+# to toggle a block comment and ctrl+# to toggle a line comment.

The shortcut in Preferences->Key Bindings - Default is set to Ctrl+Shift+/ and Ctrl+/, but to actually use the functions, press the keys stated above.

L Y E S - C H I O U K H
  • 4,765
  • 8
  • 40
  • 57
happymoep
  • 141
  • 1
  • 8
  • Don't suppose you could advise me on how to assign to the hash key? I'm trying to make a shortcut for the 'reindent' command but it's having none of it. Trying: `{ "keys": ["ctrl+#"], "command": "reindent" }` Is there an issue using the **#** character in JSON? – Graham R. Armstrong Apr 10 '15 at 15:42
  • 1
    @GrahamR.Armstrong As the shortcut in that file is assigned to `Ctrl+/` and one needs to press `ctrl + #` to use the function, I would suggest you try to use `/` to assign that key. I don't know what that JSON parser does with the # character. – happymoep Apr 26 '15 at 22:09
10

In a Brazilian Portuguese ABNT2 keyboard I have a similar issue to the one reported by JoshDM. In the file sublime-keymap I have:

{ "keys": ["ctrl+/"], "command": "toggle_comment", "args": { "block": false } },
{ "keys": ["ctrl+shift+/"], "command": "toggle_comment", "args": { "block": true } },

But I have to use ctrl+; and ctrl+shift+;. On my keyboard, ; is on the left of /.

It seems like a bug.

ArtOfWarfare
  • 20,617
  • 19
  • 137
  • 193
marcelo.guedes
  • 482
  • 5
  • 12
9

you need to replace "/" with "7", it works on non english keyboard layout.

ganchan
  • 317
  • 4
  • 4
8

This did the trick for me coming from Brackets and being used to ctrl+/ on the numpad.

[
    { "keys": ["ctrl+keypad_divide"], "command": "toggle_comment", "args": { "block": false } },
    { "keys": ["ctrl+shift+keypad_divide"], "command": "toggle_comment", "args": { "block": true } }
]
Xavier Guihot
  • 54,987
  • 21
  • 291
  • 190
Gísli Freyr Svavarsson
  • 2,275
  • 2
  • 18
  • 13
6

In my keyboard (Swedish) it´s the key to the right of "ä": "*".

ctrl+*

ArtOfWarfare
  • 20,617
  • 19
  • 137
  • 193
daniel_aren
  • 1,714
  • 3
  • 24
  • 41
5

In keyboard (Spanish), SO: Win7.

Go into Preferences->Key Bindings - Default, replace..."ctrl+/"]... by "ctrl+7"...

And don't use the numpad, it doesn't work. Just use the numbers above the letters

BrennQuin
  • 656
  • 10
  • 19
2

On a Mac with a US keyboard, you want cmd+/.

ArtOfWarfare
  • 20,617
  • 19
  • 137
  • 193
2

Seems like some kind of keyboard mapping bug. I'm Portuguese, so I'm using a PT/PT keyboard. Sublime Text 3 apparently is handling / as ~.

ArtOfWarfare
  • 20,617
  • 19
  • 137
  • 193
0

Max OS: If you want to toggle comment multiple individual lines versus block comment an entire selection, you can do multi line edit, shift+cmd+L, then cmd+/ in that sequence.

0

Ctrl+d and Ctrl+Shift+d....

[

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

{ "keys": ["ctrl+shift+d"], "command": "toggle_comment", "args": { "block": true } },

]
Xavier Guihot
  • 54,987
  • 21
  • 291
  • 190
Shahnawaz
  • 105
  • 1
  • 12
0

First Open The Sublime Text 2.

And top menu bar on select the Preferences.

And than select the Key Bindings -User.

And than put this code,

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

    { "keys": ["ctrl+shift+c"], "command": "toggle_comment", "args": { "block": true } }
]

I use Ctrl+Shift+C, You also different short cut key use.

Mayur Vora
  • 922
  • 2
  • 14
  • 25
0

On my laptop with spanish keyboard, the problem seems to be the "/" on the key binding, I changed it to ctrl+shift+c and now it works.

{ "keys": ["ctrl+shift+c"], "command": "toggle_comment", "args": { "block": true } },
eyllanesc
  • 235,170
  • 19
  • 170
  • 241