Sublime Forum

Save_on_focus_lost not working with multi-file replace

#1

Hi,

I am using ST4 on Linux and have save_on_focus_lost set.
Changes are saved when I switch tabs or app windows.

My expectation of the editor’s state is with this setting: there can be changes only in the currently active tab/buffer.

However, when I am using the search-replace over multiple files, the files are opened and the replacements are done in the editor, but they are not saved, until I switch to each and every newly opened tab and switch away/close them or explicitly save all/close all buffers, which I sometimes do not want to do, as I have some scratch, file-less buffers I do not want as files at all.

It is tolerable for a few files, but when I suspect that 5+ files are affected, I rather do the replacement from the shell unhappily with a recursive grep xargs sed pipeline.

I consider the behavior I am experiencing a bug and I hope this is the place to report it.

BTW, a related feature could be not to keep files with replaced content open, that were not open before - to prevent tab-clutter.

Maybe there is a way to work around these and keep my assumption of “no unsaved change except in current file”, but I have not found it, yet.

Best,
Krisztian

0 Likes

#2

The setting in question is:

	// Set to true to automatically save files when switching to a different file
	// or application
	"save_on_focus_lost": false,

I would not interpret that (personally) as saving all files in all windows when the focus switches, but rather when you make a change to a file and it’s unsaved, that view losing focus to another one causes it to save. So I would probably hedge on that working as expected and not being a bug.

That said, File > Save All will save all files with changes all at once, so if you use Find in Files and do a replacement, you can save all such files all at once doing that. There’s no key binding on it by default, but there could be.

The Sublime core always opens files when you do this, so that you can verify the changes yourself (so that you don’t have to click “Yes” or whatever on every change in 10’s or 100’s of files, for example). There’s no internal functionality for closing such views afterwards though.

In theory a plugin can easily find all tabs that are unsaved and save them (as save_all would do), but in order to also close the files that were not previously open there would need to be some way to give it context on what files were open before you took the replace action.

0 Likes

#3

Thanks, I appreciate the response, but I still disagree.

  1. It is not possible to get to a state where there is any other unsaved tab, except through search-replace/some plugin like LSP-refactoring operation over multiple files + scratch buffers (these are actually expected to remain unsaved!)
  2. Save All is not an option if there are scratch buffer[s], that I do not want to save: I can not get past them. Either I have to save all scratch buffers or cancel the save all operation - usually neither is my preferred choice.

I usually do the verification of replace operations outside of Sublime Text - either git diff or Sublime Merge is usually a better option after mass-replaces.

I still feel, that search replace in files is quite unusable and I rather do it with find-xargs-sed from the shell, than from ST.

0 Likes