Skip to content

Instantly share code, notes, and snippets.

@torrentails
Last active October 28, 2025 04:02
Show Gist options
  • Select an option

  • Save torrentails/e8b6e3bcfc20fc3ffe013a079e834c4b to your computer and use it in GitHub Desktop.

Select an option

Save torrentails/e8b6e3bcfc20fc3ffe013a079e834c4b to your computer and use it in GitHub Desktop.
Sublime text 4 cycle setting extension
import os
import sublime
from sublime_plugin import ApplicationCommand
# Copyright (c) 2025 Alex Sullivan released under MIT License
# This is an adaption of the 'toggle_setting_ext' plugin snippet at:
# https://github.com/STealthy-and-haSTy/SublimeScraps/blob/fc2eaff26f45b7aa94fe3cadc8cbd3e15b1361d6/plugins/toggle_setting_ext.py
#
# Notable changes are that this version is named 'cycle_setting', and that the
# setting cycled now persists across windows, views, and reloads. Most of the
# rest is copied verbatim from the above link. Much thanks to STealthy-and-haSTy!
#
# UPDATE v1.0.1
# - Added a 'temoprary' optional arg that defaults to False. If set to True, the
# settings file won't be updated, and so the change will only persist as long as
# you have at least one Sublime window open, but will reset on reload.
# - Added a licence file and tagline.
# --- Original readme ---
# Copyright (c) 2017 Terence Martin and Keith Hall released under MIT License
#
# Related reading:
# https://stackoverflow.com/questions/46167234/sublimetext-3-dynamically-enable-disable-invisible-white-space-option/
#
# This implements an extended version of the internal toggle_setting command.
# Where that command can toggle a boolean setting between True and False, this
# version can toggle any setting between any values, even two or more.
#
# This was originally written as an example for someone that wanted to toggle
# the display of visible white space on and off. The version here is extended
# and made more generic by allowing not only the list of options to toggle
# between, but also the setting to use.
#
# The command takes an argument that specifies the setting to toggle as well as
# a list of the values to toggle between, which can be two or more values that
# are valid for the setting in question.
#
# When the setting is not already set for the current view, or the current
# setting is not in the list of options given, the first item in the list is
# used as the setting. Otherwise, the value of the setting is set to the next
# item in the list.
# Examples:
#
# // Toggle between all white space display options
# {
# "keys": ["super+s"], "command": "cycle_setting",
# "args": {
# "setting": "draw_white_space",
# "options": ["all", "selection", "none"]
# }
# },
#
#
# // Swap between two sets of rulers for the current view
# {
# "keys": ["super+s"], "command": "cycle_setting",
# "args": {
# "setting": "rulers",
# "options": [[80, 110], [40, 55]],
# "temporary": True
# }
# },
#
# // Standard: Toggle word wrap on or off
# {
# "keys": ["f5"], "command": "cycle_setting",
# "args": {
# "setting": "word_wrap",
# "options": [True, False],
# "temporary": True
# }
# },
#
# // Swap between always and never in open_files_in_new_window
# // The flowing keybind only works with 'cycle_setting'
# {
# "keys": ["f8"], "command": "cycle_setting",
# "args": {
# "setting": "open_files_in_new_window",
# "options": ["always", "never"]
# }
# }
class CycleSetting(ApplicationCommand):
"""
An extended version of the toggle_setting internal command. Along with a
setting, provide a list of options to cycle between, which can contain
more than two items if the setting can have more than two values.
(e.g. the draw_white_space option)
Most of the code is copied verbatim from the original git linked above,
with tweaks and changes where necessary. The settings cycled will now
persist across windows, views, and reloads.
Was renamed 'cycle_setting' so that it's named synonymously with its
function, and to keep it distinct from 'toggle_setting_ext', just in case.
"""
def run(self, setting, options, temporary=False):
s = sublime.load_settings('Preferences.sublime-settings')
try:
current = s.get(setting, None)
index = -1 if current is None else options.index(current)
except:
print(f"{self.name()}: {setting} has no defined value, defaulting to \"{options[0]}\"")
s.set(setting, options[0])
else:
index = (index + 1) % len(options)
print(f"{self.name()}: setting {setting} to \"{options[index]}\"")
s.set(setting, options[index])
if temporary is not True:
sublime.save_settings('Preferences.sublime-settings')
MIT License
Copyright (c) 2025 Alex Sullivan
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment