Sublime Forum

Auto Save on Every Keypress

#1

Does anyone know how to make SublimeText2 save on every keypress? I use LiveReload for web dev and would love to have it refresh without having to hit cmd+s.

0 Likes

#2

Wouldn’t that be really annoying?

What happens when your in the middle of writing an HTML tag, or haven’t closed a CSS bracket…you would get some bad results.

Cmd-S is really easy to press. :smile:

1 Like

#3

I don’t think it would be annoying. Maybe it could be configured to wait x milliseconds for another key before triggering the save. So that it saves on idle like WebStorm does, but without the minimum time limit of 1 second like it has.

0 Likes

#4

Something like this might work for you. Save it into a file (e.g. AutoSave.py) and drop it into /Sublime Text 2/Packages/User folder.

import sublime, sublime_plugin

class AutoSaveCommand(sublime_plugin.EventListener):
    def on_modified(self, view):
        view.run_command('save')

NOTE: it will save on every modification. To add the ‘wait x ms’ functionality you will have to add a new thread; waiting in the event handler is NOT a good idea.

0 Likes

#5

This is very memory intensive work though? I really don’t see this being any good for anybody?

0 Likes

#6

How would I make it save every x number of keypresses?

0 Likes

#7

Just a quick example

[code]import sublime, sublime_plugin

class AutoSaveCommand(sublime_plugin.EventListener):
clicks = 0
clicksTrigger = 10
def on_modified(self, view):
self.clicks += 1
if self.clicks <= self.clicksTrigger:
self.clicks = 0
view.run_command(‘save’)[/code]

0 Likes

#8

Anybody got this to work?

Or have other solutions for an alternative to CTRL + S -> Livereload scenario?

Best regards,
Kristian Thrane

0 Likes

#9

You can use the following setup:

"save_on_focus_lost": true,

Switching to the browser will save and reload.

If you use a side by side view this will not help I know.

0 Likes

#10

Thanks - but yeah - I am looking for a solution that either fires the save-event every 10 keystrokes or every 3rd second-ish :smile:

0 Likes

#11

Did KonTrax’s plugin not work for you?

0 Likes

#12

[quote=“KonTrax”]

Just a quick example

[code]import sublime, sublime_plugin

class AutoSaveCommand(sublime_plugin.EventListener):
clicks = 0
clicksTrigger = 10
def on_modified(self, view):
self.clicks += 1
if self.clicks <= self.clicksTrigger:
self.clicks = 0
view.run_command(‘save’)[/code][/quote]

When I changed the “<=” to “>=” things worked properly for me.
if self.clicks >= self.clicksTrigger:

[code]import sublime, sublime_plugin

class AutoSaveCommand(sublime_plugin.EventListener):
clicks = 0
clicksTrigger = 10
def on_modified(self, view):
self.clicks += 1
if self.clicks >= self.clicksTrigger:
self.clicks = 0
view.run_command(‘save’)[/code]

0 Likes

#13

Hi guys!

What do you think about saving by pressing Enter or moving up/down by arrows? I saw that solution in Aptana or similar software as far as I remember.

Can you write that code for me, please? I don’t know Python.

Thanks a lot!

0 Likes

#14

As someone pointed out earlier, if the purpose is for realtime updates through LiveReload, constant reloading of the browser while coding is going to result in a FUBR’d output in the browser 80%+ of the time. Even an event listener on return will cause the browser to display all sorts of ridiculousness if a ruleset hasn’t been closed yet, or an element tag is open while coding. For viewing minor changes realtime, I suggest using the developer tools that are included with Chrome, Safari, or Firefox.

Also keep in mind that even on localhost, any scripts (particularly javascript) not correctly set to load after the DOM could realistically cause the page to never load at all while coding, even if set on a time delay (such as every 3 seconds). Probably one of my primary concerns personally would be the fact that I am constantly referring back and forth to a portion of the code in the browser’s developer tools while making changes in Sublime Text during a debug process or when fine tuning the frontend. If the browser is constantly refreshing, the whole reasoning behind using developer tools in conjunction with your IDE is moot.

0 Likes

#15

Check this github.com/jamesfzhang/auto-save

0 Likes

#16

I use auto-save for auto-saving and live-server for live reloading. but autosave won’t work when I’m inside of an HTML attribute like class. I have to press space and then hit back to remove that space, and then the auto-complete triggers when I press space. is there any way to solve this problem?

0 Likes