Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Emacs compilation mode won't see bash alias

Tags:

emacs

emacs23

Emacs M-x compile does not see any aliases set in .bashrc. If I use M-x shell then type the alias, it is fine. I tried sourcing .bashrc from /etc/profile, from ~/.profile, ~/bash_env, anything I can think of to no avail.

I am on Emacs 23 and Ubuntu 11. I start emacs using /usr/bin/emacs %F, from a desktop button.

like image 615
BBSysDyn Avatar asked Jun 08 '12 09:06

BBSysDyn


2 Answers

Emacs inherits its environment from the parent process. How are you invoking Emacs - from the command line, or some other way?

What happens if you:

M-x compile RET C-a C-k bash -i -c your_alias RET

Invoking bash as an interactive shell (-i option) should read your .bashrc aliases.

Edit: 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

like image 53
Keith Flower Avatar answered Sep 28 '22 19:09

Keith Flower


Keith Flower's answer works but can result in some slowdowns due to .bashrc being unnecessarily loaded in other places (presumably many many times, my computer is not exactly under-powered but emacs was almost unusable when trying to use autocomplete.el).

An alternative way is to locally modify shell-command-switch only for the functions where it is needed. This can be done using emacs' "advice" feature to create a wrapper around those functions. Here's an example that modifies compile:

;; Define + active modification to compile that locally sets
;; shell-command-switch to "-ic".
(defadvice compile (around use-bashrc activate)
  "Load .bashrc in any calls to bash (e.g. so we can use aliases)"
  (let ((shell-command-switch "-ic"))
    ad-do-it))

You need to write similar "advice" for each function that you want to use .bashrc (e.g. I also needed to define the same advice for recompile), just copy the above and replace compile in the above with another function name.

like image 23
dshepherd Avatar answered Sep 28 '22 19:09

dshepherd