Column mode smart selection shortcut
-
I’m trying to find a way to improve my usage of the Column mode option, thus I’m in the need of something like this:
Basically, move the carret up (or down) until it hits the “top” (or “bottom”) so I don’t have to press Alt+Shift that many times, or use the mouse, god forbid. A “smart” column mode selection if you will. It would be even more helpful if also worked like that:
In other words, move upwards until either the line doesn’t go that far or the left or right character is different from those at the initial position. Or at least it’s how I think a script should handle it. Yes, a script, I think that’s the only way… Although it would be nice to have it as a standard feature, I’m sure other people would find it useful.
So before I dabble in python or lua or something, if anyone could point me in the right direction, preferably a easier way or “it’s already been done, stupid” I’d appreciate.
-
@notdodgeball said in Column mode smart selection shortcut:
so I don’t have to press Alt+Shift that many times
Well, there’s this that may help:
User manual ref..
-
Ok, my first python script:
if editor.getSelectionEmpty(): currentLine = editor.lineFromPosition(editor.getCurrentPos()) currentLineLength = editor.lineLength(currentLine) currentLineStart = editor.positionFromLine(currentLine) currentPos = editor.getCurrentPos() currentHorizontalPos = currentPos - currentLineStart currentChar = editor.getTextRange(currentPos, currentPos + 1) for x in range(1, 69): previousLineStart = editor.positionFromLine(currentLine-x) previousLinelength = editor.lineLength(currentLine-x) if editor.lineLength(currentLine-x) < currentHorizontalPos: break previousLineSamePosition = previousLineStart + currentHorizontalPos previousChar = editor.getTextRange(previousLineSamePosition, previousLineSamePosition + 1) if currentChar != previousChar: break else: editor.addSelection(previousLineSamePosition,previousLineSamePosition)
There is better ways to do it, I assume, but it worked so far. Please tell me if there is something outstandingly bad.
-
@notdodgeball said in Column mode smart selection shortcut:
if there is something outstandingly bad
Probably this is a leading candidate:
69
-
@notdodgeball
To expand on why @Alan-Kilborn said the number69
is “oustandingly bad”:It’s (usually) bad practice to include “magic numbers” like
69
in your code with no explanation as to why you chose that number. Better to declare a global constant (preferably with an explanatory comment) like so, and then reference it later on in the code:# I'm using a proprietary file format where every file has 69 lines MY_MAGIC_NUMBER = 69 ... ... for x in range(1, MY_MAGIC_NUMBER):
That may reduce your confusion weeks later when you stumble across this script and wonder what the hell it’s supposed to do.
-
Yeah, I know, I thought putting it as 69 would make it clear but yes, you are right.
Regardless, I was putting it into use and it’s actually worse than that. When the file has tabs, getCurrentPos() it does not line up as the position varies depending on the tabulation. How do circumvent it? I assume getColumn() might help looking at the docs. Hum…
-
if editor.getSelectionEmpty(): currentLine = editor.lineFromPosition(editor.getCurrentPos()) currentLineLength = editor.lineLength(currentLine) currentPos = editor.getCurrentPos() currentCol = editor.getColumn(currentPos) currentLineStart = editor.positionFromLine(currentLine) currentHorizontalPos = currentPos - currentLineStart currentRChar = editor.getTextRange(currentPos, currentPos + 1) currentLChar = editor.getTextRange(currentPos, currentPos - 1) count = 0 while True: count += 1 previousLine = currentLine-count previousLineStart = editor.positionFromLine(previousLine) previousLineLength = editor.lineLength(previousLine) previousPos = editor.findColumn(previousLine,currentCol) previousCol = editor.getColumn(previousPos) previousRChar = editor.getTextRange(previousPos, previousPos + 1) previousLChar = editor.getTextRange(previousPos, previousPos - 1) if currentRChar != previousRChar or currentLChar != previousLChar or previousCol != currentCol: break editor.addSelection(previousPos,previousPos)
-
@notdodgeball said in Column mode smart selection shortcut:
I thought putting it as 69 would make it clear but yes
Shouldn’t that be 42 instead? ;-)
-
@Ekopalypse said in Column mode smart selection shortcut:
Shouldn’t that be 42 instead?
That just selects everything. And by “everything,” I mean everything: life, the universe, etc.
-
This post is deleted! -
This post is deleted!