SublimeTips

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() window.run_command('close_file') view = window.new_file() view.set_name(newfilename) view.settings().set("auto_indent", False) view.run_command("insert", {"characters": contents}) view.set_scratch(True) view.run_command("prompt_save_as") Flow of this sample Copy all text on current file to memory (contents). Close current file. Create new file with new file name.

Changing File Name and Reopening Renamed File by Sublime Text

This sample is for changing file name and reopening the file with new name. The flow is as follows. A file (sample.py) is opened. Rename the file from sample.py to newsample.py. The opened file is replace to the file with new name. os.rename(oldfilewithpath, newname) view = self.view.window().find_open_file(oldfilewithpath) if view: view.retarget(newname)