Sublime Forum

Move up or down by N lines

#1

I am wondering if there is a way I can have a keyboard shortcut behave like the default up/down key behaviour, but instead of moving the cursor by one like at a time it would move by N line at a time. Most realistically just 2 lines at a time.

Is this possible, or is there a plugin that exists that implements this behaviour?

Thanks

0 Likes

#2

This plugin (forum post here) implements a drop-in replacement for the move command named move_amount that takes an optional extra argument amount to specify the number of times to take the motion (if not provided, the default is 1, which mimics what the move command would do).

Using that you can create bindings similar to up and down that move by multiple lines.

0 Likes

#3

Somewhat related, I’ve assigned my pageup/pagedown keys to a half-pager plugin:

import sublime
import sublime_plugin

class PartialPagerCommand(sublime_plugin.TextCommand):
    def run(self, edit, amount):

        view = self.view
        rowcnt = float(view.rowcol(view.visible_region().end())[0] -
                       view.rowcol(view.visible_region().begin())[0])
        movecnt = round(rowcnt * amount)

        rowcol = view.rowcol(view.sel()[0].begin())
        line = rowcol[0] + movecnt
        col = rowcol[1]

        pt = view.text_point(line, col)
        view.sel().clear()
        view.sel().add(sublime.Region(pt))
        view.show_at_center(pt)

This results in a smooth scroll of half a visible page which makes it easier (for me) to track the jumps visually.

3 Likes

A better paging behaviour
#4

The plugin listed in the second post is what I am using now. It works nicely.

Is there a way I could also have it scroll the page by the same N lines that it is moving the caret?

0 Likes

#5

Probably the least complicated thing would be to use a macro that uses scroll_lines to scroll the view by a given number of lines followed by using the plugin above to move the cursor as well.

Alternatively, a simpler plugin that is dedicated to only scrolling the buffer could be used to do both of them in one shot.

0 Likes

#6

As an extension of the move_amount behaviour would it be possible to have a key cord to the effect of

{"keys": ["alt+m","#"],  "command": "move_amount", "args":{"amount":"#"}}

where # is an any positive integer. Obviously you could write out separate key bindings for #=1, #=2, #=3,… but this would result in a huge key binding file if you wanted to go up to several hundred lines say.

0 Likes

#7

There’s not a way for a command to know what key binding caused it to execute (if any; commands can also trigger via menu actions, the command palette, or just other plugins running the command) so something like this is not possible.

It’s possible to set up a command so that when you run it from the command palette it can prompt you for the value of it’s arguments, so a potential workaround would be to have have key bindings with the more common numbers that you use most often and then have the command in the command palette interactively ask you for uncommon cases.

0 Likes

#8

That’s good to know - I was mainly curious as to whether it was possible to use the later part of a key ‘chord’ as the argument to a command directly.

Yes perhaps the first 10 numbers; thereafter using the in-built goto overlay is probably a far more sensible way to navigate a document anyway!

0 Likes