Sublime Forum

Jump to next empty line?

#1

is there a way to remap ctrl-up/down to jumpt to the next empty line?

i use this for navigation in emacs all the time, and just leave an empty line as punctuation in my code…

tx!

0 Likes

Move cursor up or down multiple lines?
#2

you could look at using bookmarks. Ctrl+F2 to toggle a bookmark, and F2 to go to the next one.

0 Likes

#3

You could use the code below and tweek it a bit.
My previous editor was TextPad and I used move by paragraph all the time. I missed it dearly and wrote a command for it.
I have it mapped to alt+up/down and the selecting variants mapped to ctrl+shift+up/down.
Hope this helps.

…\AppData\Roaming\Sublime Text 2\Packages\User\move_by_paragraph.py:

[code]import sublime, sublime_plugin

class MoveByParagraphCommand(sublime_plugin.TextCommand):
def run(self, edit, extend = False, forward = True):
self.view.run_command(“move_to”, {“to”: “hardbol”, “extend”: extend}) # to mimic TextPad’s behaviour
pt = self.view.sel()[0].b
if forward:
rg = self.view.find("\n\s*\n", pt)
new_pt = rg.b if rg else self.view.size()
else:
# couldn’t find “find previous” command
rgs = self.view.find_all("\n\s]*\n")
new_pt = 0
for rg in rgs:
if rg.b < pt:
new_pt = rg.b
new_pt_visible = self.view.visible_region().contains(new_pt)
#
# obviously inefficient way of doing it (couldn’t find amount for move command)
# becomes slow for veeeeery long paragraphs
self.view.run_command(“move”, {“by”: “lines”, “forward”: forward, “extend”: extend})
while (self.view.sel()[0].b < new_pt) == forward and self.view.sel()[0].b != pt:
pt = self.view.sel()[0].b
self.view.run_command(“move”, {“by”: “lines”, “forward”: forward, “extend”: extend})
while self.view.sel()[0].b != new_pt:
self.view.run_command(“move”, {“by”: “characters”, “forward”: self.view.sel()[0].b < new_pt, “extend”: extend})
if not new_pt_visible:
self.view.show_at_center(new_pt)
[/code]

In …\AppData\Roaming\Sublime Text 2\Packages\User\Default (Windows).sublime-keymap:

{ "keys": "ctrl+shift+up"], "command": "move_by_paragraph", "args": {"extend": true, "forward": false} }, { "keys": "ctrl+shift+down"], "command": "move_by_paragraph", "args": {"extend": true} }, { "keys": "alt+up"], "command": "move_by_paragraph", "args": {"extend": false, "forward": false} }, { "keys": "alt+down"], "command": "move_by_paragraph", "args": {"extend": false} },

0 Likes

#4

Another option is:

{ "keys": "alt+up"], "command": "move", "args": {"by": "stops", "empty_line": true, "forward": false} },
{ "keys": "alt+down"], "command": "move", "args": {"by": "stops", "empty_line": true, "forward": true} }
1 Like

#5

[quote=“jps”]Another option is:

{ "keys": "alt+up"], "command": "move", "args": {"by": "stops", "empty_line": true, "forward": false} }, { "keys": "alt+down"], "command": "move", "args": {"by": "stops", "empty_line": true, "forward": true} } [/quote]

I was looking for the same command (curly braces in Vim command mode, I always used it) and this is the correct answer, thanks.

0 Likes

#6

Didn’t know “move by stops” exists !!!

Is there a comprehensive list of command and their arguments ?
Maybe there’s a way to generate it automatically ?

0 Likes

#7

More to the point: how do we extend the above command with shift to get selection by whole paragraphs?
(shift+alt+up/down = select up to the prev/next empty line)

This would be very useful to quickly move blocks of code around.

Is there a select by stops command?

0 Likes

#8

You can add “extend”:“true” to any movement command, I think. Just create another binding with the shift key involved.

0 Likes

#9

Thank you!

This is the complete set of bindings that solve my issue:

{"keys": "alt+up"], "command": "move", "args": {"by": "stops", "empty_line": true, "forward": false}},
{"keys": "alt+down"], "command": "move", "args": {"by": "stops", "empty_line": true, "forward": true}},
{"keys": "shift+alt+up"], "command": "move", "args": {"by": "stops", "empty_line": true, "forward": false, "extend": true}},
{"keys": "shift+alt+down"], "command": "move", "args": {"by": "stops", "empty_line": true, "forward": true, "extend": true}},
0 Likes

#10

Thanks lappri,

I have been missing paragraph jumping tonnes.

0 Likes

#11

[quote=“jps”]Another option is:

{ "keys": "alt+up"], "command": "move", "args": {"by": "stops", "empty_line": true, "forward": false} }, { "keys": "alt+down"], "command": "move", "args": {"by": "stops", "empty_line": true, "forward": true} } [/quote]

Most excellent.
Trying to make the transition from TextPad. Adding this makes me feel a bit more at home.
Thanks

0 Likes

#12

The syntax in all posts shown so far was incorrect, "keys": "alt+up"] lacks an open bracket [.

Here’s my fixed version:

// Jumping over blocks (to next empty line)
{"keys": ["alt+up"], "command": "move", "args": {"by": "stops", "empty_line": true, "forward": false}},
{"keys": ["alt+down"], "command": "move", "args": {"by": "stops", "empty_line": true, "forward": true}},
// With selecting
{"keys": ["ctrl+shift+alt+up"], "command": "move", "args": {"by": "stops", "empty_line": true, "forward": false, "extend": true}},
{"keys": ["ctrl+shift+alt+down"], "command": "move", "args": {"by": "stops", "empty_line": true, "forward": true, "extend": true}},
0 Likes