Sublime Forum

Run plugin on keypress

#1

Hi, I’ve made a simple plugin that replaces all spaces with tabs whenever the file is saved, which is great… except for if you do ctrl-z for a while and save the file in order to test an older version of your file, because as it replaces the spaces it gets rid of all the undo history so you’re stuck with your old version >_<

I thought maybe instead it could be activated whenever the return key is pressed but couldn’t figure out how to use the on_query_context function. Here is my plugin code:

import sublime, sublime_plugin
class mykeIndent(sublime_plugin.EventListener):
	def on_pre_save(self, view):
		edit = view.begin_edit()
		view.run_command('unexpand_tabs', {"set_translate_tabs": True})
		view.end_edit(edit)

Thanks!

0 Likes

#2

Can’t you just convert all spaces to tabs once, and then be done with it? Why are you doing it every save?

0 Likes

#3

maybe you can bind to a key sequence. Something like:

{ "keys": "ctrl+s", "ctrl+s"], "command": "whatever" }

Although it’s a pretty bad idea to do this on every save. A quick fix to a file may mess with your version control…

0 Likes

#4

I would just do it once, but oftentimes I paste code in that’s filled with spaces and it goes by unnoticed.
I might use the keybinding like you suggested although … you’re right that it could mess with version control so I wish I could think of a better solution…

0 Likes

#5

Why not just do a search for any tabs in the view and if there are some tabs, then convert them to spaces…

I know this is old but … I think checking on every save makes complete sense, but just don’t do it unless there is something to do.

0 Likes