Sublime Forum

Scroll to the top and bottom of all opened docs

#1

I’m trying to write a .py file that will automatically scroll all open docs to the bottom or top with the code below.

import sublime, sublime_plugin

class ScrollAllOpenFiles(sublime_plugin.TextCommand):
  def run(self, edit, **kwargs):
    window = sublime.active_window()
    views = window.views()
    for view in views:
      view.run_command("move_to", kwargs)

Along with this in the .sublime-menu file.

[
  { "caption": "-" },
  { "caption": "Scroll All Open Files to the Top", "command": "scroll_all_open_files", "args": {"to": "bof" }, "mnemonic": "T" },
  { "caption": "Scroll All Open Files to the Bottom", "command": "scroll_all_open_files", "args": {"to": "eof" }, "mnemonic": "B" },
  { "caption": "-" }
]

It doesn’t work when I have around 100 or so files open, is there any way to fix this? Thanks.

0 Likes

#2

Quick bump to see if anyone has any ideas.

I still haven’t found a way to resolve it.

It works when I quickly hold Shift+Tab to tab through all the open docs. Then if I run this it’ll scroll to the top or the bottom.

Thanks in advance.

0 Likes

#3

if you do some print logging and check the console when you have lots of tabs open, what do you see? the command is run for each view but the view isn’t scrolled?

0 Likes

#4

I did tweak the code from the original post but it still doesn’t work until I Shift+Tab through them first.

Here’s my code that I ran with print logging.

import sublime, sublime_plugin

from datetime import datetime

class ScrollAllOpenFiles(sublime_plugin.TextCommand):
  def run(self, view, **kwargs):
    for view in self.view.window().views():
      view.run_command("move_to", kwargs)
      view.run_command("move_to", {"extend": "false", "to": "bol"})
      current_time = datetime.now().time()
      print("View object: %s @ %s" % (view, current_time))

I have 131 docs open and it prints each out but it doesn’t scroll.

It’s looping through them so it must be an issue with the move_to part of the code.

View object: <sublime.View object at 0x000001CC8046B940> @ 14:03:43.274541
View object: <sublime.View object at 0x000001CC8046B5F8> @ 14:03:43.281554
View object: <sublime.View object at 0x000001CC8046B780> @ 14:03:43.287553
View object: <sublime.View object at 0x000001CC8046B2E8> @ 14:03:43.293542
View object: <sublime.View object at 0x000001CC8046B550> @ 14:03:43.300553
View object: <sublime.View object at 0x000001CC8046B7F0> @ 14:03:43.306553
View object: <sublime.View object at 0x000001CC8046BF60> @ 14:03:43.312553
View object: <sublime.View object at 0x000001CC8046B278> @ 14:03:43.319553
View object: <sublime.View object at 0x000001CC8046B7B8> @ 14:03:43.325553
View object: <sublime.View object at 0x000001CC8046B9E8> @ 14:03:43.331557
View object: <sublime.View object at 0x000001CC8046BA20> @ 14:03:43.336553
View object: <sublime.View object at 0x000001CC8046BF98> @ 14:03:43.342553
View object: <sublime.View object at 0x000001CC8046B908> @ 14:03:43.348553
View object: <sublime.View object at 0x000001CC8046BD68> @ 14:03:43.353553
View object: <sublime.View object at 0x000001CCFF26B7B8> @ 14:03:43.358742
View object: <sublime.View object at 0x000001CCFF26B6A0> @ 14:03:43.365554
View object: <sublime.View object at 0x000001CCFF26B240> @ 14:03:43.370741
View object: <sublime.View object at 0x000001CCFF26BDA0> @ 14:03:43.376542
View object: <sublime.View object at 0x000001CCFF26BC50> @ 14:03:43.381554
View object: <sublime.View object at 0x000001CCFF26BE80> @ 14:03:43.387553
View object: <sublime.View object at 0x000001CCFF26B0B8> @ 14:03:43.392554
View object: <sublime.View object at 0x000001CCFF26BC88> @ 14:03:43.398553
View object: <sublime.View object at 0x000001CCFF26BA20> @ 14:03:43.403553
View object: <sublime.View object at 0x000001CCFF26BD68> @ 14:03:43.409553
View object: <sublime.View object at 0x000001CCFF26B080> @ 14:03:43.414557
View object: <sublime.View object at 0x000001CCFF26B9B0> @ 14:03:43.420554
View object: <sublime.View object at 0x000001CCFF26B1D0> @ 14:03:43.426543
View object: <sublime.View object at 0x000001CCFF26B710> @ 14:03:43.432553
View object: <sublime.View object at 0x000001CCFF26B518> @ 14:03:43.437553
View object: <sublime.View object at 0x000001CCFF26B4A8> @ 14:03:43.443542
View object: <sublime.View object at 0x000001CCFF26B978> @ 14:03:43.449553
View object: <sublime.View object at 0x000001CCFF26BE48> @ 14:03:43.454541
View object: <sublime.View object at 0x000001CCFF26B278> @ 14:03:43.460543
View object: <sublime.View object at 0x000001CCFF26B6D8> @ 14:03:43.467557
View object: <sublime.View object at 0x000001CCFF26BEF0> @ 14:03:43.472553
View object: <sublime.View object at 0x000001CCFF26B2B0> @ 14:03:43.478557
View object: <sublime.View object at 0x000001CCFF26B630> @ 14:03:43.484553
View object: <sublime.View object at 0x000001CCFF26B860> @ 14:03:43.489557
View object: <sublime.View object at 0x000001CCFF26B2E8> @ 14:03:43.495553
View object: <sublime.View object at 0x000001CCFF26BB38> @ 14:03:43.501553
View object: <sublime.View object at 0x000001CCFF26B470> @ 14:03:43.506553
View object: <sublime.View object at 0x000001CCFF26B5F8> @ 14:03:43.512557
View object: <sublime.View object at 0x000001CCFF26BDD8> @ 14:03:43.518542
View object: <sublime.View object at 0x000001CCFF26BB70> @ 14:03:43.523553
View object: <sublime.View object at 0x000001CCFF26B438> @ 14:03:43.529553
View object: <sublime.View object at 0x000001CCFF26BBE0> @ 14:03:43.535557
View object: <sublime.View object at 0x000001CCFF26B940> @ 14:03:43.540553
View object: <sublime.View object at 0x000001CCFF26B4E0> @ 14:03:43.546710
View object: <sublime.View object at 0x000001CCFF266048> @ 14:03:43.552711
View object: <sublime.View object at 0x000001CCFF266EF0> @ 14:03:43.557557
View object: <sublime.View object at 0x000001CCFF2663C8> @ 14:03:43.563554
View object: <sublime.View object at 0x000001CCFF266630> @ 14:03:43.569553
View object: <sublime.View object at 0x000001CCFF2665F8> @ 14:03:43.575553
View object: <sublime.View object at 0x000001CCFF266C88> @ 14:03:43.581554
View object: <sublime.View object at 0x000001CCFF266748> @ 14:03:43.586542
View object: <sublime.View object at 0x000001CCFF2664A8> @ 14:03:43.592553
View object: <sublime.View object at 0x000001CCFF266940> @ 14:03:43.598553
View object: <sublime.View object at 0x000001CCFF266908> @ 14:03:43.603553
View object: <sublime.View object at 0x000001CCFF266BE0> @ 14:03:43.609554
View object: <sublime.View object at 0x000001CCFF266BA8> @ 14:03:43.615553
View object: <sublime.View object at 0x000001CCFF266588> @ 14:03:43.621553
View object: <sublime.View object at 0x000001CCFF266978> @ 14:03:43.627553
View object: <sublime.View object at 0x000001CCFF1EDFD0> @ 14:03:43.632554
View object: <sublime.View object at 0x000001CCFF1EDCC0> @ 14:03:43.638554
View object: <sublime.View object at 0x000001CCFF1EDE80> @ 14:03:43.643542
View object: <sublime.View object at 0x000001CCFF1ED668> @ 14:03:43.649715
View object: <sublime.View object at 0x000001CCFF1EDA90> @ 14:03:43.655554
View object: <sublime.View object at 0x000001CCFF1ED400> @ 14:03:43.661554
View object: <sublime.View object at 0x000001CCFF1ED358> @ 14:03:43.666553
View object: <sublime.View object at 0x000001CCFF1ED898> @ 14:03:43.672553
View object: <sublime.View object at 0x000001CCFF1EDAC8> @ 14:03:43.678554
View object: <sublime.View object at 0x000001CCFF1ED5C0> @ 14:03:43.684554
View object: <sublime.View object at 0x000001CCFF1ED780> @ 14:03:43.690554
View object: <sublime.View object at 0x000001CCFF1ED208> @ 14:03:43.699554
View object: <sublime.View object at 0x000001CCFF1EDDA0> @ 14:03:43.708554
View object: <sublime.View object at 0x000001CCFF1ED6A0> @ 14:03:43.715553
View object: <sublime.View object at 0x000001CCFF1ED550> @ 14:03:43.722554
View object: <sublime.View object at 0x000001CCFF1ED160> @ 14:03:43.729554
View object: <sublime.View object at 0x000001CCFF1ED438> @ 14:03:43.736558
View object: <sublime.View object at 0x000001CCFF1ED748> @ 14:03:43.742722
View object: <sublime.View object at 0x000001CCFF1EDBA8> @ 14:03:43.748739
View object: <sublime.View object at 0x000001CCFF1ED588> @ 14:03:43.754554
View object: <sublime.View object at 0x000001CCFF1ED710> @ 14:03:43.761554
View object: <sublime.View object at 0x000001CCFF1ED1D0> @ 14:03:43.767554
View object: <sublime.View object at 0x000001CCFF1EDDD8> @ 14:03:43.773554
View object: <sublime.View object at 0x000001CCFF1ED198> @ 14:03:43.780554
View object: <sublime.View object at 0x000001CCFF1EDB70> @ 14:03:43.786554
View object: <sublime.View object at 0x000001CCFF1ED978> @ 14:03:43.793542
View object: <sublime.View object at 0x000001CCFF1EDD30> @ 14:03:43.800554
View object: <sublime.View object at 0x000001CCFF1ED128> @ 14:03:43.807554
View object: <sublime.View object at 0x000001CCFF1EDEB8> @ 14:03:43.813554
View object: <sublime.View object at 0x000001CCFF1EDC18> @ 14:03:43.820554
View object: <sublime.View object at 0x000001CCFF1EDEF0> @ 14:03:43.826542
View object: <sublime.View object at 0x000001CCFF1ED4A8> @ 14:03:43.832554
View object: <sublime.View object at 0x000001CCFF1EDA58> @ 14:03:43.837724
View object: <sublime.View object at 0x000001CCFF1ED3C8> @ 14:03:43.843542
View object: <sublime.View object at 0x000001CCFF1ED470> @ 14:03:43.849554
View object: <sublime.View object at 0x000001CCFF1DD400> @ 14:03:43.854553
View object: <sublime.View object at 0x000001CCFF1DD0F0> @ 14:03:43.860554
View object: <sublime.View object at 0x000001CCFF1DD128> @ 14:03:43.866553
View object: <sublime.View object at 0x000001CCFF1DD080> @ 14:03:43.871543
View object: <sublime.View object at 0x000001CCFF1DD0B8> @ 14:03:43.877557
View object: <sublime.View object at 0x000001CCFF1DDCC0> @ 14:03:43.883554
View object: <sublime.View object at 0x000001CCFF1DDF60> @ 14:03:43.889553
View object: <sublime.View object at 0x000001CCFF1DD748> @ 14:03:43.894554
View object: <sublime.View object at 0x000001CCFF1DDA90> @ 14:03:43.900716
View object: <sublime.View object at 0x000001CCFF1DD240> @ 14:03:43.906553
View object: <sublime.View object at 0x000001CCFF1DD470> @ 14:03:43.912553
View object: <sublime.View object at 0x000001CCFF1DD5C0> @ 14:03:43.918554
View object: <sublime.View object at 0x000001CCFF1DD710> @ 14:03:43.924554
View object: <sublime.View object at 0x000001CCFF1DD630> @ 14:03:43.930542
View object: <sublime.View object at 0x000001CCFF1DD4E0> @ 14:03:43.936542
View object: <sublime.View object at 0x000001CCFF1DD8D0> @ 14:03:43.942554
View object: <sublime.View object at 0x000001CCFF1DD978> @ 14:03:43.948554
View object: <sublime.View object at 0x000001CCFF1DD358> @ 14:03:43.953554
View object: <sublime.View object at 0x000001CCFF1DDF98> @ 14:03:43.959554
View object: <sublime.View object at 0x000001CCFF1DDDA0> @ 14:03:43.965554
View object: <sublime.View object at 0x000001CCFF1DDEB8> @ 14:03:43.971554
View object: <sublime.View object at 0x000001CCFF1DD4A8> @ 14:03:43.977554
View object: <sublime.View object at 0x000001CCFF1DD828> @ 14:03:43.983554
View object: <sublime.View object at 0x000001CCFF1DD6A0> @ 14:03:43.989554
View object: <sublime.View object at 0x000001CCFF1DD7F0> @ 14:03:43.995554
View object: <sublime.View object at 0x000001CCFF1DD390> @ 14:03:44.001554
View object: <sublime.View object at 0x000001CCFF1DD2B0> @ 14:03:44.006554
View object: <sublime.View object at 0x000001CCFF1DDAC8> @ 14:03:44.012554
View object: <sublime.View object at 0x000001CCFF1DDB70> @ 14:03:44.018554
View object: <sublime.View object at 0x000001CCFF1DDF28> @ 14:03:44.024554
View object: <sublime.View object at 0x000001CCFF1DDBE0> @ 14:03:44.030557
View object: <sublime.View object at 0x000001CCFF1DD588> @ 14:03:44.035646
View object: <sublime.View object at 0x000001CCFF1DDBA8> @ 14:03:44.041558
View object: <sublime.View object at 0x000001CCFF1DDDD8> @ 14:03:44.047554
View object: <sublime.View object at 0x000001CCFF1DDE10> @ 14:03:44.048554
0 Likes

#5

I guess as you are manipulating multiple views, you should be using a window command, which may help - not sure, but worth trying

0 Likes

#6

Not sure I follow. I cannot see any info on the window command.

I’ve tried this but still the same view.run_command('goto_line', {"line": 10000000})

import sublime, sublime_plugin
class ScrollAllOpenFiles(sublime_plugin.TextCommand):
  def run(self, view):
    for view in self.view.window().views():
      view.run_command('goto_line', {"line": 0})
0 Likes

#7

Just as a note, the second argument to the run method of a TextCommand is actually an Edit object; normally you would set that argument to be named edit and not view as you’ve done here because that gives an incorrect picture of what the argument actually represents. In a TextCommand, there’s one instance created for every view, and self.view represents the actual view that the command is running for.

The change that @kingkeith mentioned above relates to the fact that a TextCommand is for modifying the contents/visual state of a specific file (i.e. the one the that command is executed for). Normally one would use a WindowCommand for an operation that is going to do something that’s not related to a specific file, which is what you’re doing here.

The change to make would be to use sublime_plugin.WindowCommand as the base class. A WindowCommand has one instance created for each window (instead of for each file like TextCommand. For that reason it has a self.window instead of a self.view. It also can’t make any edits to files, so it does not get an edit argument passed to it’s run method.

That would make your second example look something like this:

import sublime, sublime_plugin
class ScrollAllOpenFiles(sublime_plugin.WindowCommand):
  def run(self):
    for view in self.window.views():
      view.run_command('goto_line', {"line": 0})
1 Like

#8

Thanks. @OdatNurd. This has the same issue with a large number of documents open. Even with only a few also.

It’s only when I Shift+Tab through the files and run this it’ll work.

I’ve tested this in a fresh build too and it has the same issue.

This basically what I was after only to try fix the issue:

import sublime, sublime_plugin

class ScrollAllOpenFiles(sublime_plugin.WindowCommand):
    def run(self, **kwargs):
        for view in self.window.views():
            view.run_command("move_to", kwargs)
            view.run_command("move_to", {"extend": "false", "to": "bol"})
            view.run_command("show_at_center")

With my Tab Context.sublime-menu file like so:

[
  { "caption": "Scroll to the Top\t- [ All Open Files ]", "command": "scroll_all_open_files", "args": {"extend": false, "to": "bof"}, "mnemonic": "T" },
  { "caption": "Scroll to the Bottom\t- [ All Open Files ]", "command": "scroll_all_open_files", "args": {"extend": false, "to": "eof"}, "mnemonic": "B" }
]
0 Likes

#9

If it works for a smaller number of files but not for a lot of them, I think I would consider that a bug; what works for n files should work for n+1 too.

That said, items in the Tab context menu may not always do what you want because they get special arguments to tell them what file is being targeted (since it can be different from the currently active file).

Do you get the same resulting if you bind the command to a key as well?

0 Likes

#10

Hi, the same issue remains if I map it to a keyboard shortcut.

Is there anything wrong with the .py code in this case?

0 Likes

#11

Nothing obvious that I can see. I didn’t try it myself, but if it works for a small number of files and not a larger number, that’s suspect because the number of files shouldn’t matter.

0 Likes

#12

Thanks. I’ve created a bug report here.

0 Likes

#13

Are you opening files as well? I can imagine a view for which is_loading() returns True would fail to execute the command.

0 Likes

#14

No, just running the python code when the files are open.

0 Likes