Sublime Forum

Change font size for an individual buffer/panel?

#1

I’ve looked for a way to change the font size individually for each panel/buffer but haven’t found anything, so I’m assuming it’s not a feature.

But would it be possible? What I’m looking for specifically is the ability to use a hotkey similar to ctrl+(-/+) to change the font size, but only in the active panel. On the particular file would be workable too, but maybe not as useful. I think it would be a great feature because I might be keeping multiple files open in one panel for reference or reminders, and I don’t need it occupying the same screen real estate and portion of my attention as the file I’m really editing.

Is this possible to do? Does anyone else think this would be useful?

0 Likes

#2

It’s easily possible to change the font size of an individual view (file) to be different than what would be used by default (see plugin below). Assigning a custom font size to a particular panel(view group) isn’t as easy to pull off, though.

In particular a plugin like the below could use view.window() to get the window that it’s contained in, then use window.active_group() to get the group (i.e. “panel”) that the view is contained in and apply the appropriate settings change to all of the views reported by window.views_in_group() so that all of the files in that section of the window would change at once.

However as evidenced below the font size is really a view by view setting, so moving a tab to that group won’t apply the group font size to it, nor will the font size be changed back to the default if you move a tab out.

As far as I’m aware the only way to reliably detect when a file tab is moved between groups would be to continuously check with the above API calls and see when a view is in a different group than it was in last time, which is probably not that great for performance. There may be a better way to do that, though.

Of course if that doesn’t bother you and you don’t mind manually managing the font when the files in the group are changing, that may not matter.

In any case, the following is a plugin that implements view specific versions of the standard commands for changing the font size, which affect things globally. In fact this is just a modified version of the Default/font.py plugin that ships with Sublime by default.

To use it you would select Tools > Developer > New Plugin... from the menu, then replace the stub code with the code here and save it in the location that Sublime defaults to as a python file (I called mine view_font.py):

import sublime
import sublime_plugin


class IncreaseViewFontSizeCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        s = self.view.settings()
        current = s.get("font_size", 10)

        if current >= 36:
            current += 4
        elif current >= 24:
            current += 2
        else:
            current += 1

        if current > 128:
            current = 128
        s.set("font_size", current)



class DecreaseViewFontSizeCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        s = self.view.settings()
        current = s.get("font_size", 10)
        # current -= 1

        if current >= 40:
            current -= 4
        elif current >= 26:
            current -= 2
        else:
            current -= 1

        if current < 8:
            current = 8
        s.set("font_size", current)




class ResetViewFontSizeCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        s = self.view.settings()
        s.erase("font_size")

With that in place, you can create your own key bindings that use these commands. For example:

    { "keys": ["shift+ctrl++"], "command": "increase_view_font_size" },
    { "keys": ["shift+ctrl+="], "command": "increase_view_font_size" },
    { "keys": ["shift+ctrl+-"], "command": "decrease_view_font_size" },

This is just the default key bindings with a Shift added, but you could use whatever you want. You can also bind something to the reset_view_font_size command to set it back to the original font, or add it to the view context menu, etc.

3 Likes

Reset_font_size removing font_size from settings
#3

Thanks for the great response! You explained it so well, it makes writing plugins look easy. I’ll give this a try, and it’s probably all that I need.

2 Likes

#4

You can also use the package QuickSettings:

  1. https://github.com/evandrocoan/SublimeQuickSettings

To change the current view font size. And if you want to, you can use the package BufferScroll:

  1. https://github.com/evandrocoan/BufferScroll

To remember the view settings. So, after open the same file next time, it will remember the settings you have set.

0 Likes