Making the PATH and other environment variables available in emacs - macos

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/

Related

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

Set path to applications for vim on mac capitan

I have used vim to make small scripts in python and typesetting things in LaTeX. So it is very useful to run applications from vim by typing :!python or :!pdflatex etcetera. But after upgrading to el capitan, this seems not to work anymore, get message like /bin/bash: pdflatex: command not found. But the funny thing is that it is possible to run the applications directly from terminal. Anyone that know how to set the correct that for vim as well?
$PATH variable not properly set in gvim/MacVim when it is opened from the finder
first check :!echo $SHELL and see if SHELL is set to bash or not
then try to set your path in your ~/.bash_profile?
export PATH=<dir_contains_pdflatex>:$PATH

Setting environment variables in Yosemite

What is the proper way to modify environment variables like PATH in Yosemite?
This is this question Setting environment variables in OS X? but specifically for yosemite since it doesn't work anymore.
Have you tried editing ~/.bash_profile?
Adding a line like this to bash_profile ought to do it:
export PATH=/usr/local/bin:$PATH
What shell are you using? I'm assuming you're using the default Bash shell. There's also Csh, Ksh, and Zsh.
The Terminal.app on Mac OS X by default starts a new login shell each time a window is open. This means that $HOME/.bash_profile or $HOME/profile is always executed when you open a new terminal window. You can set particular defaults in here. For example, I set PS1 and set -o vi.
NOTE: This may not be the case if you're using other Terminal apps like xterm. These open new terminal windows as just new shells. This means that you may not see the changes made in .bash_profile until you log out and log back in.
You can try editing $HOME/.bashrc and see if that helps.
What about other shells?
If you're using Kornshell (ksh), you need to edit the $HOME/profile and not $HOME/.bash_profile. If you're using Zshell (zsh), you're on your own. It's too wacky to describe here. Read the manpage for zsh and search for ZDOTDIR.
When you run a shell script, the $HOME/.bashrc is executed. Most people put something like this in their .bash_profile, so their .bashrc settings are included in a new terminal window:
[[ -x $HOME/.bashrc ]] && source "$HOME/.bashrc"
Some people set things they want to be set when they run a shell script, for example export $PS4="\$LINENO> ".
The $PATH is a bit different. You can set it in .bash_profile (I would not set it in .bashrc), But, Mac OS X has an automated why on how systemwide paths are set. A file called /etc/paths is used to set the default path for all users using either Bash or Kornshell via the /usr/libexec/path_helper program.
On my Mac, I set my $PATH to:
/usr/local/bin:/usr/share/bin:/bin:/usr/bin:/usr/sbin:/sbin:$HOME/bin
When I install programs, I usually install them under /opt when possible. Then, I link their binaries (where ever they're placed) to /usr/local/bin. This way, I don't have to keep building my PATH. Plus, it allows me to override system defaults. For example, /usr/bin/git is at 1.9.3. while my installed /usr/local/bin/git is at version 2.2.1.
One thing you should not do is modify /etc/profile because changes there may be replaced on OS X upgrades.
The problem is not with environment variables set and accessed from within /bin/bash or /bin/sh, but with envars that should be set for programs NOT executed from the shell; i.e. normal apps executed from the dock or Finder.
After finally getting things right with launchctl in Mavericks, Apple is in the process of changing things again. The useful subcommands of launchctl are now labelled as "legacy subcommands", some of which are no longer supported. That leaves a question mark over the others.
In any case, the most important subcommands are still available for now.
launchctl allows for the setting of the overall environment in which user processes execute. It appears that the overall user environment is inherited by all Terminal processes; i.e. all setenv variables are exported. It's a bit tricky to confirm that. In any case, you will still need your .profile and .bashrc to define functions and aliases, which are not supported by launchctl.
I go to some lengths to ensure that all of my launchctl vars are also defined in my profile. This enables me to set up the same environment on remote or VM linux systems, with a few minor tweaks.
Al of my setup is described in this blog post .
following solution worked for me.
Open Terminal
Click on Terminal Menu at right upper corner.
click on Preferences
Click on General
Change Shell open with to command and put /bin/bash in text box.
Now whatever configuration you do in ~/.bash_profile takes effect. Previously you were not using bash(were using ksh) that is why it was not reading .bash_profile.

How to open emacs gui/ide from mac terminal?

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.

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