Sublime Forum

File: Copy path as unix

#1

I code on a windows box using ST3 but run my code via ssh on a linux server. Often I want to easily grab the file path.

however CTRL + SHIFT + P then search for “File: File Path” always returns a windows path as expected. Is there some way I can make my own implementation so it can return it using a unix file path.

That is instead of blah1\blah2\blah3 it would return blah/blah2/blah3 (yes essentially just get it to return using forward slash rather than backslash).

Bonus question:
Instead of C:/Blah1/Blah2/Blah3 I would even want to specify that it skips the drive name too (would this also be possible).

I have no idea how to do this - all suggestions welcome!

Essentially it would be just calling the same function then adjusting the string but I don’t know how or where to write this script.

0 Likes

#2

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" }
]
1 Like

#3

Absolutely amazing. Thank you so much! And sorry for the delayed reply I have been getting home late from work every day without a chance to try it. I have, and it works so well!

1 Like