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:
- Open “Preferences/Key Bindings User”
- 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!