127

I want to comment out a block of code in sublime text.

I see it in RailsCasts, but don't think he uses sublime text ... to do the following ...

if (uncommented)
  some uncommented example
  # if (commented) 
  #   some commented example code
  # end
end

Is there a shortcut in sublime text that I can use to insert the block of #'s?

Seanny123
  • 8,776
  • 13
  • 68
  • 124
user749798
  • 5,210
  • 10
  • 51
  • 85

10 Answers10

270

The shortcut to comment out or uncomment the selected text or current line:

  • Windows: Ctrl+/
  • Mac: Command ⌘+/
  • Linux: Ctrl+Shift+/

Alternatively, use the menu: Edit > Comment

For the block comment you may want to use:

  • Windows: Ctrl+Shift+/
  • Mac: Command ⌘+Option/Alt+/
naimdjon
  • 3,162
  • 1
  • 20
  • 41
Randall Ma
  • 10,486
  • 9
  • 37
  • 45
  • 1
    That same shortcut also works in Textmate which is used by Ryan Bates. – Holger Just Jul 17 '12 at 12:44
  • 1
    In Notepad++ and Geany (for example, amongst others), you have one command to _comment_ AND another, different one to _un_ -comment. That's very useful if you want to select a whole block where some lines are commented and some are not, and put everything under comments, or everything without comments. If you have only toggle (which is the case with Sublime), the commenting will be inverted, which is annoying. – heltonbiker Jun 30 '14 at 19:32
  • 34
    If you are using a German keyboard layout and have the problem of the shortcuts not working, you have to go to "Preferences / Key Bindings - User" and add these two lines `{ "keys": ["ctrl+7"], "command": "toggle_comment", "args": { "block": false } }, { "keys": ["ctrl+shift+7"], "command": "toggle_comment", "args": { "block": true } }` – Robert Oct 25 '15 at 17:32
  • 3
    Thank you @Robert, I was having the same problem with my Spanish keyboard and that solved it. – Santiago Corredoira Aug 25 '16 at 08:49
  • Doesn't work for me. In Edit > Comment ...I click on the buttons and nothing happens... very common in Mac. Mac works terrible with keyboard shortcuts, it's designed to do everything by mouse but that's too slow. – Dr Potato May 08 '21 at 00:50
  • @DrPotato if you are using Mac (and a German keyboard layout) you can add the following key bindings: `{ "keys": ["super+shift+7"], "command": "toggle_comment", "args": { "block": false } }, { "keys": ["super+alt+shift+7"], "command": "toggle_comment", "args": { "block": true } }` – OSX55 Jul 29 '21 at 05:05
  • @Robert thank you. It just solved my problem with German keyboard. You are my hero of the day. – Alex May 12 '23 at 08:30
25

You're looking for the toggle_comment command. (Edit > Comment > Toggle Comment)

By default, this command is mapped to:

  • Ctrl+/ (On Windows and Linux)
  • Command ⌘+/ (On Mac)

This command also takes a block argument, which allows you to use block comments instead of single lines (e.g. /* ... */ as opposed to // ... in JavaScript). By default, the following key combinations are mapped to toggle block comments:

  • Ctrl+Shift+/ (On Windows and Linux)
  • Command ⌘+Alt+/ (On Mac)
Ajedi32
  • 45,670
  • 22
  • 127
  • 172
20

With a non-US keyboard layout the default shortcut Ctrl+/ (Win/Linux) does not work.

I managed to change it into Ctrl+1 as per Robert's comment by writing

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

to Preferences -> Key Bindings (on the right half, the user keymap).

Note that there should be only one set of brackets ('[]') at the right side; if you had there something already, copy paste this between the brackets and keep only the outermost brackets.

01F0
  • 1,228
  • 2
  • 19
  • 32
Niko Föhr
  • 28,336
  • 10
  • 93
  • 96
  • 1
    Thank you. This answerd worked with me on Windows 10. – smirandac1978 Dec 21 '19 at 03:46
  • Thank a lot! To add on, I used this method, and as I wanted the same behaviour that my VSCode has I used following keymap: [{ "keys": ["ctrl+7"], "command":"toggle_comment", "args": { "block": true } }] – José Ripoll Apr 06 '20 at 21:57
9

Ctrl-/ will insert // style commenting, for javascript, etc
Ctrl-/ will insert <!-- --> comments for HTML,
Ctrl-/ will insert # comments for Ruby,
..etc

But does not work perfectly on HTML <script> tags.

HTML <script> ..blah.. </script> tags:
Ctrl-/ twice (ie Ctrl-/Ctrl-/) will effectively comment out the line:

  • The first Ctrl-/ adds // to the beginning of the line,
    which comments out the script tag, but adds "//" text to your webpage.
  • The second Ctrl-/ then surrounds that in <!-- --> style comments, which accomplishes the task.

Ctrl--Shift-/ does not produce multi-line comments on HTML (or even single line comments), but does
add /* */ style multi-line comments in Javascript, text, and other file formats.

--

[I added as a new answer since I could not add comments.
I included this info because this is the info I was looking for, and this is the only related StackOverflow page from my search results.
I since discovered the / / trick for HTML script tags and decided to share this additional information, since it requires a slight variation of the usual catch-all (and reported above)
/ and Ctrl--Shift-/ method of commenting out one's code in sublime.]

NIMISHAN
  • 1,265
  • 4
  • 20
  • 29
SherylHohman
  • 16,580
  • 17
  • 88
  • 94
6

You can toggle the block comment with

Ctrl+Shift+/

Source: http://www.sublimetext.com/forum/viewtopic.php?f=3&t=2967

NIMISHAN
  • 1,265
  • 4
  • 20
  • 29
DHinch
  • 109
  • 1
  • 5
5

Just in case someone is using the Portuguese ABNT keyboard layout The shortcut is

Ctrl + ;
nassim
  • 388
  • 3
  • 9
4

Just an important note. If you have HTML comment and your uncomment doesn't work
(Maybe it's a PHP file), so don't mark all the comment but just put your cursor at the end or at the beginning of the comment (before ) and try again (Ctrl+/).

NIMISHAN
  • 1,265
  • 4
  • 20
  • 29
Eran Or
  • 1,252
  • 15
  • 22
2

In mac I did this

  • type your comment and press command + D to select the text
  • and then press Alt + Command + / to comment out the selected text.
TT.
  • 15,774
  • 6
  • 47
  • 88
TrickyJ
  • 61
  • 2
0

with my keyboard (logitech) here in Italy I can:

  • select the portion of text with the mouse and then with [ctrl + ù] comment or uncomment.

  • select the line and use the same command.

  • with [ctrl + shift] and using the left and right cursors I select the text. then using the up and down keyboard cursors I move the selected text up or down the page.

(SublimeText)

Greetings

TigerMat90

0

If you don't have a US keyboard, the conventional Ctrl + / can be used to comment your code in Sublime Text just by changing the Sublime Key map Configuration (the right side file) and changing the character '/' for 'keypad_divide' which is the identifier for the '/' symbol in non US keyboards, as shown in the Sublime Text Official documentation

Writing the following on Preferences > Key Bindings right file should work.

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