Sublime Text Comment and Next Line Shortcut

Dec 10 2015 Published by under Technology

Just a quick tip. I’ve been using Android Studio extensively, daily, for the last year and a half. One minor editing feature I’ve grown to love is the way it handles commenting lines. You have your keyboard shortcut, Ctrl-/ or Cmd-/. This toggles the comment on the current line and then moves to the next line. So if you want to comment, or uncomment a bunch of lines, you can just hit that shortcut a few times and it will move through the lines and do its thing.

When I started doing a lot more personal JS work and dove back into Sublime Text, I really missed this feature. So I figured out how to re-create it.

First, you have to make a macro. Just put your cursor at the start of a line, and choose Tools -> Record Macro. Then hit your comment shortcut. Then hit down arrow. Then go to Tools -> Stop Recording Macro and then Tools -> Save Macro.

Now you just need to hook that up to your comment toggle keyboard shortcut. Go to Preferences -> Key Bindings – User and add this line:

{ "keys": ["ctrl+/"], "command": "run_macro_file", "args": {"file": "Packages/User/comment_next_line.sublime-macro"} }

Obviously, “ctrl” is for Windows / Linux. You’ll probably want “cmd” “command” for Mac. And the “file” argument is the path to where you saved the macro you just recorded.

5 responses so far

  • Dennis L says:

    Nice tip, Keith.

    I needed to spell out “command” for Mac.

    { “keys”: [“command+/”], “command”: “run_macro_file”, “args”: {“file”: “Packages/User/comment_next_line.sublime-macro”} }

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

  • Artem says:

    Thx for the suggesting!
    I get it work with { “keys”: [“super+/”], “command”: “run_macro_file”, “args”: {“file”: “res://Packages/User/comment_next_line.sublime-macro”} },

  • Joel Hjerten says:

    On Windows I have to use “[“ctrl+keypad_divide”]” as “[ctrl+/]” does’t work.