Sublime Text 2 tip: Reset font size

2 minute read

Sublime Text 2 is an extremely powerful text editor that is particularly well suited for programming and editing markup. It has in a very short time become my favourite programming tool. It comes with built-in support of a great number of programming languages and file formats, and an even bigger number of community supplied plugins and extensions. You can extend the functionality of Sublime Text 2 in many ways just by adding packages. In this and later posts I am going to share some of my favourite ways to improve the functionality of Sublime Text 2. It will not be just about listing my favourite packages (although that may come in a later post), but how you can make small, but useful improvements in other ways. This post contains the first tip.

Reset font size (that doesn’t remove your custom font size setting)

You might know that you can use Ctrl + and Ctrl - (or Cmd + and Cmd - if on OSX) to temporarily increase or decrease the font size, similar to the way you can zoom in and out in a web browser. This is a very useful feature if you use Sublime Text in a presentation. It is also quite common that you can use Ctrl 0 to reset the zoom back to default, but that doesn’t work in Sublime Text. By default Ctrl 0 doesn’t bind to anything. There is a menu item Reset under Preferences->Font, but that doesn’t work quite like you would expect. Sublime Texts default font size is 10 pt. If you have defined a custom font size to something else, Reset doesn’t revert to your custom font size, but to the default 10 pt. And what is worse, it actually removes the font_size setting from your user settings file.

The following solution was found on the Sublime Text forum and works great. This lets you set a default font size to anything you want, and assigns the key binding Ctrl 0 to the reset function.

Add your default font size to your user settings like this

"default_font_size": 10

Store the following command in a file in you User package folder.

import sublime
import sublime_plugin
class ResetFontSizeToUserDefaultsCommand(sublime_plugin.ApplicationCommand):
    def run(self):
        s = sublime.load_settings("Preferences.sublime-settings")
        if s.has('default_font_size'):
            s.set('font_size', s.get('default_font_size'))

And finally add the key binding to the command by putting the following in your user key bindings file

{ "keys": ["ctrl+0"], "command": "reset_font_size_to_user_defaults" }