开发者

How do I tell a shell that it is running from within Emacs?

开发者 https://www.devze.com 2023-03-29 16:47 出处:网络
I have a program that uses ansi terminal codes to do fancy stuff like erasing lines when it runs in the terminal. When that is run from within Emacs, it obviously screws up the buffer.

I have a program that uses ansi terminal codes to do fancy stuff like erasing lines when it runs in the terminal. When that is run from within Emacs, it obviously screws up the buffer.

The program ha开发者_JS百科s an option to not use those, so I'd like to be able to turn on that option when running in a shell within Emacs.

I execute this program as a part of a Makefile, either from the terminal or using compile-mode in Emacs.

So is there a way to have compile-mode set an environment variable (or something similar) that would be easy to catch in the Makefile to use the option when appropriate?

Manually adding an extra argument to make in the make-command (in either of the cases) is what I am trying to avoid.


Emacs will set the $INSIDE_EMACS environmental variable as well. This can be useful to you if you need to know not only whether you're running the shell session inside emacs but which version of emacs you're using and how you're running the shell session (which is useful to know if you're using ansi control characters in your scripts). Here's what I get when I'm in a bash shell in Emacs (using 'shell'):

~ $ echo $INSIDE_EMACS
24.0.50.3,comint
~ $ 

Here's the same when I'm in a Bourne shell in Emacs (using 'ansi-term'):

sh-3.2$ echo $INSIDE_EMACS
24.0.50.3,term:0.96
sh-3.2$ 

Here's what I get when I do the same from a terminal (not in Emacs):

~ $ echo $INSIDE_EMACS

~ $ 


EMACS sets the $EMACS environment variable within M-x shell sessions, so you should just check for that (assuming you don't inadvertently define EMACS elsewhere within your shell scripts):

# from login shell
echo $EMACS

# within Emacs shell
bash-4.2$ echo $EMACS
t
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号