Emacs compilation mode won't see bash alias

浪子不回头ぞ 提交于 2019-12-04 23:05: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'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.

Ehvince

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 )

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"

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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!