Sublime Forum

Select line also select the beginning of the next line

#1

Hi,
I’m having a strange behavior with “Expend Selection to Line” (Ctrl+L).

lets say I have the following code:

just an example
next line

When I place the cursor over the first line, hit Ctrl+L, it selects line 1 up until the beginning of line 2 (just before the word “next”).

These are my User Settings:

{
	"default_line_ending": "unix",
	"detect_indentation": true,
	"font_size": 14,
	"ignored_packages":
	
		"Vintage"
	],
	"smart_indent": true,
	"highlight_line": true,
	"translate_tabs_to_spaces": true,
	"use_tab_stops": true
}

What might be the problem?

0 Likes

#2

I think this behaviour is by design, though it’s less than ideal. Other editors have the notion of modal selection: line, column and linear, whereas sublime only has one (linear) selection that’s kludged to do all three. It’s engineered this way due to sublime’s multiselection/multicursor features.

The behaviour you describe is a function of the above design behaving intuitively when combined with, say, delete - if the caret didn’t move to the next line, delete wouldn’t delete the whole line, it would only empty the selected line.

Though I say “kludged” above in reality it’s not that bad, it just leads to annoying workflow inconsistencies. For example, if I press ctrl+l then shift+down x2, I’ve selected 3 lines. I then ctrl+c to copy, dump the caret on another line in my file and hit ctrl+v. What I want to happen is the 3 lines I copied to be inserted above the current line. What actually happens is the 3 lines are inserted at the current caret position, screwing up indents and generally not what’s wanted. To make things work right, I need to go to the line I want to insert above, hit up, ctrl+enter to open a new line, home to move to the beginning of that (blank) line, then ctrl+v.

Whilst it’s easy to be critical about this, I also recognise the potential clash between modal selections (line, column, linear) and multiple cursors/selections. I’m sure that with some thought it could be done well, but Jon’s opted for the KISS approach. Given the benefits that multiple selections/cursors brings, it’s a compromise I can live with, though I’d love to see an elegant melding of the two concepts.

Better macros (too limited atm) and a combination of simple plugin-style extensions could probably alleviate most of the headaches with the current model.

0 Likes

#3

If you really dislike that behavior you can use a plugin to get the behavior you want. Assuming you just don’t like the fact that it grabs the newline also, you can create the following plugin.

[code]import sublime
import sublime_plugin

class MyLineSelectionCommand(sublime_plugin.TextCommand):
def run(self, edit):
view = self.view
new_cursors = ]
cursors = view.sel()
for cursor in cursors:
new_cursors.append(view.line(cursor))
cursors.clear()
for cursor in new_cursors:
cursors.add(cursor)[/code]

Then add the following as a key binding

{ "keys": "ctrl+l"], "command": "my_line_selection" }
1 Like

#4

Thank you :smile:

1 Like

#5

hi, I know this is an old thread but I would really like to use this plugin. I don’t know how to set it up… anyway I put the following code you gave into a file in my users Sublime Text 3/packages/user/ directory and saved it as my_line_selection.py

import sublime
import sublime_plugin
class MyLineSelectionCommand(sublime_plugin.TextCommand):
def run(self, edit):
view = self.view
new_cursors = ]
cursors = view.sel()
for cursor in cursors:
new_cursors.append(view.line(cursor))
cursors.clear()
for cursor in new_cursors:
cursors.add(cursor)

then I went into my user keymap file and added this line
{ “keys”: [“ctrl+l”], “command”: “my_line_selection” }

and saved it, but it doesn’t do anything. am I missing something here?

0 Likes

#6

There seems to be a typo in the code. Line 6 should have both [], not just ]

0 Likes

#7

This is caused by a faulty import from the old forum software where [ characters were missed in code blocks. The import occured at the start of 2016.

1 Like

#8

Hi

import sublime
import sublime_plugin
class MyLineSelectionCommand(sublime_plugin.TextCommand):
def run(self, edit):
view = self.view
new_cursors = []
cursors = view.sel()
for cursor in cursors:
new_cursors.append(view.line(cursor))
cursors.clear()
for cursor in new_cursors:
cursors.add(cursor)

I also have got problem. How to fix this problem

0 Likes

#9

Python is an indentation based language. You’ll have to indent your code properly.

import sublime
import sublime_plugin

class MyLineSelectionCommand(sublime_plugin.TextCommand):
	def run(self, edit):
		view = self.view	
		new_cursors = []
		cursors = view.sel()
		for cursor in cursors:
			new_cursors.append(view.line(cursor))
			cursors.clear()
		for cursor in new_cursors:
			cursors.add(cursor)
1 Like

#10

Thanks error is left but line selects with line break:(

0 Likes

#11

Can you explain a bit more on what you are trying to achieve ? I can’t understand what your GIF is trying to convey.

1 Like

#12

I use ctrl+l to select line

I set up key bindings to this function
{
“keys”: “ctrl+l”,
“command”: “my_line_selection”
},

So I need to select entire line with OUT line brake at the end

0 Likes

#13

The following method worked for me like a charm.

Install the Pacage :ChainOfCommand

Add this key binding

{ "keys": ["ctrl+l"], 
	"command": "chain", 
	"args": 
	{
		"commands":
		[
	      ["expand_selection",{"to": "line"} ],
	      
	      ["move",{"by": "characters", "forward": false, "extend": true}],
	    ]
	}	
}

This idea behind this is simple , i am using two commands for the shortcut(ctrl+l) to achieve the result. The problem is , ctrl+l(by default) selects the line but also first character of the new line.
What this keybinding does ,
It selects the line + newline char but then the second command(copied from shift+left ) deselects the newline character and bravo!

Limitation: Now you cannot select multiple lines by hitting ctrl+l multiple times. But a new keybinding will solve this easily (copy the default ctrl+l keybinding and assign it to something else)
Hope it helps.

0 Likes

#14

Just to tack on to that response, if you’re using ST4 you don’t need to install the Chain Of Command (or MultiCommand) package; there’s a chain command built into ST4 that takes the same arguments as the package does, so it’s now officially in the core:

    { "keys": ["super+t"], "command": "chain",
        "args": {
            "commands": [
                ["echo", {"chain": true}], ["echo", {"chain": false}]
            ]
        },
    },
    { "keys": ["super+s"], "command": "chain",
        "args": {
            "commands": [
                {"command": "echo", "args":{"chain": true} },
                {"command": "echo", "args":{"chain": false} }
            ]
        },
    },
0 Likes