Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AGx10k/f13eda5d59619c620325ffdf2b52b16f to your computer and use it in GitHub Desktop.
Save AGx10k/f13eda5d59619c620325ffdf2b52b16f to your computer and use it in GitHub Desktop.
Sublime Text on OS X, make Cmd+F do the equivalent of Cmd+E followed by Cmd+F. Works for all selections, not just single line like "find_selected_text" setting.
[
// ... (existing content)
// Find selected text, even if it spans multiple lines (unlike "find_selected_text").
{ "keys": ["super+f"], "command": "run_multiple_commands", "args":
{ "commands":
[
// Only execute slurp if there's selected text.
{"command": "slurp_find_string", "context": "window", "condition": "selected_text"},
{"command": "show_panel", "args": {"panel": "find", "reverse": false}, "context": "window"}
]
}
},
{ "keys": ["super+shift+f"], "command": "run_multiple_commands", "args":
{ "commands":
[
// Only execute slurp if there's selected text.
{"command": "slurp_find_string", "context": "window", "condition": "selected_text"},
{"command": "show_panel", "args": {"panel": "find_in_files"}, "context": "window"}
]
}
}
]
import sublime, sublime_plugin
# Takes an array of commands (same as those you'd provide to a key binding) with
# an optional condition, context (defaults to view commands) & runs each command in order.
# Valid condition is 'selected_text'.
# Valid contexts are 'text', 'window', and 'app' for running a TextCommand,
# WindowCommands, or ApplicationCommand respectively.
class RunMultipleCommandsCommand(sublime_plugin.TextCommand):
def exec_command(self, command):
if not 'command' in command:
raise Exception('No command name provided.')
# Check that the condition for this command is met.
if 'condition' in command:
condition = command['condition']
if condition == 'selected_text':
selected_text = False
for r in self.view.sel():
if not r.empty():
selected_text = True
if selected_text == False:
return
args = None
if 'args' in command:
args = command['args']
# default context is the view since it's easiest to get the other contexts
# from the view
context = self.view
if 'context' in command:
context_name = command['context']
if context_name == 'window':
context = context.window()
elif context_name == 'app':
context = sublime
elif context_name == 'text':
pass
else:
raise Exception('Invalid command context "'+context_name+'".')
# skip args if not needed
if args is None:
context.run_command(command['command'])
else:
context.run_command(command['command'], args)
def run(self, edit, commands = None):
if commands is None:
return # not an error
for command in commands:
self.exec_command(command)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment