Shortcut to comment out a block of code with sublime text
Keyboard ShortcutsSublimetextKeyboard Shortcuts Problem Overview
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?
Keyboard Shortcuts Solutions
Solution 1 - Keyboard Shortcuts
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 - Keyboard Shortcuts
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 - Keyboard Shortcuts
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 - Keyboard Shortcuts
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 - Keyboard Shortcuts
You can toggle the block comment with
Ctrl+Shift+/
Source: http://www.sublimetext.com/forum/viewtopic.php?f=3&t=2967
Solution 6 - Keyboard Shortcuts
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+/).
Solution 7 - Keyboard Shortcuts
Just in case someone is using the Portuguese ABNT keyboard layout The shortcut is
Ctrl + ;
Solution 8 - Keyboard Shortcuts
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.
Solution 9 - Keyboard Shortcuts
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