Home Download Buy Blog Forum Support

Move cursor to top/middle/bottom of visible lines.

Move cursor to top/middle/bottom of visible lines.

Postby planet on Thu Feb 23, 2012 4:15 pm

Hi,
I made a simple Plugin that moves the cursor to the top, middle or bottom of the screen (like VI H / M / L):

Update: Added two Functions for moving the cursor up/down by x lines (see key bindings).

Code: Select all
import sublime, sublime_plugin

class Move_caret_topCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        screenful = self.view.visible_region()

        col = self.view.rowcol(self.view.sel()[0].begin())[1]
        row = self.view.rowcol(screenful.a)[0] + 1
        target = self.view.text_point(row, col)

        self.view.sel().clear()
        self.view.sel().add(sublime.Region(target))

class Move_caret_middleCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        screenful = self.view.visible_region()

        col = self.view.rowcol(self.view.sel()[0].begin())[1]
        row_a = self.view.rowcol(screenful.a)[0]
        row_b = self.view.rowcol(screenful.b)[0]

        middle_row = (row_a + row_b) / 2
        target = self.view.text_point(middle_row, col)

        self.view.sel().clear()
        self.view.sel().add(sublime.Region(target))

class Move_caret_bottomCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        screenful = self.view.visible_region()

        col = self.view.rowcol(self.view.sel()[0].begin())[1]
        row = self.view.rowcol(screenful.b)[0] - 1
        target = self.view.text_point(row, col)

        self.view.sel().clear()
        self.view.sel().add(sublime.Region(target))

class Move_caret_forwardCommand(sublime_plugin.TextCommand):
    def run(self, edit, nlines):
        screenful = self.view.visible_region()

        (row,col) = self.view.rowcol(self.view.sel()[0].begin())
        target = self.view.text_point(row+nlines, col)

        self.view.sel().clear()
        self.view.sel().add(sublime.Region(target))
        self.view.show(target)

class Move_caret_backCommand(sublime_plugin.TextCommand):
    def run(self, edit, nlines):
        screenful = self.view.visible_region()

        (row,col) = self.view.rowcol(self.view.sel()[0].begin())
        target = self.view.text_point(row-nlines, col)

        self.view.sel().clear()
        self.view.sel().add(sublime.Region(target))
        self.view.show(target)


My Windows key bindings:

Code: Select all
{ "keys": ["alt+up"], "command": "move_caret_top" },
{ "keys": ["alt+left"], "command": "move_caret_back", "args": { "nlines": 10}  },
{ "keys": ["alt+right"], "command": "move_caret_forward", "args": { "nlines": 10} },
{ "keys": ["alt+down"], "command": "move_caret_bottom"},


Note: The code is just a stripped down version of the commands found in the Vintage package (vintage_motions.py). To install, just save the code as a new Plugin (Tools->New Plugin...).

Regards, Thorsten.
planet
 
Posts: 24
Joined: Fri Feb 17, 2012 12:30 pm

Re: Move cursor to top/middle/bottom of visible lines.

Postby kernelp4nic on Wed Mar 07, 2012 4:43 pm

Works fantastic! Thanks!
kernelp4nic
 
Posts: 1
Joined: Wed Mar 07, 2012 4:41 pm

Re: Move cursor to top/middle/bottom of visible lines.

Postby pedrober on Fri Sep 07, 2012 7:31 am

In my computer, your veru useful plugin moves the cursor not to the top of the screen but to the second line. It's a minor inconvenience but...
I have a big display (48 lines). Any suggestion?
pedrober
 
Posts: 40
Joined: Thu Jun 28, 2012 6:21 pm

Re: Move cursor to top/middle/bottom of visible lines.

Postby planet on Mon Sep 10, 2012 8:19 am

pedrober wrote:In my computer, your veru useful plugin moves the cursor not to the top of the screen but to the second line. It's a minor inconvenience but...
I have a big display (48 lines). Any suggestion?


Hi! You can remove "+ 1" in "Move_caret_topCommand" and "- 1" in "Move_caret_bottomCommand". However, sometimes the cursor will be slightly out of view, if the first/last line is not completely visible.

Thorsten.
planet
 
Posts: 24
Joined: Fri Feb 17, 2012 12:30 pm

Re: Move cursor to top/middle/bottom of visible lines.

Postby pedrober on Thu Nov 29, 2012 3:00 pm

Perfect solution. Thank you very much.
pedrober
 
Posts: 40
Joined: Thu Jun 28, 2012 6:21 pm

Re: Move cursor to top/middle/bottom of visible lines.

Postby jonemi on Fri Oct 25, 2013 6:37 pm

How do you set shift+alt+left to select the text of the lines you move over?
jonemi
 
Posts: 1
Joined: Fri Oct 25, 2013 6:36 pm


Return to Plugin Announcements

Who is online

Users browsing this forum: No registered users and 13 guests