Shortcut to comment out a block of code with sublime text
Solution 1
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+/
Solution 2
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)
Solution 3
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.
Solution 4
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.]
Solution 5
You can toggle the block comment with
Ctrl+Shift+/
Source: http://www.sublimetext.com/forum/viewtopic.php?f=3&t=2967
user749798
Updated on August 21, 2020Comments
-
user749798 almost 4 years
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?
-
Holger Just almost 12 yearsThat same shortcut also works in Textmate which is used by Ryan Bates.
-
heltonbiker about 10 yearsIn 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.
-
Noah over 9 yearsIn Sublime Text 3, the block comment style shortcut does not work.
-
Robert over 8 yearsIf 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 } }
-
Santiago Corredoira almost 8 yearsThank you @Robert, I was having the same problem with my Spanish keyboard and that solved it.
-
smirandac1978 over 4 yearsThank you. This answerd worked with me on Windows 10.
-
José Ripoll about 4 yearsThank 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 } }]
-
dvlper about 4 yearsThat's how an answers should be. Simple!
-
Dr Potato about 3 yearsDoesn'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.
-
OSX55 almost 3 years@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 } }