Sublime Forum

Disable Sublime Minimap for a Specific Syntax

#1

Hello, is it possible to disable the minimap for specific syntax? I’m looking for a way to edit syntax-specific preferences, and add a property like "show_minimap": false. I would use this for .md, .txt files for which I never need to see the minimap.

I found this stack overflow question, which doesn’t seem to have a resolution:

Thanks for any help!

1 Like

#2

There does not seem to be a setting for the minimap state specifically. Additionally, the state of the minimap is stored per window and not per view (tab or file), so it’s not directly possible to have it turned off only for some files and not for others without doing some plugin magic.

I spent a few minutes and cobbled the below code together. Stash it in a python file in your Packages/User folder (use Preferences > Browse Packages if you don’t know where that is; Sublime Text > Preferences > Browse Packages if you’re on Mac OS), for example minimap_toggler.py or some such.

import sublime, sublime_plugin

# Sublime versions 3116 and higher have new functionality for determining
# if the minimap is visible or not without having to resort to some hackery.
ST3116 = int(sublime.version()) >= 3116

class MiniMapToggler(sublime_plugin.EventListener):
    # Invoked when a view is deactivated. This triggers prior to the newly
    # activated view becoming active.
    def on_deactivated(self, view):
        # Ignore deactivations of panels
        if view.settings ().get ("is_widget", False):
            return

        # If the option says we should hide the minimap and it's not currently
        # visible, then assume we hid it when we were activated and show it now.
        if (view.settings ().get ("hide_minimap", False) and
                self.is_minimap_visible (view) == False):
            view.window ().run_command ("toggle_minimap")

    # Every time a view is activated. This triggers after the previous
    # view has been deactivated
    def on_activated(self, view):
        # Ignore activations of panels
        if view.settings ().get ("is_widget", False):
            return

        # If the option says we should hide the minimap and it's currently
        # visible, then hide it.
        if (view.settings ().get ("hide_minimap", False) and
                self.is_minimap_visible (view)):
            view.window ().run_command ("toggle_minimap")

    # Check to see if the minimap is currently visible. This takes a view but
    # minimap visibility is a per-window setting, so the view is just used to
    # get at the window.
    #
    # This code is lifted from the DistractionFreeWindow plugin by aziz.
    #     https://github.com/aziz/DistractionFreeWindow/blob/master/distraction_free_window.py
    def is_minimap_visible(self, view):
      if ST3116:
          return view.window().is_minimap_visible()
      else:
          v = view.window().active_view()
          state1_w = v.viewport_extent()[0]
          v.window().run_command("toggle_minimap")
          state2_w = v.viewport_extent()[0]
          v.window().run_command("toggle_minimap")
          if state1_w and state2_w:
              return (state1_w < state2_w)

Once that’s done, open up a file that is using the syntax you want to turn the minimap off for (a markdown file, say) and use Preferences > Settings - More > Syntax Specific - User and add the following to it:

{
    "hide_minimap": "true"
}

What the plugin code does is catch the events for whenever a view is activated or deactivated, and then check to see if this option is set along with the current state of the minimap:

  • When a view is being activated AND we want to hide the minimap AND it is currently visible, then hide it
  • When a view is being deactivated AND we want to hide the minimap AND is is currently hidden, then assume that we’re the reason it is hidden and display it again

With this in place when you switch back and forth between files, the state of the minimap should toggle.

This code is fairly crude, so some caveats:

  1. while in a file where the minimap is hidden, opening something like the command palette or switching to the console view will temporarily turn the minimap back on; it will turn off again when you go back to the file
  2. if the current file has the minimap hidden due to this and you change it’s syntax (via menu or command palette), when you switch away it will not re-display the minimap again; you will need to turn it on manually
  3. probably other things that don’t occur to me at the moment

Also, this uses some code I lifted from the DistractionFreeWindow plugin by @aziz for determining the current state of the minimap in versions of sublime text that don’t have native support for that yet.

6 Likes

Hide minimap for certain filetypes?
Interface Suggestions
#3

Thanks so much @OdatNurd! Once again you make an awesome solution for my question. I need to get better at making these slick python scripts!

1 Like