I want to see the minimap only when a file has more than 100 lines, is possible to achieve this?
Toggle the minimap only with large files
This works I think:
import sublime_plugin
import sublime
class MinimapHider(sublime_plugin.ViewEventListener):
def on_modified(self):
region = sublime.Region(0, self.view.size())
count = len(self.view.split_by_newlines(region))
self.view.window().set_minimap_visible(count > 100)
on_new = on_activated = on_clone = on_modified
save as MinimapHider.py
in your User directory of your Packages folder.
Just as an aside to the above code, a potential enhancement to collecting the number of lines in the buffer is:
count = self.view.rowcol(self.view.size())[0] + 1
Instead of splitting the buffer into lines and then counting the result, this uses the rowcol
method on view to convert a point to a row and column offset. Passing in the size of the buffer gives you the row and column of the last character in the buffer.
The first element in the returned tuple is the row
, and we add 1
because the row count is 0 based and not 1 based.
Alright, let’s make some final tweaks. Having a ViewEventListener
is redundant, because we’re not storing data per view. The on_clone
and on_new
methods are also redundant because the on_activated
method is sufficient to check wether we should display the minimap upon switching or opening views. With @OdatNurd’s suggestion, the import sublime
line is also redundant. Final version:
import sublime_plugin
class MinimapHider(sublime_plugin.EventListener):
def on_modified(self, view):
count = view.rowcol(view.size())[0] + 1
view.window().set_minimap_visible(count > 100)
on_activated = on_modified
Huh, it looks like using ViewEventListener
keeps it enabled. I guess we are storing state per-view somehow. For the record:
import sublime_plugin
class MinimapHider(sublime_plugin.ViewEventListener):
def on_modified(self):
count = self.view.rowcol(self.view.size())[0] + 1
self.view.window().set_minimap_visible(count > 100)
on_activated = on_modified
I think this is because View
encompasses more than just the edit area for files; text inputs are also views, which includes the console input area and the quick panel/goto anything text entry field as well. Those all have less than 100 lines, so while they have the focus it’s as if a file with a single line is the current one.
Trivially, a change such as the following stops it from happening:
def on_modified(self, view):
if not view.settings().get("is_widget", False):
count = view.rowcol(view.size())[0] + 1
view.window().set_minimap_visible(count > 100)
However, I think using ViewEventListener
is a much cleaner solution. As mentioned in this forum post, the is_widget
setting is potentially a bit flakey: