Sublime Forum

UP arrow key to move cursor at the beginning of first line?

#1

It is there a way to make SublimeText to go at the beginning of the paragraph after reaching the top line of the document when I do press and hold the UP arrow key. The same for DOWN arrow, it is not going to the end of last paragraph when holding the DOWN arrow key.

This is the default behavior in a lot of text editors including web browsers, even when I edit this post I can have this behavior, but unfortunately SublimeText it is not behaving the same and becomes frustrating at times.

0 Likes

#2

You can achieve this by creating a plugin with a small listener to check whether any or all carets are on first/last line and then conditionally remap up/down to home/end.

plugin template code:

import sublime
import sublime_plugin

class LineContextListener(sublime_plugin.EventListener):
    def on_query_context(
        self,
        view: sublime.View,
        key: str,
        operator: sublime.QueryOperator,
        operand: int,
        match_all: bool = True,
    ) -> bool:
        if key == "preceding_lines":
            ...
        elif key == "following_lines":
            ...
        return False

Key bindings:

	{
		"keys": ["up"],
		"command": "move_to",
		"args": {"to": "bol", "extend": false},
		"context": [{"key": "preceding_lines", "operator": "equal", "operand": 0}],
	},
	{
		"keys": ["down"],
		"command": "move_to",
		"args": {"to": "eol", "extend": false},
		"context": [{"key": "following_lines", "operator": "equal", "operand": 0}],
	},
0 Likes

#3

ST actually has a setting for this. It’s enabled by default on Mac, but should work when set to true on any platform

"move_to_limit_on_up_down": true,
4 Likes

#4

Wonderful. Thank you for the tip.

0 Likes