Skip to content

Commit 43b2118

Browse files
committed
Initial checkin.
1 parent cd63fbe commit 43b2118

7 files changed

+766
-2
lines changed

ColumnSelect.sublime-project

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"folders":
3+
[
4+
{
5+
"path": "."
6+
}
7+
],
8+
"settings":
9+
{
10+
"pylinter": {
11+
"run_on_save": false
12+
}
13+
}
14+
}

Default (Linux).sublime-keymap

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[
2+
{ "keys": ["alt+shift+up"], "command": "column_select", "args": {"by": "lines", "forward": false}},
3+
{ "keys": ["alt+shift+down"], "command": "column_select", "args": {"by": "lines", "forward": true}},
4+
{ "keys": ["alt+shift+pageup"], "command": "column_select", "args": {"by": "pages", "forward": false}},
5+
{ "keys": ["alt+shift+pagedown"], "command": "column_select", "args": {"by": "pages", "forward": true}},
6+
{ "keys": ["alt+shift+home"], "command": "column_select", "args": {"by": "all", "forward": false}},
7+
{ "keys": ["alt+shift+end"], "command": "column_select", "args": {"by": "all", "forward": true}}
8+
]

Default (OSX).sublime-keymap

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[
2+
{ "keys": ["ctrl+shift+up"], "command": "column_select", "args": {"by": "lines", "forward": false}},
3+
{ "keys": ["ctrl+shift+down"], "command": "column_select", "args": {"by": "lines", "forward": true}},
4+
{ "keys": ["ctrl+shift+pageup"], "command": "column_select", "args": {"by": "pages", "forward": false}},
5+
{ "keys": ["ctrl+shift+pagedown"], "command": "column_select", "args": {"by": "pages", "forward": true}},
6+
{ "keys": ["ctrl+shift+home"], "command": "column_select", "args": {"by": "all", "forward": false}},
7+
{ "keys": ["ctrl+shift+end"], "command": "column_select", "args": {"by": "all", "forward": true}}
8+
]

Default (Windows).sublime-keymap

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[
2+
{ "keys": ["ctrl+alt+up"], "command": "column_select", "args": {"by": "lines", "forward": false}},
3+
{ "keys": ["ctrl+alt+down"], "command": "column_select", "args": {"by": "lines", "forward": true}},
4+
{ "keys": ["ctrl+alt+pageup"], "command": "column_select", "args": {"by": "pages", "forward": false}},
5+
{ "keys": ["ctrl+alt+pagedown"], "command": "column_select", "args": {"by": "pages", "forward": true}},
6+
{ "keys": ["ctrl+alt+home"], "command": "column_select", "args": {"by": "all", "forward": false}},
7+
{ "keys": ["ctrl+alt+end"], "command": "column_select", "args": {"by": "all", "forward": true}}
8+
]

README.md

+55-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,57 @@
1-
Sublime-Column-Select
1+
Sublime Column Select
22
=====================
33

4-
Alternate behavior for Sublime keyboard column selection.
4+
This plugin provides an alternate behavior for Sublime keyboard column selection. I often found it challenging to select columns with lines that were shorter than the rest.
5+
6+
Downloading
7+
-----------
8+
The best way to download and install Sublime Column Select is to use the Package Control plugin. If you do not already have it installed, it's really the best way to manage your packages.
9+
10+
XXX TBD
11+
12+
Configuring
13+
-----------
14+
No need to configure anything. By default it uses the default keystroke for column selection, plus a few extras. These keystrokes will select the same column in the next or previous line, page, or until the beginning/end of the document.
15+
16+
Windows:
17+
18+
- Ctrl-Alt-Up / Ctrl-Alt-Down: Up/down one line.
19+
- Ctrl-Alt-PageUp / Ctrl-Alt-PageDown: Up/down one page.
20+
- Ctrl-Alt-Home: Up until the beginning of the document.
21+
- Ctrl-Alt-End: Down until the end of the document.
22+
23+
Linux:
24+
25+
- Alt-Shift-Up / Ctrl-Alt-Down: Up/down one line.
26+
- Alt-Shift-PageUp / Ctrl-Alt-PageDown: Up/down one page.
27+
- Alt-Shift-Home: Up until the beginning of the document.
28+
- Alt-Shift-End: Down until the end of the document.
29+
30+
OS X:
31+
32+
- Ctrl-Shift-Up / Ctrl-Alt-Down: Up/down one line.
33+
- Ctrl-Shift-PageUp / Ctrl-Alt-PageDown: Up/down one page.
34+
- Ctrl-Shift-Home: Up until the beginning of the document.
35+
- Ctrl-Shift-End: Down until the end of the document.
36+
37+
If you want to use a different keystroke, go to "Preferences" then "Key Bindings - User", and add an entry like this:
38+
39+
{ "keys": ["ctrl+alt+up"], "command": "column_select", "args": {"by": "lines", "forward": false}},
40+
{ "keys": ["ctrl+alt+down"], "command": "column_select", "args": {"by": "lines", "forward": true}},
41+
{ "keys": ["ctrl+alt+pageup"], "command": "column_select", "args": {"by": "pages", "forward": false}},
42+
{ "keys": ["ctrl+alt+pagedown"], "command": "column_select", "args": {"by": "pages", "forward": true}},
43+
{ "keys": ["ctrl+alt+home"], "command": "column_select", "args": {"by": "all", "forward": false}},
44+
{ "keys": ["ctrl+alt+end"], "command": "column_select", "args": {"by": "all", "forward": true}},
45+
46+
Do not include the trailing comma if it is the last entry.
47+
48+
Using
49+
-----
50+
You should be able to place the character on any position on the line, and then use the keystrokes to add additional carets (cursors) to the next/previous lines. It will skip over lines that are too short.
51+
52+
It will behave differently if the cursor is at the end of the line. In this case, it will select the end of every line.
53+
54+
Contact
55+
-------
56+
If you find a bug, or have suggestions, head over to the github page:
57+
https://github.com/ehuss/Sublime-Column-Select

column_select.py

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import sys
2+
import sublime, sublime_plugin
3+
4+
class ColumnSelect(sublime_plugin.TextCommand):
5+
6+
def all_selections_at_end(self, sel):
7+
at_end = True
8+
for i, s in enumerate(sel):
9+
# Don't bother looking at more than a thousand lines.
10+
if i > 1000:
11+
break
12+
if s.end() != self.view.line(s.end()).end():
13+
at_end = False
14+
break
15+
return at_end
16+
17+
def run(self, edit, by, forward):
18+
all_sel = self.view.sel()
19+
20+
# Whether or not to ignore lines that are too short in the line count.
21+
ignore_too_short = True
22+
# How far to go?
23+
if by == 'lines':
24+
num_lines = 1
25+
elif by == 'pages':
26+
# Is there a better way?
27+
vr = self.view.visible_region()
28+
lines = self.view.lines(vr)
29+
num_lines = len(lines)
30+
ignore_too_short = False
31+
elif by == 'all':
32+
num_lines = sys.maxint
33+
else:
34+
sublime.error_message('Invalid "by" argument.')
35+
return
36+
37+
all_at_end = self.all_selections_at_end(all_sel)
38+
39+
if forward:
40+
sp = all_sel[-1].end()
41+
else:
42+
sp = all_sel[0].begin()
43+
current_line = self.view.line(sp)
44+
sp_column = sp - current_line.begin()
45+
# Cound of selections made.
46+
sel_count = 0
47+
# Count of lines traversed. Lines too short are not counted unless
48+
# ignore_too_short is False.
49+
line_count = 0
50+
# Keep track of the last selection made to scroll the viewport.
51+
last_sel = None
52+
while 1:
53+
if line_count >= num_lines:
54+
# Force it to select at least one line.
55+
if sel_count > 0:
56+
break
57+
if forward:
58+
if current_line.end() == self.view.size():
59+
# End of view.
60+
break
61+
next_line = self.view.line(current_line.end()+1)
62+
else:
63+
if current_line.begin() == 0:
64+
# Beginning of view.
65+
break
66+
next_line = self.view.line(current_line.begin()-1)
67+
if all_at_end:
68+
sel_pos = next_line.end()
69+
else:
70+
if next_line.size() >= sp_column:
71+
# A good line.
72+
sel_pos = next_line.begin()+sp_column
73+
else:
74+
# Line too short.
75+
sel_pos = None
76+
if sel_pos == None:
77+
if not ignore_too_short:
78+
line_count += 1
79+
else:
80+
r = sublime.Region(sel_pos, sel_pos)
81+
last_sel = r
82+
all_sel.add(r)
83+
line_count += 1
84+
sel_count += 1
85+
current_line = next_line
86+
87+
if last_sel != None:
88+
self.view.show(last_sel)
89+

0 commit comments

Comments
 (0)