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