Sublime Forum

Rename a file you have open

#1

I think it might be useful to be able to rename the file being worked on from inside the editor, maybe adding rename to the tab context menu, or ctrl+click on the tab?

I know you can rename the buffer, and then save the file, and then delete the other fileā€¦heh

Any other quick way to rename a file and keep it open?

  • toothrot
0 Likes

#2

Not that I know of but very possible with a plugin :smile:

0 Likes

#3

Youā€™re right, I know thereā€™s an openInCurrentTab, however, I will lose my undo history because itā€™s actually a new viewā€¦

Also, I canā€™t get it to work.

This is what Iā€™m trying without success in the console:

view.window().runCommand(ā€˜openInCurrentTabā€™, [rā€™c:\somefile.txtā€™])

(if I use the ā€˜openā€™ command instead, it opens the file correctly, in a new tab of course.)

It prompts me for a filename. Can anyone point me in the right direction?

Anyhow, plugin aside, I just thought it would be useful to be able to rename from the tab and keep the undo history.

-toothrot

0 Likes

#4

Ok hereā€™s the plugin for all of you, enjoy it :smile:

[code]import sublime, sublimeplugin, os, functools

class renameCurrentTab(sublimeplugin.WindowCommand):
def run(self, window, args):
view = window.activeView()
current_name = os.path.basename(view.fileName())
current_dir = os.path.dirname(view.fileName())
window.showInputPanel(ā€œNew Tab Name:ā€, current_name,
functools.partial(self.doRename, window, current_name, current_dir), None, None)

def doRename(self, window, current_name, current_dir, new_name):
	old_name_path = os.path.join(current_dir, current_name)
	new_name_path = os.path.join(current_dir, new_name)
	os.rename(old_name_path, new_name_path)
	window.runCommand('close')
	window.openFile(new_name_path.replace('\\','/'))[/code]

To add it to your context menu or tab context menu read this:

NOTE TO JON: Apparently **openInCurrentTab **is BUGGY! it does NOT accept any arguments, it will always ask you for a file to open :frowning:
beta 20100201

0 Likes

#5

Thanks for that, itā€™s not all that different from what Iā€™m already usingā€¦

Hopefully the powers that be might think renaming while preserving undo history is useful enough to spend a little time onā€¦

At the very least a fixed openinCurrentTab will improve things a bit

0 Likes

#6

[quote=ā€œtoothrotā€]Thanks for that, itā€™s not all that different from what Iā€™m already usingā€¦

Hopefully the powers that be might think renaming while preserving undo history is useful enough to spend a little time onā€¦

At the very least a fixed openinCurrentTab will improve things a bit[/quote]

Agree.

0 Likes

#7

EJ12N, would you be able to extend that plugin so that it can also make a backup? One of the things I find myself doing often is wanting to make big changes to a file I have open, but I want to save a backup first, so first I would Save Asā€¦ with a new filename, then I would close it and reopen the file I was originally editing. This also removes the undo history :neutral_face:

It would be nice if there was a ā€œSave backupā€ command that would save the current file but tack on a ā€œ.bakā€ extension, while keeping the current file open.

0 Likes

#8

Did you try the ā€œAutomatic Backupsā€ plugin?

0 Likes

#9

+1 for ā€œSave As Copyā€¦ā€ if thatā€™s what you mean Grey Wyvern

0 Likes

#10

I donā€™t want to make automatic backups. Every now and then I am working on a file that is part of a larger framework, like a website or a source repository, so I want to keep editing the file using the same filename; but I also want to save a version of it before I start doing the edits, in case it messes up everything, so I can easily restore it.

Yup, exactly. Itā€™s just Save Asā€¦ except the current view sticks with the previous file rather than moving to the new one.

0 Likes

#11

[code]import sublime, sublimeplugin, os, functools, shutil

class renameCurrentTab(sublimeplugin.WindowCommand):

def run(self, window, args):
	view = window.activeView()
	current_name = os.path.basename(view.fileName())
	current_dir = os.path.dirname(view.fileName())
	window.showInputPanel("New Tab Name:", current_name,
        functools.partial(self.doRename, window, current_name, current_dir), None, None)

def doRename(self, window, current_name, current_dir, new_name):
	old_name_path = os.path.join(current_dir, current_name)
	new_name_path = os.path.join(current_dir, new_name)
	os.rename(old_name_path, new_name_path)
	window.runCommand('close')
	window.openFile(new_name_path.replace('\\','/'))

class backupCurrentTab(sublimeplugin.WindowCommand):
backup_ext = ā€œ.bakā€ #file extension for backups
def run(self, window, args):
view = window.activeView()
backup_name_path = view.fileName() + self.backup_ext
shutil.copy(view.fileName(), backup_name_path)[/code]

NOTES: renameCurrentTab is same code as the other post Iā€™m just posting everything for the sake of copy and paste :stuck_out_tongue:

**renameCurrentTab **= renames the current tab and opens the newly renamed file.
**backupCurrentTab **= creates a backup file of the ā€œcurrent tabā€ by default does ā€œ.bakā€ extension, this can be changed to whatever you want. Like it was said before, this is basically a quick ā€œSave Asā€ with a predefined name that keeps old file open. Useful when youā€™re about to do ā€œriskyā€ or large changes to a file.

happy editing :smile:

0 Likes

#12

This is great :smile: It works just fine from the File menu or if the tab is active.

Can you also include an ā€œunderā€ version so it can be called from the right-click context menu on tabs that arenā€™t active? I tried figuring it out myself, but it just kept saving the active tab :neutral_face:

0 Likes

#13

This is what Iā€™m currently using, This renames the buffer and will prompt you to confirm the filename when you issue the save command. It would be nice if the saveAs command took an optional filename argument. This would allow renaming with only one prompt to be done with a plugin: record the original filename, change the buffer name with an inputpanel, save it without prompting using something like 'saveTo ā€™ and then os.remove the old file after checking all is well

As it stands the extra prompt just actually needs you to hit enter so itā€™s not too bad, but I think being able to programmatically save to a specified file would be nice.

0 Likes

#14

Iā€™m afraid we canā€™t do our own ā€œUnderā€ commands like the ones sublime has in the tab context menu. is not possible right now. The API doesnā€™t allow it. or at least I canā€™t find it where it gives u the view object or id of the tab hoveredā€¦

0 Likes