Sublime Forum

Simple Way To Rename The Current File

#1

So I have been trying to find a simple way to rename the file I have open in my focused view. I did some searching and I found that people were going to the lengths of using entire packages just to do this. I did some digging and I found a pretty simple solution.
Here is the solution:

  1. Open “Preferences/Key Bindings User”
  2. Add this binding to you list of personal bindings:
{ "keys": "shift+f6"], "command": "rename_path", "args": {"paths": "$file"]} }

So my overall custom binding map looks like this:

{ "keys": "alt+f1"], "command": "reveal_in_side_bar" }, { "keys": "alt+f2"], "command": "open_dir", "args": {"dir": "$file_path", "file": "$file_name"} }, { "keys": "ctrl+alt+s"], "command": "save_all" }, { "keys": "ctrl+alt+h"], "command": "htmlprettify" }, { "keys": "ctrl+keypad0"], "command": "focus_side_bar" }, { "keys": "shift+f6"], "command": "rename_path", "args": {"paths": "$file"]} } ]
3. When pressing shift+f6 on the current file should result in a new edit window with the literal “$file”
4. Now to have sublime check for the current file being edited I needed to update my side_bar.py file. This file is where the rename file command lives and is the same command used when you rename a file from the side bar right click menu.
5. Add this line of code to the RenamePathCommand Class under the run function.

if paths[0] == "$file":
            paths[0] = self.window.active_view().file_name()

So my whole run function now looks like this:

    def run(self, paths):
        if paths[0] == "$file":
            paths[0] = self.window.active_view().file_name()
        branch, leaf = os.path.split(paths[0])
        v = self.window.show_input_panel("New Name:", leaf, functools.partial(self.on_done, paths[0], branch), None, None)
        name, ext = os.path.splitext(leaf)

        v.sel().clear()
        v.sel().add(sublime.Region(0, len(name)))

Now once everything is saved try to rename a file again and you should get the actual file name instead of “$file”. When the rename occurs it will update the current view to the new file name.
I hope this helps you guys out!

0 Likes

#2

The sidebar enhancements plugin provides this functionality with side_bar_move, and can be accessed with a keyboard shortcut without the sidebar being visible. I have added side_bar_move to the contextual menu, for a right click option.

I was unable to get the plugin mentioned in this thread to work out of the box. The plugins of Quick Move and Quick Rename (available on github) require at least two open tabs to function correctly. So, my preferred solution is:

github.com/titoBouzout/SideBarEnhancements

0 Likes