Sublime Forum

Moving multiple lines

#1

This is a plugin @OdatNurd wrote, but I can’t get the keybinding to work, what am I doing wrong please ?

import sublime_plugin

class MoveAmountCommand(sublime_plugin.TextCommand):
    def run(self, edit, amount=1, **kwargs):
      for _ in range(amount):`
            self.view.run_command("move", args=kwargs)

{ "keys": ["ctrl+alt+up"], "command": "move_amount", "args": {"amount":10} }```

Thanks 

Ps Finally cracked the formatting, woo hoo !!

Pps I know the keyboard input is being processed by ST3 because this
{ "keys": ["ctrl+alt+up"], "command": "insert", "args": {"characters": "PRESSING UP"}},`
outputs to tab **PRESSING UP** when i press *ctrl+alt+up*

Ppps Almost cracked the formatting....
0 Likes

#2

The move_amount command implemented by the plugin is a drop-in replacement for the internal move command with the added ability to take the action more than once per key press.

So, your binding doesn’t work because you haven’t provided the arguments that move expects to know how to move.

For example, the default binding for going up a single line is:

{ "keys": ["up"], "command": "move", "args": {"by": "lines", "forward": false} },

So, to use move_amount to take that action but go up by 10, you need to also include the by and forward arguments to the command to tell it that you’re moving by lines going backwards:

{ "keys": ["ctrl+alt+up"], "command": "move_amount", "args": {"by": "lines", "forward": false, "amount": 10} },

If you want to also extend the selection when you move, the extend argument can be provided to indicate that, such as in the default binding for moving up a line and also extending the selection:

{ "keys": ["shift+up"], "command": "move", "args": {"by": "lines", "forward": false, "extend": true} },
1 Like

#3

I was really excited for a moment. Nothing happens, copied and pasted your command in, cursor remains immovable.
Tried different keys, nothing
This is output from control panel.

command: move_amount {"amount": 10, "by": "lines", "forward": false}

I guess it’s the python code, but that’s a copy and paste from your old post…

Thanks for all your efforts, I’d have moved to other pastures otherwise :wink:

ATB :unicorn:

0 Likes

#4

The three backticks that close a code block need to be a line on their own; in your post they’re on the same line as the key binding.

Not sure if it’s present in your file as you actually have it installed, but your paste above as a trailing ` character at the end of the for line, which would make the code invalid.

For completeness, that plugin is also available on GitHub, which links to the forum post you mentioned and also to an SO post with additional example key bindings.

0 Likes

#5

The ` is a hangover from my battles with the formatting…

The code in GitHub is exactly the same as in my plugin. I copied and pasted it over from github, no differences.

Unfortunately I can’t even get a
print "here"
statement to work in the python code because I know no python and know nothing about the ST3/python interface. I have had a look about, all good fun if one had some extra time, but it’s beyond me. The being able to move somewhere between 1 and 52 lines would great esp the selection option too !

Ps My machine has been on and off (so reboot), no avail, caret/cursor won’t budge…

0 Likes

#6

In a pinch, all you have to do is open the plugin file and then save it; every time Sublime sees a plugin appear or be modified, it’ll reload it. So you should see a message in the console that says something like Reloading User.someplugin. If you see that and then an error message, the code is broken somehow. If instead you don’t see the message at all, then the plugin isn’t in a place that it will load (which has to be in the top level of a package, generally your User package).

0 Likes

#7
    self.view.run_command("move", args=kwargs)
                                             ^
TabError: inconsistent use of tabs and spaces in indentation

Is the error message from the control panel

import sublime, sublime_plugin

class MoveAmountCommand(sublime_plugin.TextCommand):
    def run(self, edit, amount=1, **kwargs):
        for _ in range(amount):
						self.view.run_command("move", args=kwargs)

Unfortunately the formatting here isn’t replicating what I’ve just copied and pasted, but I did a copy and paste from github, even if this doesn’t look like it, it is

Ps I’ve some plugin installation experience, you’ve written a couple for me before…:+1:

0 Likes

#8

Python is sensitive to indentation, so it’s important that the whole file either use tabs or spaces. As a sanity test, selecting all of the text in the buffer should show that every line either has spaces leading up to the first character (dots) or tabs (lines); if there’s a mismatch, that’s the reason.

1 Like

#9

Yep cracked it, it was a mixture of tabs and spaces, tho the indenting looked the same. Nice little trick that, using the select all to show what’s what. Fab, fab, fab !

:revolving_hearts: @OdatNurd :revolving_hearts:

0 Likes

#10

I assume the above would mean this, but it doesn’t seem to work. We’re (and by that I mean you) winning !!

{ "keys": ["ctrl+alt+shift+down"], "command": "move_amount", "args": {"by": "lines", "forward": true, "extend": true, "amount": 7} },
0 Likes

#11

The only explanation I can offer for that one is something outside of Sublime seeing the key and eating it before Sublime can handle it, which would be shown by using sublime.log_input(True) in the console and pressing the key to see if Sublime sees it (and sees it as what you expect).

Apart from that I can’t see anything obviously wrong; that binding works here with no issues.

1 Like

#12

In my defence it’s 4.30 AM here. I had to disable alt+ctrl+up earlier for the OS. Doh ! Anyway selecting / moving 7 (or any i want) lines at a time, spectacular !

Thank you !

0 Likes