Sublime Forum

Keybindings with sequence and save

#1

Heya,

I tried to use this to strip whitespace and then save automatically when I press ctrl+s, but somehow as soon as I save the snippet it fails to save. It runs the whitespace cleanup alright though, but doesn’t seem to execute the save command. I tried just binding it to command=“sequence save”, this also fails to save the file. Is it expected ?

<binding key="ctrl+s" command="sequence 'removeWhitespace trailing' 'removeWhitespace extra' 'save'" />

Cheers

0 Likes

#2

sequence only works on commands that operate on the view, while save is a window command, so it doesn’t know what to do with it.

Your best bet is to write a quick python plugin to do this, something along the lines of:

import sublime, sublimeplugin

class StripAndSaveCommand(sublimeplugin.WindowCommand):
	def run(self, window, args):
		window.activeView().runCommand('sequence', 'removeWhitespace trailing', 'removeWhitespace extra'])
		window.runCommand('save')

and then

<binding key="ctrl+s" command="stripAndSave"/>
0 Likes

#3

bah, beaten to it :slight_smile:

0 Likes

#4

Alright thanks for the explanation, and thanks nonetheless jps for the example, my python is getting rusty and I’m just getting started with sublime so I much appreciate it.

0 Likes

#5

How about changing ‘sequence’ to do something like this with each command:

if view.canRunCommand(cmd):
    view.runCommand(cmd)
else:
    view.window().runCommand(cmd)
0 Likes