Reopening Current File as a File with New Name at Sublime

This is for Sublime Text. This sample is for reopening current file as a file with new file name. The current file is closed when reopening a new file.

newfilename = "new file name"
contents = self.view.substr(sublime.Region(0, self.view.size()))
window = self.view.window()
view = window.new_file()
view.settings().set("auto_indent", False)
view.run_command("insert", {"characters": contents})

Flow of this sample

  1. Copy all text on current file to memory (contents).
  2. Close current file.
  3. Create new file with new file name.
  4. Paste contents to new file.
  5. Open dialog box for saving new file.