Sublime Forum

File Name and Full Path to Clipboard

#1

I’m often in need of either the full path+filename or just the filename for copy/pasting. I see that full path is available via right click in the document body. I’d love to see both of these options added to the file name in the Side Bar, file name in the tabs AND in the body (or at least one of the three).

Brilliant app btw…

1 Like

#2

It is available when you right click inside the view as “Copy File Path”

2 Likes

#3

As I said, full path is available; I want full path AND just the filename as I use both often. It would be nice if they were both available via right click anywhere the name of the file is. Eg. in the sidebar and on the tab when the file is open.

Thanks.
Brian

1 Like

#4

Why not make it yourself? Sounds simple enough. Just create a small plugin the copies the filename to the clipboard and add it to the context menu.

  • set_clipboard(string)
  • file_name() #returns full path, just parse to get the filename
  • Create a file named: Context.sublime-menu to add an option the the context menu

sublimetext.com/docs/2/api_reference.html

1 Like

#5

COOL, what i am locking for, thanks.

sublimetext.com/docs/menus

1 Like

#6

SideBarEnhancements package already add the menus to the sidebar. And now these are available via: Tools -> Commands Palette or via the shortcut which is faster.

1 Like

#7

If you still want these in some menu, with that package installed, you can copy the file

“…/Packages/SideBarEnhancements/Commands.sublime-commands”

to

“…/Packages/User/Context.sublime-menu”
“…/Packages/User/Tab Context.sublime-menu”

and customize the entries ( deleting these you don’t use).
( notice these files ends with “sublime-menu” instead of “sublime-commands”

1 Like

#8

Thanks Tito… that did the trick.

[size=150]Solution:[/size]
First install SideBarEnhancements

Then copy:
“…/Packages/SideBarEnhancements/Commands.sublime-commands”
to
“…/Packages/User/Context.sublime-menu”
“…/Packages/User/Tab Context.sublime-menu”

1 Like

#9

Just made a plugin command for this in case it helps anyone else.

{ "keys": "ctrl+alt+c"], "command": "filename_to_clipboard" }, { "keys": "ctrl+alt+shift+c"], "command": "path_to_clipboard" },

[code]import sublime, sublime_plugin, os

class FilenameToClipboardCommand(sublime_plugin.TextCommand):
def run(self, edit):
sublime.set_clipboard(os.path.basename(self.view.file_name()))

class PathToClipboardCommand(sublime_plugin.TextCommand):
def run(self, edit):
sublime.set_clipboard(self.view.file_name())
[/code]

2 Likes

#10

[quote=“robertcollier4”]Just made a plugin command for this in case it helps anyone else.

{ "keys": "ctrl+alt+c"], "command": "filename_to_clipboard" }, { "keys": "ctrl+alt+shift+c"], "command": "path_to_clipboard" },

[code]import sublime, sublime_plugin, os

class FilenameToClipboardCommand(sublime_plugin.TextCommand):
def run(self, edit):
sublime.set_clipboard(os.path.basename(self.view.file_name()))

class PathToClipboardCommand(sublime_plugin.TextCommand):
def run(self, edit):
sublime.set_clipboard(self.view.file_name())
[/code][/quote]

just found this http://superuser.com/questions/636057/how-to-set-shortcut-for-copy-file-path-in-sublime-text-3/636175#636175, the code for copy file path is “copy_path”

1 Like

#11

The copy-file-name plugin does the job very nicely, as long as you install it “properly”, as described in this issue: github.com/nwjlyons/copy-file-name/issues/3

1 Like

#12

On Right Click on the file not on the title of file.

you will get pop up like this

2 Likes

#13

[on OSX] I have tried to avoid the destruction of the clipboard as a side-effect of this command. I couldn’t always as AppleScript keystroke is not maintained anymore, it seems, and does not work well with utf-8 chars, so in that case I needed to copy the path to the clipboard, but tried to keep/restore the previous clipboard. Tried, but didn’t success. I don’t know why, but sublime.set_clipboard(p) doesn’t do its job

import sublime, sublime_plugin, os

# Fails with accents in path string

class cdTerminalWithPathCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        #  need ~ be typed (keystroke below) as ~space
        p = os.path.dirname(self.view.file_name())

        if all(ord(c) < 128 for c in p): # is ascii
            p = p.replace('~', '~ ')
            # sublime.set_clipboard(p)
            aps = f"""osascript<< END
tell application "Terminal"
    activate
    delay 1 -- just to free mod keys
    tell application "System Events"
        keystroke "c" using {{control down}}
        keystroke "cd '{p}'"
    end tell
end tell
END"""
        else:
            oldClipb = sublime.get_clipboard() # if text!
            # https://www.sublimetext.com/docs/api_reference.html#ver-dev
            sublime.set_clipboard(p)
            aps = """osascript<< END
tell application "Terminal"
    activate
    delay 1 -- just to free mod keys
    tell application "System Events"
        keystroke "c" using {control down}
        keystroke "cd '"
        keystroke "v" using {command down}
        keystroke "'"
    end tell
end tell
END"""

            sublime.set_clipboard(oldClipb)
            sublime.status_message(f"Some NO-ASCIIs in path {p}")

        exit = os.system(aps)
    def is_enabled(self):
        return self.view.file_name() is not None

0 Likes