Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
288 views
in Technique[技术] by (71.8m points)

sublimetext - What is Sublime Text doing when I save a file?

I was watching files with fsnotify which is a Go library, and I found there are many events triggered when I save a file.

Why does Sublime Text do so much?

2013/12/17 20:46:25 event: "parser.go": MODIFY
2013/12/17 20:46:25 event: ".subl317.tmp": MODIFY
2013/12/17 20:46:25 event: "parser.go": DELETE
2013/12/17 20:46:25 event: ".subl317.tmp": DELETE
2013/12/17 20:46:25 event: "parser.go": CREATE
2013/12/17 20:46:27 event: "parser.go": MODIFY
2013/12/17 20:46:27 event: ".subl3aa.tmp": MODIFY
2013/12/17 20:46:28 event: ".subl28d.tmp": CREATE
2013/12/17 20:46:28 event: ".subl28d.tmp": MODIFY
2013/12/17 20:46:28 event: "parser.go": MODIFY
2013/12/17 20:46:28 event: ".subl3aa.tmp": MODIFY
2013/12/17 20:46:28 event: "parser.go": DELETE
2013/12/17 20:46:28 event: ".subl3aa.tmp": DELETE
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Sublime Text 3 (which I assume you're using) uses atomic saves by default (it can be disabled by setting "atomic_save": false in your user settings), which means it creates temp files and then overwrites the original file (and deletes the temp file) on save. See this thread on the Sublime forums for a little more info, especially the reply from jps (Sublime's author) on its disadvantages.

Basically, atomic saving is useful because if anything were to happen during saving you (theoretically) shouldn't end up with a corrupted original file. Downsides include potential loss of file metadata (although Sublime uses native OS X and Windows APIs to prevent that), unexpected behavior in directories with nonstandard permissions (for example, they allow modification of existing files, but not the creation of new ones), and issues when saving to some network drives or services like Dropbox, which I've experienced personally.

EDIT

Since I originally answered this, I've come across a number of questions where post-save file-watching compilers/preprocessors like LESS/SASS/SCSS, Guard, etc. were not doing their thing because the original file they were watching had been deleted by atomic saving, then created again, but they weren't watching it any more. It can also severely affect file I/O speed when working with network filesystems, SSHFS in particular. It's a good idea in theory, but it can wreak havoc if you don't know it's there or what it's doing, so if you'll be doing any kind of work on networked/shared/watched files, it's probably best just to turn it off. Just go to Preferences -> Settings-User and add this line

"atomic_save": false

to the end (the file has to be valid JSON, so make sure there's a comma , after the previous line). Save, and you're good to go!

UPDATE

As of Sublime Text 3 Build 3072, atomic_save is now disabled by default! If you are a registered user, you can download the latest development build here. This feature has not been ported to the public beta (currently Build 3065), but hopefully a new version will be released soon. As of Feb/March 2015, Sublime's development pace has picked up substantially, with a number of new features having been added. Once the bugs have been worked out of them, a new public version should be forthcoming.

update to the UPDATE

"atomic_save": false is (as of March 2015) now in the default settings from Build 3080 and higher.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...