Sublime Forum

End of line key binding

#1

I would like to create a key binding that works as follows:

  • If in the middle of a line, enter will take you to the end of the line.
  • If at the end of a line, enter will take you to a new line.

Is this possible?

0 Likes

Pressing Up or Down after End has been pressed: auto-scroll to line end (EOL)
#2

The default binding for Enter will already put you on the next line no matter where the cursor is, so we can instead focus on a key binding that will detect when you’re in the middle of a line and jump you to the end of the line instead of doing that.

An example of that is:

{
    "keys": ["enter"],
    "command": "move_to", "args": {"to": "eol", "extend": false},
    "context": [
        {
            "key": "following_text",
            "operator": "not_regex_match",
            "operand": "$",
            "match_all": true
        }
    ]
}

This uses the command bound to the End by default (see Preferences > Key Bindings) with a context that says that this binding only applies when the text following the cursor position is not a match for the end of the line.

Note that we set match_all to true; that means that if you have multiple cursors in your file, this binding will only apply if every cursor is in the middle of a line.

The ramification of that is that if even one cursor is at the end of a line, this binding does not apply, so the default binding for enter will be used instead and pressing enter will make all of the cursors (even those inside of a line) insert a newline.

If you instead change the match_all to false, then the binding applies if at least one of the multiple cursors is in the middle of the line. In that case, as long as one cursor is in the middle of the line, the binding applies and all of the cursors will jump to the end of the line (with any already there visually doing nothing).

Also note that this does not apply to the enter key that is on the numeric keypad (if your keyboard) has one, only the standard enter key. If you want it to apply to that key as well, you need to duplicate the mapping and map it to keypad_enter as well.

2 Likes

Vintage mode, end of line (g$)
#3

ah thanks so much! I had the first part figured out but I didn’t know how to do the context part, so my code was overwriting the normal functionality of enter and it wouldn’t go to a new line.

1 Like

#4

I just tried it out and it doesn’t work exactly as I expected. Is it possible to add a new line if the cursor is at the beginning of a line also, instead of jumping to the end?

0 Likes

#5

Nevermind, I was trying to mimic some functionality another editor I use has, but there are too many components I can’t account for. Thanks for your help!

0 Likes