Sublime Forum

Sort Open Files

#12

There is an API method to set the position of a tab in the tabbar with set_view_index. Example to move the current view to group 0 in position 0

You can check current view_index with

[quote]
group, index = sublime.active_window().get_view_index(sublime.active_window().active_view())
print group
print index[/quote]

sublimetext.com/docs/2/api_reference.html

0 Likes

#13

Thank you tito. This could be an additional feature to the one I’ve built. Andy.

0 Likes

#14

Inspired/ encouraged by tito, the following will sort tabs alphabetically. My previous code was case-sensitive, whereas this is not. It should also sort within each existing group - although I haven’t tried this.

[code]import sublime, sublime_plugin, os

class SortTabsCommand(sublime_plugin.TextCommand):
def run(self, edit):
file_views = ]
for vw in self.view.window().views():
head, tail = os.path.split(vw.file_name())
file_views.append((tail, vw))
file_views.sort(key = lambda (tail, ): tail.lower())
win = self.view.window()
for index, (
, vw) in enumerate(file_views):
group, _ = win.get_view_index(vw)
win.set_view_index(vw, group, index)[/code]
Added: Careful! It doesn’t work with more than one group as yet.

0 Likes

#15

Now we can work with more than one group (if you use such things…)

[code]import sublime, sublime_plugin, os
from operator import itemgetter

class SortTabsCommand(sublime_plugin.TextCommand):
def run(self, edit):
file_views = ]
win = self.view.window()
curr_view = win.active_view()
for vw in win.views():
head, tail = os.path.split(vw.file_name())
group, _ = win.get_view_index(vw)
file_views.append((tail.lower(), vw, group))
file_views.sort(key = itemgetter(2, 0))
for index, (_, vw, group) in enumerate(file_views):
if not index:
prev_group = group
moving_index = 0
elif group > prev_group:
moving_index = 0
prev_group = group
else:
moving_index += 1
win.set_view_index(vw, group, moving_index)
win.focus_view(curr_view)[/code]

0 Likes

#16

I’m interested into having a “sort tabs” package. In rare occasions is useful, please add it to github. if you can add it to the sublime organization better.

0 Likes

#17

I’ve put it on Gist for the moment. Oops, try this https://gist.github.com/1995067

This final (again) version of OrderedFiles.py is now case-insensitive:

[code]import sublime, sublime_plugin, os, datetime
from operator import itemgetter

file_views = ]

class OrderedFilesCommand(sublime_plugin.TextCommand):
def run(self, edit, index):
global file_views
file_views = ]
for vw in self.view.window().views():
head, tail = os.path.split(vw.file_name())
modified = os.path.getmtime(vw.file_name())
file_views.append((tail, vw, modified))
if index == 0:
file_views.sort(key = lambda (tail, Doh, Dur): tail.lower())
else:
file_views.sort(key = itemgetter(2))
if index == 2:
self.view.window().show_quick_panel(
(datetime.datetime.fromtimestamp(z)).strftime("%d-%m-%y %H:%M ") + x
for (x, y, z) in file_views], self.on_chosen)
else:
self.view.window().show_quick_panel([x for (x, y, z) in file_views],
self.on_chosen)

def on_chosen(self, index):
	if index != -1:
		win = self.view.window()
		win.focus_view(file_views[index][1])[/code]
0 Likes

#18

OrderedFiles.py is more sensible as a WindowCommand, and I’ve *tidied *the code a little.

[code]import sublime_plugin
from os import path
from operator import itemgetter
from datetime import datetime

class OrderedFilesCommand(sublime_plugin.WindowCommand):
def run(self, index):
OF = OrderedFilesCommand
OF.file_views = ]
win = self.window
for vw in win.views():
head, tail = path.split(vw.file_name())
modified = path.getmtime(vw.file_name())
OF.file_views.append((tail, vw, modified))
if index == 0: # sort by file name (case-insensitive)
OF.file_views.sort(key = lambda (tail, _, Doh): tail.lower())
win.show_quick_panel([x for (x, y, z) in OF.file_views], self.on_chosen)
else: # sort by modified date (index == 2)
OF.file_views.sort(key = itemgetter(2))
win.show_quick_panel(
(datetime.fromtimestamp(z)).strftime("%d-%m-%y %H:%M ") + x
for (x, y, z) in OF.file_views], self.on_chosen)

def on_chosen(self, index):
	if index != -1:
		self.window.focus_view(OrderedFilesCommand.file_views[index][1])[/code]

No previous poster has come back about this, so I’ve no idea if anyone is finding it useful (I am :sunglasses: )

I was thinking it’s (probably) possible to expand it such that typing a single number will jump to that (numbered) file: ‘(1) somefile.ext’. But having more than 10 files open might make it a little cumbersome… But still, you could just type a second number :question:. A more complex solution would be to have a ‘key’ letter(s) highlighted (I believe? this is possible in the quick panel…) to cause the file to be focused.

But I won’t bother unless someone expresses an interest :smile:. Andy.

0 Likes

#19

How does one use this? Do you copy and save to a file call OrderedFiles.py? Does it have to go in any particular folder in the packages directory?

0 Likes

#20

Go to Preferences > Browse Packages. Then save it in the User folder.

0 Likes

#21

The file doesn’t have to be named ‘OrderedFiles.py’, although it seems sensible to do so.

0 Likes

#22

Great thanks. But how do I actually sort? I’ve tried adding the key bindings:

{ "keys": "ctrl+alt+x"], "command": "ordered_files", "args": { "index": 0 } }, { "keys": "ctrl+alt+c"], "command": "ordered_files", "args": { "index": 2 } }
But nothing happens.

0 Likes

#23

@skube. Every key-binding must be separated by a comma, except the last one.

{ "keys": "ctrl+alt+x"], "command": "ordered_files", "args": { "index": 0 } }, { "keys": "ctrl+alt+c"], "command": "ordered_files", "args": { "index": 2 } } << bet you need to add a comma!

Otherwise, press the key-combination and then Ctrl-apostrophe to open the Console and see if there is an error message.

0 Likes

#24

I managed to get this running. However, this opens a new list/dopdown with all the open files. Would it be possible to use this to re-order the “open files” list on the left?

0 Likes

#25

That is beyond the scope of my little utility. I think there may be a package/plug-in that does what you are after: check here. Maybe SidebarEnhancements(?) but I don’t really know.

I suppose you could use my SortTabs.py:

[code]import sublime_plugin
from os import path
from operator import itemgetter

A simple command to sort current tabs alphabetically (returning focus to the

original tab).

Does not work with different groups or windows. Not catered for unsaved views

(although it seems to work okay if there are any). It could be modified to

work in these circumstances.

{ “keys”: “ctrl+alt+b”], “command”: “sort_tabs” },

class SortTabsCommand(sublime_plugin.WindowCommand):
def run(self):
file_views = ]
win = self.window
curr_view = win.active_view()
for vw in win.views():
, tail = path.split(vw.file_name() or path.sep)
group, _ = win.get_view_index(vw)
file_views.append((tail.lower(), vw, group))
file_views.sort(key = itemgetter(2, 0))
moving_index = 0
for index, (
, vw, group) in enumerate(file_views):
if index == 0 or group > prev_group:
moving_index = 0
prev_group = group
else:
moving_index += 1
win.set_view_index(vw, group, moving_index)
win.focus_view(curr_view)[/code]
Sorting the tabs will also sort the list in the sidebar. It works within the current group - not across all groups.

# suggested key binding { "keys": "ctrl+alt+b"], "command": "sort_tabs" }

0 Likes

#26

thanks. that works as expected. Didn’t notice that it would also sort the list in the sidebar before.

Could this somehow be linked to the opening of a file, so that this plugin is automatically run when a file is opened or saved?

0 Likes

#27

https://github.com/bizoo/SortTabs
However automatic sorting is not bullet proof. There are some case when it’s not triggered (drag’n’drop of files for example).

0 Likes

#28

agibsonsw: For some reason using your script I get a non-C collation order (case sensitive sort)

This happens even if I set the collation explicity to C with locale.setLocale(“LC_COLLATE”, “C”) and use locale.strcoll as a sorting comparator. :frowning:

0 Likes

#29

@pez This is not something I know much about I’m afraid, although I’m guessing you need to check your character encoding as well(?). Hopefully someone else here may have experience on this topic. Here’s my original code:

[code]import sublime_plugin
from os import path
from operator import itemgetter
from datetime import datetime

Lists open files in a quick panel for jumping to,

ordered alphabetically or by modified date: index, 0, for alphabetical.

{ “keys”: “ctrl+alt+x”], “command”: “ordered_files”, “args”: { “index”: 0 } },

{ “keys”: “ctrl+alt+c”], “command”: “ordered_files”, “args”: { “index”: 2 } },

Does not work with different groups, windows, or unsaved views (although it could

be modified so that it does).

class OrderedFilesCommand(sublime_plugin.WindowCommand):
def run(self, index):
OF = OrderedFilesCommand
OF.file_views = ]
win = self.window
for vw in win.views():
if vw.file_name() is not None:
_, tail = path.split(vw.file_name())
modified = path.getmtime(vw.file_name())
OF.file_views.append((tail, vw, modified))
else:
pass # leave new/untitled files (for the moment)
if index == 0: # sort by file name (case-insensitive)
OF.file_views.sort(key = lambda (tail, _, Doh): tail.lower())
win.show_quick_panel([x for (x, y, z) in OF.file_views], self.on_chosen)
else: # sort by modified date (index == 2)
OF.file_views.sort(key = itemgetter(2))
win.show_quick_panel(
(datetime.fromtimestamp(z)).strftime("%d-%m-%y %H:%M ") + x
for (x, y, z) in OF.file_views], self.on_chosen)
def on_chosen(self, index):
if index != -1:
self.window.focus_view(OrderedFilesCommand.file_views[index][1])[/code]
My original code, though, includes tail.lower() within the sort-key, so this would need to be just tail.

0 Likes

#30

How can I make use of these Python scripts? Where (and with which name) should I store them for ST2 on a Linux machine?

0 Likes

#31

sublime.wbond.net/packages/SortTabs

0 Likes