Is there a way to get my emacs to recognize my bash aliases and custom functions when I run a shell command?

Below are my comments about what I think was a related question:

I think both M-x shell-command and M-x compile execute commands in an inferior shell via call-process. Try the following in your .emacs (or just evaluate):

(setq shell-file-name "bash")
(setq shell-command-switch "-ic")

I notice that after evaluation of the above, .bashrc aliases are picked up for use by both M-x shell-command and M-x compile, i.e

M-x compile RET your_alias RET

should then work.

My environment: Emacs 24.1 (pretest rc1), OSX 10.7.3

Source


Have a read through http://www.gnu.org/software/bash/manual/bashref.html#Bash-Startup-Files

For non-interactive shells, the only file that is sourced is the value of the BASH_ENV environment variable. You invoke emacs like BASH_ENV=~/.bashrc emacs if emacs will use bash for shell commands -- some programs specifically use "/bin/sh"


To get shell-command to read ~/.bashrc before executing the command per the technique described in https://stackoverflow.com/a/12228646/8869495, without potential side effects due to $BASH_ENV being defined for your entire Emacs session, try this in your ~/.emacs.d/init.el (or .emacs) file:

;; I want M-! (shell-command) to pick up my aliases and so run .bashrc:
(setq shell-file-name "bash-for-emacs.sh")  ; Does this: BASH_ENV="~/.bashrc" exec bash "$@"
(setq shell-command-switch "-c")

As described by the comment in the second line, also install (in your $PATH) a script named bash-for-emacs.sh that contains:

#!/bin/bash

BASH_ENV="~/.bashrc" exec bash "$@"

Note that your ~/.bashrc might need to be changed to define non-interactive aliases even when [ -z "$PS1" ] is true (which indicates a non-interactive shell). That's the approach I'm taking for my own environment (such as it is), which is at https://github.com/jcburley/UnixHome.

Also, this assumes you want to use Bash as your Emacs-spawned shell, as I do.