Home Download Buy Blog Forum Support

Rename a file you have open

Re: Rename a file you have open

Postby GreyWyvern on Mon Feb 22, 2010 2:55 pm

gpfsmurf wrote:Did you try the "Automatic Backups" plugin?


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.

toothrot wrote:+1 for "Save As Copy.." if that's what you mean Grey Wyvern


Yup, exactly. It's just Save As... except the current view sticks with the previous file rather than moving to the new one.
GreyWyvern
 
Posts: 106
Joined: Fri Oct 23, 2009 7:08 pm
Location: Canadia

Re: Rename a file you have open

Postby EJ12N on Mon Feb 22, 2010 7:48 pm

Code: Select all
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)


NOTES: renameCurrentTab is same code as the other post I'm just posting everything for the sake of copy and paste :P

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 :)
EJ12N
 
Posts: 281
Joined: Tue Apr 21, 2009 5:23 pm

Re: Rename a file you have open

Postby GreyWyvern on Mon Feb 22, 2010 8:14 pm

This is great :) 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 :|
GreyWyvern
 
Posts: 106
Joined: Fri Oct 23, 2009 7:08 pm
Location: Canadia

Re: Rename a file you have open

Postby toothrot on Mon Feb 22, 2010 9:22 pm

sublimator wrote:The view.setName() API might come in handy


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 <path>' 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.
toothrot
 
Posts: 28
Joined: Sun Dec 13, 2009 8:20 am

Re: Rename a file you have open

Postby EJ12N on Wed Feb 24, 2010 2:40 pm

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..
EJ12N
 
Posts: 281
Joined: Tue Apr 21, 2009 5:23 pm

Previous

Return to Ideas and Feature Requests

Who is online

Users browsing this forum: No registered users and 9 guests