I’m guessing that you have installed a third party package or added an item to your command palette to do this because File: File Path
isn’t a command palette command that appears in core Sublime. However, opening the context menu in the body of a file (i.e. not on the tab) provides a Copy File Path
command that does this very thing; copy the path of the current file to the clipboard.
The command that does this in this case is copy_path
, whose source you can see by using View Package File
from the command palette and choosing Default/copy_path.py
as the file to open (it will open read-only).
Literally all the command does is set the text on the clipboard to the name of the file buffer associated with the current view (if it has one; newly opened tabs that haven’t been saved yet have no name, so the command disables itself).
A version of the command that performs this type of path manipulation would be something like the following plugin. It implements a command named copy_unix_path
that replaces all back slashes with forward slashes and then removes the drive letter.
import sublime
import sublime_plugin
import os
class CopyUnixPathCommand(sublime_plugin.TextCommand):
def run(self, edit):
name = self.view.file_name().replace(os.path.sep, "/")
sublime.set_clipboard(os.path.splitdrive(name)[1])
sublime.status_message("Copied Unix file path")
def is_enabled(self):
return self.view.file_name() is not None and len(self.view.file_name()) > 0
To use it you would add this as a plugin (see this video) if you don’t know how to do that) to make the command available, and then bind it to a key so you can call it.
Alternatively, you could add a file named something like MyCustomCommands.sublime-commands
with the following content in your User
package, which would add the command to the command palette for you.
[
{ "caption": "File: Copy Unix Path", "command": "copy_unix_path" }
]