Tags

Writing in Sublime Text

If, like me, you use Sublime Text to draft blog posts or simply to write, you might find yourself wishing for an easy way to switch between a larger font size for writing and a more normal size for coding and everything else. In this post, I will guide you through creating a custom keybinding in Sublime Text that will make toggling font sizes a breeze, ensuring your writing & coding adventures remain comfortable & productive.

Part 1: Create a Custom Command..

First, we need to create a custom command that will toggle the font size in Sublime Text. To do this, we’ll create a new plugin.

Creating the Plugin

Step 1. Open Sublime Text
Step 2. Navigate to Tools > Developer > New Plugin...
Step 3. Replace all of the default code with the following:

import sublime
import sublime_plugin

class ToggleFontSizeCommand(sublime_plugin.ApplicationCommand):
    def run(self):
        settings = sublime.load_settings("Preferences.sublime-settings")
        current_font_size = settings.get("font_size", 10)
        if current_font_size == 10:  # Normal size
            settings.set("font_size", 14)  # Larger size for writing
        else:
            settings.set("font_size", 10)  # Normal size
        sublime.save_settings("Preferences.sublime-settings")

Step 4. Save this file in the Packages/User directory with the name writing.py

This plugin defines a command called toggle_font_size that toggles the font size between 10 (normal) and 14 (larger). You can adjust these values to whatever you like.

Part 2: Add the Keybinding..

Next, we need to bind this new command to a key combination.

Adding the Keybinding

  1. Open your keymap file by navigating to Preferences > Key Bindings.
  2. Add the following entry to your existing keybindings:
[
    {
        "keys": ["alt+w"],
        "command": "toggle_font_size"
    }
]

This keybinding uses ALT+W to toggle the font size. You can replace this with any other key combination that suits you.

The Ending..

You’ve now successfully crafted your very own Sublime Text plugin for toggling font sizes and have assigned a handy keybinding to control it. From here on, simply pressing ALT+W will effortlessly toggle your font size between 10 and 14, or any other sizes you’ve specified in the plugin’s code. Customizing your Sublime Text environment can greatly enhance your writing and coding journey. Happy Writing :)