Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make Sublime Text 3 to move the cursor to the next line after ctrl + / comment?

Tags:

sublimetext3

I want to comment out pieces of code with one-line comments without using the arrows, like I do in IDEA, but Sublime Text 3 stays on the same line after commenting. How can I change this behavior?

like image 710
Dimitrius Avatar asked Jan 24 '17 09:01

Dimitrius


People also ask

How do I move to the next line in Sublime Text?

To insert a new line below the current line in your code, hit Cmd–Return (Mac) or Ctrl–Enter(Windows). You can also insert a new line above the current line by using Cmd–Shift–Return(Mac) or Ctrl–Shift–Enter (Windows).

How do I comment multiple lines in Sublime Text 3?

To add single-line comments, put the text cursor on the line to comment-out or highlight multiple lines and type CMD + / on Mac, for Linux and Windows use CTRL + / .

How do I move multiple lines in Sublime Text?

You can use Ctrl and Alt with this too. Shift+Right mouse button is an alternative way to initial a column select. Dragging in the gutter (where the line numbers are), will select entire lines at once.


1 Answers

The simplest solution is to use a Macro for this that combines together the commands for toggling a line comment and then moving the cursor, and then rebinding the key to run the macro.

Such a macro would look something like the following. Here this is saved as Packages\User\comment_line.sublime-macro.

[
    {
        "command": "toggle_comment",
        "args": {"block": false }
    },
    {
        "command": "move",
        "args": {"by": "lines", "forward": true }
    }
]

With this in place, you can add a binding such as the following to your custom key bindings:

{
    "keys": ["ctrl+/"],
    "command": "run_macro_file",
    "args": {"file": "res://Packages/User/comment_line.sublime-macro"},
    "context": [
        { "key": "selection_empty", "operator": "equal", "operand": "true", "match_all": true },
    ]
},

If you change the name of the macro when you save it, that needs to be reflected here.

This binding includes a context that makes it only apply when there is no selection, in which case this binding will be ignored and Sublime will use the default instead.

That can be removed if you wish. However, WebStorm (the only JetBrains tool I have handy at the moment) operates in this fashion, so presuming that IntelliJ does as well this more accurately mimics what happens there.

Additionally, if you're mapping a different key to the comment command, make sure the original line is above the new addition:

{ "keys": ["ctrl+q"], "command": "toggle_comment", "args": { "block": false } },
{
    "keys": ["ctrl+q"],
    "command": "run_macro_file",
    "args": {"file": "res://Packages/User/comment_line.sublime-macro"},
    "context": [
        { "key": "selection_empty", "operator": "equal", "operand": "true", "match_all": true },
    ]
},
like image 164
OdatNurd Avatar answered Jan 04 '23 15:01

OdatNurd