Sublime Text 3:将字体大小重置为自定义字体大小? [英] Sublime Text 3: Reset font size to custom font size?

查看:161
本文介绍了Sublime Text 3:将字体大小重置为自定义字体大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的按键绑定中添加以下内容,可以将Sublime Text 3中的字体大小重置为默认大小:

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" }

但这也会重置我在用户首选项中设置的所有字体大小.例如,默认字体大小为10.当我在用户首选项中添加以下内容时...

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

...然后用ctrl+0重设我的字体大小,此设置消失,字体大小恢复为默认值10.如何防止这种情况发生?

...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?

推荐答案

命令increase_font_sizedecrease_font_sizereset_font_sizefont.py插件的Default程序包中定义,它们可以直接工作修改Preferences.sublime-settingsfont_size设置的值.

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.

尤其是,increase_font_sizedecrease_font_size都修改了设置的当前值,而您已经注意到reset_font_size命令将其完全删除,以便将其恢复为默认值.

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.

乍一看,您可以尝试使用set_setting命令而不是reset_font_size,这将使您可以将font_size重置为想要的默认值.

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.

但是,这将无法正常工作,因为set_setting仅修改当前焦点文件view的设置,而Preferences.sublime-settings文件指定全局设置.因此,您会注意到,上下更改字体大小将在所有位置生效,但是重置字体大小仅在当前视图中有效.

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.

要解决此问题,您需要通过更改font_size设置的Preferences.sublime-settings版本来将其重置为所选的默认字体大小.

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.

例如以下插件,您可以通过从菜单中选择Tools > Developer > New Plugin...,然后将存根代码替换为下面的代码,并将其另存为python文件,并将其保存为Sublime默认设置的位置,来使用该插件(例如set_default_font_size.py,但只有扩展名很重要):

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")

完成后,您要编辑默认首选项并添加一个default_font_size设置,该设置指定要用作默认字体大小的字体设置,然后修改上方的键绑定以调用set_default_font_size而不是reset_font_size.

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.

另一种可能的解决方案是使用以下插件,该插件通常提供上面引用的set_setting命令版本,该命令会将设置设置为全局首选项,而不仅仅是在当前视图中更改设置:

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.

这篇关于Sublime Text 3:将字体大小重置为自定义字体大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆