Home Download Buy Blog Forum Support

Run plugin on keypress

Run plugin on keypress

Postby entozoon on Wed Dec 19, 2012 3:54 pm

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:

Code: Select all
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!
entozoon
 
Posts: 2
Joined: Wed Dec 19, 2012 3:47 pm

Re: Run plugin on keypress

Postby adzenith on Wed Dec 19, 2012 7:38 pm

Can't you just convert all spaces to tabs once, and then be done with it? Why are you doing it every save?
adzenith
 
Posts: 1217
Joined: Mon Oct 19, 2009 9:12 pm

Re: Run plugin on keypress

Postby iamntz on Wed Dec 19, 2012 10:15 pm

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

Code: Select all
{ "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...
iamntz
 
Posts: 915
Joined: Fri Apr 29, 2011 8:52 am
Location: Romania

Re: Run plugin on keypress

Postby entozoon on Thu Dec 20, 2012 10:10 am

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..
entozoon
 
Posts: 2
Joined: Wed Dec 19, 2012 3:47 pm


Return to Technical Support

Who is online

Users browsing this forum: Google [Bot], Yahoo [Bot] and 21 guests