How to open emacs gui/ide from mac terminal? - macos

I'm trying to open files up on emacs outside of the terminal. I prefer a gui/ide environment when I code instead of doing it through a terminal. I initially thought that typing emacs filename.py would open that file through Emacs.app, however it only allowed me to edit the file through the terminal. When this didn't work, I looked into editing the .profile and .emacs files in my home directory but this was to no avail.
Maybe this is more intuitive than what I've read but I can't seem to figure it out. Any help is appreciated.

Assuming you have Emacs installed from Homebrew like this:
brew install emacs --with-cocoa
Just type the following command to open Emacs.app from terminal:
open -a Emacs filename.py
If you want all files opened in the same frame, instead of new frames, put this into your .emacs file:
(setq ns-pop-up-frames nil)

The best way to open files in Emacs from the terminal is the emacsclient command, which will open the file in your existing Emacs app (preventing startup time). If you're on OSX and you installed Emacs through Homebrew, the emacsclient binary will already be set up. (In your Emacs config, you have to include (server-start) somewhere.)
If you actually want to spin up a new GUI app instance instead, you can set up your own shell script and put it in your PATH somewhere before the existing emacs binary. It sounds like you're using Homebrew, which sets up the emacs binary as the following shell script:
#!/bin/bash
/usr/local/Cellar/emacs/24.3/Emacs.app/Contents/MacOS/Emacs -nw "$#"
The -nw is what prevents Emacs from opening in GUI mode. You can make your own emacs shell script and leave out -nw:
#!/bin/bash
/usr/local/Cellar/emacs/24.3/Emacs.app/Contents/MacOS/Emacs "$#"

To do what you want, you'd need to find the location of the actual binary contained in Emacs.app, and use that as the command instead of emacs. Most likely, it's at
/path/to/Emacs.app/Contents/MacOS/Emacs
Which, if you have Emacs.app in your Applications folder, as would be typical, would be
/Applications/Emacs.app/Contents/MacOS/Emacs
To set it up with a shorter command to use, you could try adding to your .profile (I don't know what shell you use) the following line, or whatever equivalent it has for your shell (This works for bash and zsh, at least):
alias emacsgui='/Applications/Emacs.app/Contents/MacOS/Emacs'

The modern way to go about this is by installing Emacs using Homebrew Cask:
brew cask install emacs
Source: this comment by Homebrew project leader Mike McQuaid, which reads:
Cocoa support for Emacs will not be accepted. This is provided by brew cask install emacs.

One should link emacs to /Applications if not already done,
brew linkapps emacs
to link the emacs to symlink emacs installed in Cellar. Once symlinked, you can open emacs by
open -a emacs
as already pointed out by #katspaugh

brew doesn't have cask command anymore.
I used brew install emacs and I can find Emacs app installed in my application directory.
You can also head to https://emacsformacosx.com and download the .dmg file.

Related

How to make mac to use emacs 26

I installed emacs using brew cask install emacs
when I type
$ emacs
the emacs that opens still on version 22 instead of 26.
How can make my mac to use version 26?
FYI
In case anyone interested to open from command line, add below as your alias
alias emacs='open -a /Applications/Emacs.app $1'
When you use cask, it should install it to your Applications folder, not the command line. Check your Applications folder, it should be a stand alone program.

-bash: emacs.profile: command not found

I am trying to resolve this issue and trying to get it work. What are all the steps of the solution? I really don't know what to do on my Mac terminal
-bash: emacs.profile: command not found
What are you trying to accomplish? If you're trying to open a file named .profile with emacs you'll want to add a space between the command emacs and the file .profile:
$ emacs .profile
If you are typing emacs.profile you will get an error because you are mixing together the command emacs and the file you are trying to edit: .profile. Therefore the solution would be to add a space between them like someone suggested: $ emacs .profile.
On the other hand, if you are typing emacs .profile and you are still getting: -bash: emacs: command not found, this means that you haven't installed emacs yet and your system doesn't recognize that instruction.
Emacs is a very popular text editor and is widely used by technical users, but it is not installed by default.
Solution 1:
If you want to edit a file (in this case ~/.profile) you don't need emacs to do so, you could just use a normal text editor or a pre-installed text editor using your terminal like:
$ vi ~/.profile or $ nano ~/.profile
(Guide to use Vi/Vim text editors) (Guide to use Nano).
Solution 2:
If you want to install emacs to edit that file, you can do so using brew:
$ brew update
$ brew install emacs
And after that you can use:
$ emacs ~/.profile
You can find other ways to install emacs here.
Important Note:
One important thing to consider in this example, is that if you are trying to edit the .profile file (i.e. to set an exported environment variable) you should know that the name of the .profile file might vary from one Mac system to another. The name of this profile configuration file depends on the shell of your system, if you are using bash the name of this profile will be .bashrc or .bash_profile.
Before editing your profile file make sure what is the name of the profile file you are trying to edit. To do so, you can use:
$ cd $HOME
to go to the Home folder and then use:
$ ls -al
to see hidden files (These files starting with . are hidden files). You will find the profile file of your system there.

Get Vim on Mac terminal to copy to the clipboard

I'm using Vim on a Mac, installed via homebrew, version 8.0.
Vim --version shows +clipboard, and -xterm_clipboard.
I've tried
set clipboard=unnamed
in .vimrc
But I cannot get yanks to go on to the system clipboard. What do I do?
It should "just work" on MacVim. One of the reasons I'm using it is for its OSX integration, including clipboard.
brew install macvim
mvim defaults to graphical mode. To run it in terminal mode:
mvim -v
In the past, I had an alias for vim -> mvim -v which handles most cases. These days I use the following script in ~/bin/vim, which is in my $PATH:
#!/bin/bash
mvim -v "$#"
seems like something is overriding your clipboard setting after loading your vimrc.
try :verbose set clipboard?. It should tell you when and where this variable was last set.
This guide gives you a bunch of hints on what to do to figure out what's going on and fix the problem

Making the PATH and other environment variables available in emacs

Working with emacs in OSX can some times be troubling.
I interchange with using the Terminal.app application and the emace shell both of these use bash, and both seem to work, I do however have the problem that the environment is not setup identically in both.
I'm running a clean installation of OSX Lion, and emacs 24 from emacs and would like for the environment variables in the emacs shell to make the same as in the Terminal.app under osx. How do I fix that?
Just install the awesome little package exec-path-from-shell and you're done! It will automatically set your emacs exec-path to be same as the PATH in your zsh/bash config.
It will also allow you to copy the values of other shell variables like this:
(exec-path-from-shell-copy-env "PYTHONPATH")
If your environment variables are being set in ~/.bash_profile, Create a file called ~/.emacs_bash and put in it the following:
. ~/.bash_profile
Don't forget the newline at the end of that line or it won't be executed.
~/.emacs_bash is loaded by emacs when you run bash from within it, such as when using M-x shell or shell-command so you can use it to set any environment variables you want available.
Open Emacs.app from within Terminal.app (instead of from Finder):
$ emacs
This works for me using Emacs from Homebrew with Cocoa (brew install emacs --with-cocoa). It launches Emacs.app in a Cocoa GUI window (use emacs -nw to fallback to terminal UI). All the environment variables I have set in my bash environment appeared to be set in Emacs.
I have not tried this with Emacsformacosx. I personally prefer Emacs from Homebrew as I heard Emacsformacosx is not compiled with certain extensions I use such as TLS and Imagemagick... you can compile Emacs with those extensions using brew install emacs --with-XXX.
As of 2019, exec-path-from-shell will only pass PATH to Emacs, If you want to pass all environments, you can try:
https://github.com/ersiner/osx-env-sync/

emacs on OS X 10.6

I am a new iMac user. I have extensive experience with Linux on a PC. I downloaded latest version of emacs to the Applications folder. I want to invoke emacs from the command line. However, the default path for emacs is /usr/bin/emacs. what is the best practice for adding the new emacs to the path? I am tempted to create a ~/bin directory and a link to the new emacs and adding ~/bin to the beginning of my path. This is how we did things in our software development environment on linux PC's
Best way is to use Homebrew and use
brew install emacs --cocoa
so you have a easy to update emacs installation. The Cocoa will make sure you have your mac keybinding working before emacs. Make the binary run at startup as a daemon (because it starts up not very fast), for instance:
/usr/local/Cellar/emacs/23.2/Emacs.app/Contents/MacOS/Emacs --daemon
And make an script to the emacsclient command and saved it to /bin/emacs file (don't forget to make it executable):
#!/bin/bash
exec /Applications/Emacs.app/Contents/MacOS/bin/emacsclient -n -c "$#"
so when you fire up at bash "emacs something.txt" the already running emacs daemon opens it instantly. You can also extend it to open Emacs if the daemon is not running!
I tested it on the latest emacs 23.2, some features are not present on early versions.
Assuming you were still in linux land, wouldn't the canonical place to put this be in /usr/local/bin (and add that to your path?) ... I'd stick with that, if you were to go that route, but this is how I have my emacs setup:
I've downloaded the latest plain/vanilla Emacs from emacsforosx.com
I've made an emacs alias that I use to fire up a terminal-based version of emacs when I don't want (or can't) run the GUI version, like so:
alias emacs='/Applications/Emacs.app/Contents/MacOS/Emacs -nw'
If you want to fire up the GUI version of Emacs from the terminal, you can just type the following (which, AFAIK, is a mac-ism, so you wouldn't have known that coming from linux):
$ open -a Emacs
There's a slew of information about emacs on OS X at the emacs wiki.
~/bin or /usr/local/bin will work fine, as will manipulating your PATH.
Assuming you're using Emacs.app, simplest thing to do is to use open -a /Applications/Emacs.app "$#". open is the command line equivalent of double-clicking on something in Finder. Put that into a shell script, stick it into your PATH and go.
Installing emacs-app via MacPorts is probably the simplest way to get and maintain a Cocoa emacs.
You may wish to look into Aquamacs which is a further refinement of emacs for OS X. The emacs wiki page on Aquamacs is very helpful. It also has an option to add a little aquamacs script to your PATH that will open a file in the aquamacs GUI.
I create a shell script named emacs in my ~/bin directory containing:
open -a Emacs "$#"
Obviously, ~/bin needs to be before /usr/bin in my PATH which I set in ~/.profile so that it shadows the preinstalled emacs binary.
I also create a symlink via ln -s /Applications/Emacs.app/Contents/MacOS/bin/emacsclient ~/bin/emacsclient so that this also shadows the preinstalled emacsclient binary.
For additional connivence, I create an alias ec='emacsclient -a emacs -n ' and include (server-start) in my emacs init scripts. This enables me to open a file from the commandline using ec filename regardless if emacs is or is not already running.
Another tip: When you launch the emacs via Applications or open, emacs does not inherit the same path as you have in your terminal environment, so one thing I have found very useful is to run the following in my .profile after setting my path to change the PATH inherited cocoa applications:
defaults write ${HOME}/.MacOSX/environment PATH "$PATH"
That will work. If this is a native mac application, the binary is actually located under the application directory (not the capitalization of the binary): .../Emacs.app/Contents/MacOS/Emacs
Since you are coming from linux, you might be interested in MacPorts. This is a large collection of packages ported from linux. It allows packages to be installed and upgraded from the command line, doe sdependancy management, all the stuff you would expect. It includes a native version of Emacs, that can be invoked from the command line.

Resources