159

How to (easily) get current file path in Sublime Text 3

I don't often use ST console (I used it only once to install package manager), but I suppose it could be good way to :

  • get current file path like some kind pwd command.
  • But it doesn't work.

Does anyone know an easy way to get current file path?

  • to clipboard : better not a strict objective in the answer
  • not necessary by ST command, maybe package?
2
  • 4
    Just if you're curious, in console you get the path using view.file_name() and you copy it to clipboard using sublime.set_clipboard(view.file_name())
    – sergioFC
    Apr 20, 2015 at 9:46
  • If you want to see the path, just hover your mouse over the tab, and a tooltip with the path will show up.
    – a06e
    Nov 11, 2017 at 23:20

9 Answers 9

301

Right click somewhere in the file (not on the title tab) --> Copy file path

If you don't want to use the mouse, you could set up a keyboard shortcut as explained here https://superuser.com/questions/636057/how-to-set-shortcut-for-copy-file-path-in-sublime-text-3

7
  • 1
    Works in sublime text 2 as well. So awesome, yet so simple Sep 26, 2015 at 14:07
  • 1
    Right clicking images doesn't seem to work though :/
    – aug
    Oct 13, 2016 at 0:53
  • 2
    @aug keyword being 'text' in the name Sublime Text. Jan 17, 2017 at 5:43
  • 10
    been here before...damn I keep clicking on the title tab to get it
    – zok
    Jul 12, 2017 at 16:33
  • 3
    too bad it's not possible on the tab, you have to switch tab before doing that, sometimes it's a little bit annoying
    – Rafiki
    Sep 14, 2017 at 8:40
29

To easily copy the current file path, add the following to Key Bindings - User:

{ "keys": ["ctrl+alt+c"], "command": "copy_path" },

Source

Key Bindings - User can be opened via the command palette (command + p on OSX)

3
  • 4
    How do you know that copy_path is the right command ? How do we know what available ? Is there any docs that I miss ?
    – code-8
    Feb 18, 2017 at 4:57
  • 4
    @ihue - good question; wasn't able to locate copy_path in the docs; if you check out the source I provided above, that user turned on command logging via sublime.log_commands(True). Feb 27, 2017 at 16:44
  • May be it will help someone: to get back the Copy path from command palette add a Default.sublime-commands entry as described in the documentation with: [{"caption": "File: Copy path", "command": "copy_path"}]
    – Awea
    Mar 15 at 11:12
22

Easy to understand using image. On Right Click you will get this.

enter image description here

Transcribed code in image for convenience:

import sublime, sublime_plugin, os

class CopyFilenameCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        if len(self.view.file_name()) > 0:
            filename = os.path.split(self.view.file_name())[1]
            sublime.set_clipboard(filename)
            sublime.status_message("Copied file name: %s" % filename)

    def is_enabled(self):
        return self.view.file_name()...  # can't see
4

Mac OS X - Sublime Text 3

Right click > Copy File Path

enter image description here

3

A lot of these answers involve touching the mouse. Here's how to do get the path without any mouse clicks using SideBarEnhancements

  1. Install SideBarEnhancements using PackageControl.
  2. Click super + shift + P to open the command palette
  3. In the command palette begin typing path until you see File: Copy Path
  4. Select File: Copy Path

Now the path to file you are working in is copied into your clipboard.

Edit: As pointed out by @Antoine in the comments, as of 2024 the package SideBarEnhancements no longer provides this functionality. The FileManager package is the replacement for SideBarEnhancements. The steps using FileManager are

  1. Install FileManager using PackageControl.
  2. Click super + shift + P to open the command palette
  3. In the command palette begin typing copy path until you see the FileManager options FileManager: Copy Path From Root, FileManager: Copy Absolute Path, and FileManager: Copy Relative Path
  4. Select the option that's right for you.
2
2

There is a Sublime Package which gives your current file location inside a status bar. I just cloned them directly to my /sublime-text-3/Packages folder.

git clone [email protected]:shagabutdinov/sublime-shell-status.git ShellStatus;

git clone [email protected]:shagabutdinov/sublime-status-message.git StatusMessage;

You have to check/read the description on GitHub. Even it is listed in package control it would not install properly for me. You can actually edit the shell output as you want. If you have the right skills with python/shell.

Looks like this (Material Theme) enter image description here

2

If you're like me and always click on items in the sidebar just to realize that copying the path only works when clicking in the editor area, have a look at the SideBarEnhancements package. It has a huge bunch of options to copy file paths in a variety of different ways.

Installation is available via Package Control (despite the webpage only mentions installation via manual download).

Note: The package “sends basic, anonymous statistics”. The webpage explains how to opt out from that.

SublimeSideBarEnhancementsScreenshot

1
1

Go to this link. The code in the link is given by robertcollier4.

Create a file named CpoyFileName.py or whatever you like with .py extension.

Save the file in Sublime Text 3\Packages\User folder. Then paste the above given key bindings in your Preferences: Key Bindings file.

Now, you can use the specified key bindings to copy just filename or total (absolute) filepath.

Please note that the filename or filepath do contain file extension.

1

Fastest Solution ( No Packages Needed + Comprehensive ):

Folder Path:

  1. Folder in "Sidebar"
  2. Right Click
  3. "Find In Folder"
  4. "Where" field contains everything you need

File Path:

  1. File in current "Tab"
  2. Right Click
  3. "Copy File Path"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.