ChatGPT解决这个技术问题 Extra ChatGPT

How can I open the Atom editor from the command line in OS X?

I have the Atom editor and was wondering how you can open a file or folder from the terminal in Atom. I am using a Mac. I am looking for a way to do this:

atom . (opens folder)

atom file.js (opens file)

atom (opens editor)

Is this possible and how do I set it up?


P
Peter Mortensen

With the Atom editor open, in the menu bar:

Click Atom → Install Shell Commands

https://cloud.githubusercontent.com/assets/194400/7987327/ba954dce-0ad4-11e5-897a-19ed0f37ff41.png

You should expect to see:

https://cloud.githubusercontent.com/assets/194400/7987297/88143464-0ad4-11e5-81f3-4ec7c1bdfc42.png

Potentially restart your terminal (I did it just out of habit and am not sure if you need to).


What happened to me was that atom and apm symlinks were pointing to the Downloads folder from where I ran Atom for the first time. Then after moving Atom to the applications folder the symlinks were broken and the Install Shell Command option was not able to notice it. What I had to do was to remove the symlinks and then select the Install Shell Commands from the Atom menu. There was no need to restart the console as the atom command worked immediately after selecting the option. I ran sudo rm /usr/local/bin/atom and sudo rm /usr/local/bin/apm from a terminal to remove the symlinks
Turned out you don't need to restart terminal.
My computer was getting mixed up with XCODE and I had to restart and that fixed the problem.
This command only works if I don't restart my MacBook Pro after Install Shell Commands
This should be the answer
P
Peter Mortensen

When Atom installs, it automatically creates a symbolic link in your /usr/local/bin folder. However, in case it hasn't, you can create it yourself on your Mac:

ln -s /Applications/Atom.app/Contents/Resources/app/atom.sh /usr/local/bin/atom

Now you can use atom folder_name to open a folder and atom file_name to open a file.


If you get "File exists" and the Atom command still dose not work just delete it and type the command over again.
When I do this, it continues to run in the terminal. Is there a way to be able to close the terminal after the command?
@jhamm sorry I don't understand what you mean. Just go ahead and close the terminal, it doesn't affect Atom in any way.
Doesn't work. All input in Atom editor is sent to the terminal session that opened the Atom app. I solved the issue by choosing "Install Shell Commands" under the "Atom" menu.
You might need to create the /usr/local/bin/ directory with sudo mkdir /usr/local/bin/ and use sudo for ln like sudo ln -s /Applications/Atom.app/Contents/Resources/app/atom.sh /usr/local/bin/atom
P
Peter Mortensen

Roll your own with user3283997's solution, or in Atom, choose the menu option AtomInstall Shell Commands. This creates two symbolic links in /usr/local/bin.

apm -> /Applications/Atom.app/Contents/Resources/app/apm/node_modules/.bin/apm
atom -> /Applications/Atom.app/Contents/Resources/app/atom.sh

The atom command lets you do exactly what you're asking. apm is the command line package manager.


I needed this to validate that I was pointing my aliases to the correct executables.
s
sbedulin

For Windows 7 x64 with default Atom installation add this to your PATH

%USERPROFILE%\AppData\Local\atom\app-1.4.0\resources\cli

and restart any running consoles

(if you don't find Atom there - right-click Atom icon and navigate to Target)

https://i.stack.imgur.com/URPQk.png


OP is on Mac. I guess he requires a solution specific to Mac only
I agree, but since the word "Mac" is not in the title or tags, it appears in search for everyone, hence may be helpful for Windows users.
If you don't want to bind this to a specific release, just use the bin folder: %USERPROFILE%\AppData\Local\atom\bin
@sbedulin OSX appears in the title as well a question tag.
The question has been edited, but I'm not going to remove the answer util it's helpful for others (personally I'm against editing questions which are 3 years old).
s
scottmcallister

The symlink solution for this stopped working for me in zsh today. I ended up creating an alias in my .zshrc file instead:

alias atom='sh /Applications/Atom.app/Contents/Resources/app/atom.sh'


same here, weird, I can't seem to understand why this happened.
amingilani if you have installed the terminal for use with the zsh rather than the bash you need to put this alias on the file .zshrc: vim ~/.zshrc or nano ~/.zshrc for edit the file and then put on the final: alias atom='sh /Applications/Atom.app/Contents/Resources/app/atom.sh' save the file and then: source ~/.zshrc for load.
this is great if you don't have administrator privileges on your machine +1
If you are using ZSH, uncomment the line export PATH=$HOME/bin:/usr/local/bin:$PATH in ~/.zshrc and restart the terminal
sh: /Applications/Atom.app/Contents/Resources/app/atom.sh: No such file or directory
t
trusk

On macOS you can add it to your ~/.bash_profile

as

alias atom='open -a "Atom"'

and from terminal just call

atom filename.whatever


D
Dejay Clayton

Open the application by name:

open -a 'Atom' FILENAME

You can create an alias "alias atom='open -a Atom'" in your bash or zsh profile.
P
Peter Mortensen

For Windows 10 and the new release of Atom, I solved the problem by adding in my environment variable on the "PATH" row:

%USERPROFILE%\AppData\Local\atom\bin

What kind of environment variable (on Windows)? "System" or "User"?
P
Peter Mortensen

https://i.stack.imgur.com/DAJax.png

Go to your terminal and locate your directory. Run this command:

atom index.html 

index.html is just an example.


What directory? Can you be more specific? Please respond by editing (changing) your answer, not here in comments (without "Edit:", "Update:", or similar - the answer should appear as if it was written today).
P
Peter Mortensen

In addition to sbedulin's answer (greeting, lovely Windows users!):

The general path on Windows should be

%USERPROFILE%\AppData\Local\atom\bin

If you are using a Bash emulator like Babun, you'd better checkout the shell files, which are only available in the real application folders.

/c/User/<username>/AppData/Local/atom/app-<version>/resources/cli/apm.sh # Or atom.sh

P
Peter Mortensen

I’ve noticed this recently with all new Macs here at my office. Atom will be installed via an image for the developers, but we found Atom is never in the Application folder.

When doing an ls on the /usr/local/bin folder, the path for atom will show something like "/private/var/folders/cs". To resolve this, we just located file atom.app and copied it into the application folder, and then ran the system link commands provided by nwinkler which resoled the issue. Developers can now open Atom from the command line with "atom" or open the current projects from their working directory with "atom ."


P
Peter Mortensen

Make sure to put (move) "Atom" into the Application directory.

https://i.stack.imgur.com/j2xYR.png


this is done automatically when installing correctly the package, which is my case as well, so Atom didnt create the sym link as mentioned in the accepted answer
In my case, this Atom is extracted into Downloads directory.
G
Goran_Ilic_Ilke

On Linux && Mac OS-es type "atom ." in terminal.


P
Peter Mortensen

Another simple solution is to add /usr/local/bin to your PATH environment variable. I had the same issue, and I installed shell commands (see shaheenery's response). The symbolic links already existed and were pointing to the correct destination (see thomax's response), however I would still get 'not found'. I'm using Korn Shell btw.

Here's what I did:

Open your ~/.profile file using your choice of editor (in my case, emacs ~/.kshrc) Add this line: export PATH="/usr/local/bin:${PATH}" Save and exit Restart terminal or source ~/.profile Test with atom -h


P
Peter Mortensen

Upgrading Atom appears to break command line functionality on the occasion. It looks like in my case it created two versions of the application instead of overwriting them. It occurs because the new file structure doesn't match file paths created by Atom → Install Shell Commands. In order fix the issue, you'll need to do the following.

Move "Atom X" from Documents into Applications (why it ended up in here, I have no idea) Rename "Atom X" to "Atom" Might need to restart your terminal and Atom

After that, everything should work just like it did before.


Curious where you got Atom 3 from? Did you mean 1.0.3? According to their release page we're still in 1x.... atom.io/releases
@Susan So am I, not too sure why I wrote that looking back at it. Adjusted to make more sense.
h
hal9000

I had the same issue which I resolved by first moving Atom.app from downloads to Applications. Then under Atom's menu options, I selected "Install Shell Commands".


P
Peter Mortensen

With ConEmu on Windows 10 I couldn't call atom from the console even after I added %USERPROFILE%\AppData\Local\atom\bin to PATH in environment variables. I just added

alias atom="C:/Users/me/AppData/local/atom/app-1.12.7/atom"

to my .bashrc file.


P
Peter Mortensen

I had problems due to Atom being unable to write its logfile when starting from the command line. This cured it.

sudo chmod 777 ~/.atom/nohup.out

This looks like an issue with your permissions. You shouldn't have to use sudo in your own home folder. Is it possible that you started Atom using sudo before? That would explain why the logfile had the wrong permissions. Check the ownership of the file and then change it to be owned by your own user (using chown).
P
Peter Mortensen

Add the path (:/usr/local/bin/) in the shell profile file.

Mac: file $home/.bash_profile

export PATH=$GOPATH/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/git/bin:$PATH

P
Peter Mortensen

I am on MinGW Bash, so I have created the ~.profile file with the following:

alias atom='~/AppData/Local/atom/bin/atom'