I want to get the name of the current file in a package for Atom 1.0. I know how to get the full path to the file, but I would like to only get the file name part of the path. This is the code I have so far (taken from atom-terminal):
editor = atom.workspace.getActivePaneItem()
file = editor?.buffer?.file
filepath = file?.path
I tried to read though the docs to see if such an attribute already exists, but the Pane items are not documented as far as I could find. Is there documentation available somewhere else than at https://atom.io/docs/api/v1.0.0?
If there isn't an attribute, is there an appropriate standard function for extracting the file part of the path from filepath
in a platform independant way?
The method provided by @jacwah is no longer working in atom 1.18. According to the API docs you can get path using the following code:
atom.workspace.getActiveTextEditor()?.getPath()
Use file.getBaseName()
. This will return only the filename part of the path to file
. I found this by logging the file to the console and examining it's properties.
editor = atom.workspace.getActivePaneItem()
file = editor?.buffer?.file
filename = file?.getBaseName()
You can also use the node.js path
module's basename
function.
path = require('path')
editor = atom.workspace.getActivePaneItem()
file = editor?.buffer?.file
filename = path.basename(file?.path)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With