19

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.

Cœur
  • 37,241
  • 25
  • 195
  • 267
BBSysDyn
  • 4,389
  • 8
  • 48
  • 63

5 Answers5

19

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

Keith Flower
  • 4,032
  • 25
  • 16
  • A warning: for me this has the side effect of making autocomplete.el run extremely slowly. Presumably it is repeatedly creating bash shells somewhere and running my entire .bashrc every time. I might try writing a wrapper around compile that temporarily changes shell-command-switch to "-ic". – dshepherd Jul 11 '13 at 11:42
  • 1
    (about `shell-command-switch "-ic"`) No, it DOESN'T work with `shell-command`. Ubuntu 16.04, emacs 24.5. There errors printed out: "bash: cannot set terminal process group (-1): Inappropriate ioctl for device. bash: no job control in this shell". If I remove `-i` option from `shell-command-switch`, `.bashrc` with aliases defined isn't loaded. See http://emacs.stackexchange.com/questions/3447. Advice "Emacs inherits its environment from the parent process" seemed nice and worked with `export`s BUT aliases in subshells doesn't work! See http://superuser.com/questions/319538 – d9k Aug 02 '16 at 05:12
9

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.

dshepherd
  • 4,989
  • 4
  • 39
  • 46
2

You may like emac's bash-completion :

https://github.com/szermatt/emacs-bash-completion

You'll be able to use tab completion of your aliases in the compilation minibuffer and in shell-mode.

Enjoy !

(they speak about it here Bash autocompletion in Emacs shell-mode )

Arch Stanton
  • 382
  • 5
  • 14
Ehvince
  • 17,274
  • 7
  • 58
  • 79
1

I think compilation commands are not interpreted through a shell: they are juste exec'ed by emacs (which means aliases, shell functions and other shell-specific things are not taken into account).

Try to wrap you compilation command into a shell-script which would source the correct environment.

You can do this either with a full-fledged shell-script in the form

#!/bin/bash
source "~/.bashrc"
my_command

or directly in emacs with a compilation command of the form

bash -c "source ~/.bashrc; my_command"

François Févotte
  • 19,520
  • 4
  • 51
  • 74
  • Francesco, I tried the bash -c thing on the compile command, and same result: bash: [my alias]: command not found – BBSysDyn Jun 08 '12 at 12:50
  • No need for the bash -c part; just add "source /home/$USERNAME/.bashrc &&" prior to any further commands in the compile-command variable. I'm using this myself and it works quite well. – elemakil Jun 08 '12 at 13:10
  • I have an alias named run_csv. I did as you asked, source /home/[ME]/.bashrc && run_csv. The result is /bin/bash: run_csv: command not found – BBSysDyn Jun 08 '12 at 13:43
  • have you tried writing a shell-script ? In my case (which is slightly more complicated than yours), it works pretty well. – François Févotte Jun 08 '12 at 14:52
  • When I place those shell scripts to a well known directory, and add their path to $PATH, then I'll have similar problem, \C-x\c will not see them. But maybe fixing a PATH issue is easier than fixing an alias issue. So I might give this a try, if I see no other viable answers here. It is quite annoying Emacs still has this problem. – BBSysDyn Jun 08 '12 at 17:40
0

See Is there a way to get my emacs to recognize my bash aliases and custom functions when I run a shell command? for a fix which doesn't run all your .bashrc and doesn't create these error messages:

bash: cannot set terminal process group (-1): Inappropriate ioctl for device bash: no job control in this shell

Ethan Bradford
  • 710
  • 8
  • 10