How do you get file path?


If I wanted to get the current file path, how do I go about doing that? It seems in the api File is what I need but I don’t even know how to load it -.-


What you need to do is to get hold of an instance of File, which you can get via an instance of Editor. In other words, if you want the File instance associated with the currently active pane, you can do something like this

editor = atom.workspace.getActivePaneItem()
file = editor?.buffer.file

and since you wanted the path to the file

filePath = file?.path

Hope this helps!


Thanks @thomasjo ! I’ll give this a try, do I need to require anything?


Nothing, because atom is a global instance variable. You’ll have access to it from anywhere within your package.

My example code does assume you’re using CoffeeScript though. If you want to use JavaScript instead, you will have to replace the existential operator ?. with plain old if statements.


@thomasjo thanks again, I had no idea that you could use regular ol javascript, silly me. I’m here giving coffee a chance. I really appreciate the help.


I’ve come upon this older thread as I look for how to get an instance of the currently selected file in the tree view. This is close, but I can’t quite make the transition to the tree view pane. How would I do that?

P.S. Examples in JavaScript art welcome! :slight_smile:


In the file below check out the “openTreeView” method. I know it’s not javascript but it might point you in the right direction.

Key combination to paste current working files absolute path
editor = atom.workspace.getActivePaneItem()
file = editor?.buffer.file

How do I get filename with extension out of this string (index.html for example from path /HTML/detail.html)?



You can use any of the standard Node libraries, like Path:


What if I need to get the path of a file that I selected from the tree view? Let’s say I have a plugin that should be activated when an element on the tree-view is selected and should fetch the folder of the selected item (so, if the selected element is a folder, it’s going to be the folder itself, otherwise for a file it will be the file directory). How could I do that?


The selected item will have the selected class on it. The span containing the text has a data-path attribute with the full path to the file:


In case anyone else finds this post, you can also use ctrl-shift-c to copy the current file’s path. Not using the API, but as you can see in the settings, this maps to editor:copy-path