Emacs, bash, bashrc, functions and paths - emacs

Usually I use my .bashrc file to load some functions for my bash environment. When I call these functions (that I created based on some frameworks I use). So, I play around with variables such as PATH and PYTHONPATH when I use the functions depending on the environment I'm working on.
So far so well with the terminal. The problem is that when I use emacs these functions and these environmental variables that I activate with my functions, they don't exist. .bashrc is not read by emacs, and therefore I don't have the functions loaded by .bashrc don't work. I would like them to work.
Any Ideas?

The issue might be that emacs, as many other programs you run, reads your login shell rc files, such as ~/.bash_login or ~/.profile, but not ~/.bashrc, where as your terminal also reads you user shell rc file: ~/.bashrc.

Related

how do I mimic the terminal path when making a system call in MATLAB on a mac

I'm trying to automate mac terminal calls in MATLAB. In my specific use case I used brew to install cmake but in MATLAB cmake isn't recognized [~,result] = system('cmake ..'); returns zsh:1: command not found: cmake
Using the following I am pretty sure I could update the path so that cmake is recognized.
(Mac,Matlab,bash) Changing the PATH of bash in Matlab for system commands
However, I was wondering if there was a generic way of mimicking the path that the terminal is seeing.
In particular when I run env in the terminal and in MATLAB using [~,result] = system('env'); the path variables are different and I'm wondering why that is and how to ensure they align.
When you start MATLAB from its icon on macOS, you never ran the login shell startup files, so most of your configuration is not loaded. That is, anything configured in ~/.zprofile, ~/.zshrc, etc. is not seen by MATLAB. Unlike other Unixes, macOS doesn’t start a login shell when you log on. See here for the differences between a login shell and a non-login shell.
One way around this is to launch MATLAB from a terminal window. Another way is to manually load the zsh configuration before running your shell command.
A cleaner solution is to avoid ~/.zprofile (loaded for login shells) and ~/.zshrc (loaded for interactive shells), and instead put your configuration in ~/.zshenv (loaded for all shells, including the one started by MATLAB for system() or !).
In particular, Homebrew adds a line
eval "$(/opt/homebrew/bin/brew shellenv)"
to your ~/.zprofile file. Moving this line to ~/.zshenv and restarting MATLAB should add the Homebrew configuration to shells started from within MATLAB.

Script in PATH Mac OSX terminal not working

I have the following script in my /path/to/startupscripts directory, so I can open the emacs GUI with the command 'emacs':
#!/bin/sh
/Applications/Emacs.app/Contents/MacOS/Emacs "$#"
I edited my ~/.bash_profile and added the following line
export PATH=/path/to/startupscripts/emacs:$PATH
However, the script is not working because when I type in 'emacs' into the command line emacs still opens within terminal and not the GUI like I want. Also, when I am in the /path/to/startupscripts directory and I can execute and run the script with
chmod +x emacs
./emacs
but even when I type 'emacs' afterwards it still opens within terminal. I am a bit of a beginner, and I think I am missing something painfully obvious.
The PATH should contain a directory name where your script(s) can be found, not the name of your individual script.
You probably need to source your .bash_profile:
source ~/.bash_profile
No changes made in your profile will be applied unless you source the profile or log out and back in.
Aside from that every looks like it should work.
However you may want to consider just using an alias instead of a script for this. This can be done by adding this to your profile:
alias emacs=/Applications/Emacs.app/Contents/MacOS/Emacs
That's probably a cleaner way to get the functionality you want without adding more to your PATH variable.
I hope that helps! [insert obligatory comment about how you should be using vim and not emacs :P]

Defining environment variables in Linux for Emacs access

On Macs, defining environment variables in the environment.plist will allow you to use environment variables in Emacs.app. On Linux, is there such an equivalent?
I don't want to use the environment variables in one of the shell or terminal emulators but also for find-file (C-x C-f), shell-command (C-!), etc. and I have a lot of paths defined as environment variables that would be helpful here. For instance, finding a file with find-file can be $WORK/projectname and so on.
I suppose the most straightforward way is to use setenv within .emacs, but as I have a lot of such variables it would be better if I could define it once in an external file and have Emacs read it in.
Edit: Sorry for the confusion. This question is specifically addressing issue of Emacs access, as variables defined in ~/.bashrc is not available to Emacs until shell, eshell, or term is invoked.
here a script which defines several environment variables, read by Emacs in batch mode
http://bazaar.launchpad.net/~python-mode-devs/python-mode/python-mode/view/head:/test/python-mode-tests.sh
from lisp getting $HOME for example use (getenv "HOME")
To be able to access the variables in your .bashrc you need to launch emacs from a bash shell. If you just click on an icon then the .bashrc is not read and you won't have access to your variables.
If you absolutely cannot launch emacs from a bash shell, find an init file that is loaded by your desktop environment. In my case it is .xsession. Any variable I export in it will be available to emacs.
To access environment variable use getenv.
If you define environment variables in ~/.bashrc they are available in the user shell for all commands.
The right syntax is:
export VARIABLE="value"
You can edit your .bashrc in your home directory, where you can add your own aliases, define environment variables and add paths for your custom scripts.

What's the configuration file name for Emacs ansi-term?

I tried to create a .emacs-bash file and that works for M-x shell. But if I use the ansi-term, it appears that the .emacs-bash file is not loaded... How can I solve this?
I used M-x ansi-term and then \bin\bash.
The ~/.emacs_SHELLNAME (or ~/.emacs.d/init_SHELLNAME.sh) behaviour is special to the shell function (which, naturally, knows that you're going to be running a shell).
ansi-term is a terminal emulator. It doesn't know what kind of process you're going to be running with it, so it doesn't attempt to apply any custom config files.
If you run a shell in the terminal, that shell should apply its normal rules for config files, so I would try .bashrc for starters.
Failing that, read the bash man page to see what the rules are. Environment variables would likely come into play (and you could test for environment variables in your .bashrc to provide Emacs-specific behaviour).

Setting up gdb's environment when running it through emacs

I have a program that I'd like to debug with gdb via emacs. In order to run development versions of this program, I have a shell script that I can source that sets up the calling environment to see the right libraries, etc. What I can't sort out is how to ask emacs/gud to source this file before executing gdb.
I've tried using a command like "source env.sourceme && gdb my_program", but emacs complains that it doesn't know what "source" means. I guess it's not really running gdb in a shell, so these kinds of tricks won't work.
So, how can I convince gud/emacs/whatever to run gdb in my custom environment? I've got a hacky solution in place, but I feel like I must be missing something.
gdb has its own syntax for setting environment variables:
set environment varname [=value]
Instead of a shell script, write your variable definitions in a file using the above syntax, then source the file from a running gdb session. Note that this is not bash's built-in source command, but gdb's own, so naturally bash-style environment variable definitions will not work.
What's your hacky solution?
Why wouldn't you just have a wrapper script that sources env.sourceme and then run gdb?
#!/usr/bin/env bash
source env.sourceme
gdb -i=mi $1
You can modify the Emacs environment using setenv, either interactively (M-x setenv) or programmatically:
(setenv "FOOBAR" "whatever")
When you run gud-gdb, whatever you set using setenv will be passed to the gdb process.