Adding the following to my key bindings allows me to reset the font size in Sublime Text 3 to the default size:
{ "keys": ["ctrl+0"], "command": "reset_font_size" }
But this also resets any font size I had set in my user preferences. For example, the default font size is 10. When I add the following to my user preferences...
"font_size": 8
...and then reset my font size with ctrl+0
, this setting disappears and the font size returns to the default of 10. How can I prevent this behavior?
The commands increase_font_size
, decrease_font_size
and reset_font_size
are defined in the Default
package in the font.py
plugin, and they work by directly modifying the value of the font_size
setting in Preferences.sublime-settings
.
In particular, increase_font_size
and decrease_font_size
both modify the current value of the setting, while as you've noticed the reset_font_size
command deletes it entirely in order to return things to the default.
At first blush you might try using the set_setting
command instead of reset_font_size
, which would allow you to reset the font_size
back to the value that want to be the default.
However, that won't work as you might expect because set_setting
modifies the settings only for the currently focused file view
, while the Preferences.sublime-settings
file specifies the global settings. As such you would notice that changing the font size up and down would take effect everywhere, but reseting it would only work in the current view.
To get around that, you need to handle resetting back to the chosen default font size by altering the Preferences.sublime-settings
version of the font_size
setting.
An example of that would be the following plugin, which you can use by selecting Tools > Developer > New Plugin...
from the menu, then replacing the stub code with the code below and saving it as a python file in the location that Sublime will default to (e.g. set_default_font_size.py
, but only the extension is important):
import sublime
import sublime_plugin
class SetDefaultFontSizeCommand(sublime_plugin.ApplicationCommand):
def run(self):
s = sublime.load_settings("Preferences.sublime-settings")
new_size = s.get("default_font_size", 10)
s.set("font_size", new_size)
sublime.save_settings("Preferences.sublime-settings")
Once that's done, you want to edit your default preferences and add a default_font_size
setting that specifies the font setting that you want to be your default font size, then modify your key binding above to invoke set_default_font_size
instead of reset_font_size
.
This plugin simply extracts the default font size that you've specified and updates the preferences with it, which will put it back to the value that you want it to be, making the font size change everywhere all at once.
[edit]
Another potential solution to this would be the following plugin, which more generally provides a version of the set_setting
command referenced above that will set the setting into your global preferences instead of just changing the settings in the current view:
import sublime
import sublime_plugin
class GlobalSetSettingCommand(sublime_plugin.ApplicationCommand):
def run(self, setting, value):
s = sublime.load_settings("Preferences.sublime-settings")
s.set(setting, value)
sublime.save_settings("Preferences.sublime-settings")
To use this, you would need a key binding like the following (with your font size changed as appropriate):
{
"keys": ["ctrl+0"], "command": "global_set_setting",
"args": {
"setting": "font_size",
"value": 12
}
},
The benefit here is that it doesn't require you to add in an extra setting to provide the default font size. Additionally you could also use this command in other cases as well, such as a set of context menu items or key bindings that let you select between several preset sizes, for example.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With