Sublime Forum

Add cursors at beginning/end of real lines, not visual ones

#1

I have created two macro that allow to use Control-Shift-Up and Control-Shift-Down keyboard shortcuts to add cursors at the very beginning (or end) of real lines, not visual lines. This makes sense when you use the line wrapping option turned on.

select-lines-forward-false.sublime-macro:

[
  { "command": "toggle_setting", "args": {"setting": "word_wrap"} },
  { "command": "select_lines", "args": {"by": "lines", "forward": false} },
  { "command": "toggle_setting", "args": {"setting": "word_wrap"} }
]

select-lines-forward-true.sublime-macro:

[
  { "command": "toggle_setting", "args": {"setting": "word_wrap"} },
  { "command": "select_lines", "args": {"by": "lines", "forward": true} },
  { "command": "toggle_setting", "args": {"setting": "word_wrap"} }
]

Default (OSX).sublime-keymap:

[
  {
    "keys": ["ctrl+shift+up"],
    "context": [{"key": "setting.word_wrap", "operator": "equal", "operand": true}],
    "command": "run_macro_file",
    "args": {"file": "res://Packages/xyz/select-lines-forward-false.sublime-macro"}
  },
  {
    "keys": ["ctrl+shift+down"],
    "context": [{"key": "setting.word_wrap", "operator": "equal", "operand": true}],
    "command": "run_macro_file",
    "args": {"file": "res://Packages/xyz/select-lines-forward-true.sublime-macro"}
  }
]

This works, but because the macro first turns line wrapping off and then returns it to on, the screen is sometimes scrolled automatically, and this is bad, of course.

You can test it yourself:

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

Turn line wrapping on, then put the cursor at the very beginning of the last real line (that is, before the word “Lorem”), and press Control-Shift-Up. You will see the cursors were added as follows:

image

… and this is fine, but the screen has been scrolled. And this is bad.

Is there a way to prevent this? Maybe a Python plugin instead? Could someone help with this?

0 Likes

#2

Hi Johnn, you can achieve the same results, without having to unwrap and the re-wrap the lines of code. There are hardbol and hardeol flags. I have used these to override the default line selection keymaps:

[{
        "keys": ["super+left"],
        "command": "move_to",
        "args":
        {
            "to": "hardbol",
            "extend": false
        }
    },
    {
        "keys": [
            "super+shift+left"
        ],
        "command": "move_to",
        "args":
        {
            "to": "hardbol",
            "extend": true
        }
    },
    {
        "keys": [
            "super+right"
        ],
        "command": "move_to",
        "args":
        {
            "to": "hardeol",
            "extend": false
        }
    },
    {
        "keys": [
            "super+shift+right"
        ],
        "command": "move_to",
        "args":
        {
            "to": "hardeol",
            "extend": true
        }
    }]`
1 Like

#3

Hi gbird! It seems you can add

"context": [{"key": "setting.word_wrap", "operator": "equal", "operand": true}],

to make your code even better :slight_smile:

I copied it into my config, thanks. But could you show how exactly this technique can be used to achieve exactly what I’m trying to achieve?

0 Likes

#4

It allows you to either move to the start/end of a line, or select to start/end of line, even if that line happens to soft wrap over multiple lines.

In this way, you don’t need to toggle the wrapping on and off, and you avoid the viewport jumping around.

I assume this is what you wanted?

0 Likes

#5

It seems so.

Case 1:

I have 3 wrapped lines and my cursor is at the beginning of the first line. When I press Control-Shift-Down 4 times, I want the cursor to be added at the beginning of each real line, as well as on empty lines:

|Foo lorem ipsum
dolor sit amet
|
|Bar lorem ipsum
dolor sit amet
|
|Baz lorem ipsum
dolor sit amet

Case 2:

The cursor is at the end of the first line, and when I press Control-Shift-Down 4 times, I want it to be added at the end of each real line, as well as on empty lines:

Foo lorem ipsum
dolor sit amet|
|
Bar lorem ipsum
dolor sit amet|
|
Baz lorem ipsum
dolor sit amet|

Case 3:

The cursor is at the middle of the first line, and when I press Control-Shift-Down 4 times, I want it to be added at the same position on non-empty lines, as well as on empty lines:

Foo| lorem ipsum
dolor sit amet
|
Bar| lorem ipsum
dolor sit amet
|
Baz| lorem ipsum
dolor sit amet
0 Likes

#6

Oops! Sorry! I meant: When I press Control-Shift-Down 4 times. Not twice!

0 Likes

#7

Ahh, perhaps I was solving a different problem then. Forgive my misunderstanding.

Regarding your 3 use-cases. I do that differently - I simply use the inbuilt split-lines capability (which I have mapped to command-shift-l)

{
        "keys": ["command+shift+l"],
        "command": "split_selection_into_lines"
    }

By default, the places the cursor at hardeol (case 2), but I can then use the command-shift-left shortcut from earlier, to reposition to hardbol (case 1) if needed. You can also use the left and right keys to shift all cursors to any arbitrary position (case 3).

I guess it is what you are most comfortable with.

0 Likes

#8

Hm :thinking: It seems it is not necessary to bind command+shift+l to split_selection_into_lines, because it is already binded by default:

{ "keys": ["super+shift+l"], "command": "split_selection_into_lines" },

Then, another thing. By default, it doesn’t work really good. What I use myself is the following macro instead, and then Command-Left:

[
  { "command": "split_selection_into_lines" },
  { "command": "move_to", "args": { "to": "hardeol" } },
  { "command": "move_to", "args": { "to": "hardbol", "extend": true } }
]

But I would be happy to find a way to extrapolate this logic to Control-Shift-Up and Control-Shift-Down as well.

0 Likes

#9

Ah yes, but I am using the Pieces package (AI code hints), which stole command+shift+l. I am stealing it back :wink:

I guess we all have our quirks and preferred ways of working. Split lines works in a way that makes sense to me and fits in with my work patterns. Your milage may vary.

You seem to have your own workaround now. Good luck

0 Likes

#10

upd.

I tried to fix my original macros using { "command": "scroll_to_selection" }, but this doesn’t help, for some reason. Any ideas why, folks?

[
  { "command": "toggle_setting", "args": { "setting": "word_wrap" } },
  { "command": "select_lines", "args": { "by": "lines", "forward": false } },
  { "command": "toggle_setting", "args": { "setting": "word_wrap" } },
  { "command": "scroll_to_selection" },
]

The console says “Unknown macro command scroll_to_selection”. The command itself can be found under Goto > Scroll.

upd2: Okay, I figured out it should be show_at_center instead.

Now it works… It’s ugly, though, because the screen twitches. I still looking for a better solution.

0 Likes

#11

I suspect that toggling word_wrap on and off is causing the screen to ‘twitch’. Esp it is a long document. I suspect the secret is to not do this and instead use the ‘hardeol’ approach to make your line selections, but at this point my knowledge runs out.

0 Likes

#12

I suspect that toggling word_wrap on and off is causing the screen to ‘twitch’

Yes, of course!

I suspect the secret is to not do this and instead use the ‘hardeol’ approach

Probably!

…but at this point my knowledge runs out.

And this is the exact problem we currently have. :smile:

Any ideas, folks?

0 Likes