Sublime Forum

How to safe a view to disk?


How do i save a view?

I created a new window.new_file and applied my modifications to the view. The view should be automatically be saved to disk.

If i instead create an empty file first then use window.open_file my modifications are not applied to the opened file.

Do i create an empty file first, then open it for modification or do i create a new view first, modify it then safe it to the disk?



Creating a new view window.new_file() and than run view.run_command("save") will cause the save as dialog box to pop up.

Doing so with an empty file which was opened via window.open_file() shouldn’t.

1 Like


awesome. I need to learn more about run_command. Is there a list of commands that can be run with it?



run_command() can execute any command that’s available (i.e. it draws from the same list of commands that you can use in key bindings, menu entries or the command palette).

So you can look in those places to get a sense for commands, or use sublime.log_commands(True) in the console and take an action to see what command (and arguments) you need, which you can translate directly into a run_command() call.



I’ve got this working, thank you very much.

If i use open_file instead of new_file i run into a problem:

view = self.window.open_file("delete")
view.run_command("insert_snippet", ...)

this’ll make the opened file the current active tab.

if the file was already open before, my modifications appear on the file
if the file was not in an open tab my modifications do not appear.

I do not understand the behaviour :frowning:

Is that because i have to place a cursor position, first?



Calls to window.open_file() for files that are already opened focus the view that represents that file and return it back to you. If it’s not already open, a new view is created and Sublime starts loading the file.

However the load happens asynchronously while your code continues to execute, and you can’t make any modifications to a buffer while it’s still loading. So in the case that the file isn’t already open, your command is essentially rejected because it’s trying to modify the file before it’s ready.

view.is_loading() returns True or False to tell you if the view is currently loading or not, which you can use to detect if this is happening. One way around it is to use an EventListener or ViewEventListener listening for the on_load event to tell when the load is finished.

Alternatively, you can “defer” the operation that you would normally take until the load is done:

def post_load_action(view):
    if view.is_loading():
        return sublime.set_timeout(lambda: post_load_action(view), 10)

    # do something with the view

view = self.window.open_file('delete')

In this sample you put all actions that you want to take on the view after it’s opened into the function and call it. If the file was already open, it just immediately executes and you’re done. Otherwise it schedules itself to be called at some point in the near future to try again. Eventually the file will be loaded and the action will occur.

1 Like