Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

In my shell environment I have aliases and custom functions. When I am in an instance of emacs (I always use emacs -nw) and I execute a shell command (M-!) I cannot use them. This makes sense since I imagine it launches it's own subshell to do these... but is there a way (maybe in my .emacs) to get this to work? Perhaps even if it involved sourcing an environment by default before executing any shell command given?

like image 996
Palace Chan Avatar asked Sep 01 '12 04:09

Palace Chan


People also ask

How do I run a shell command in Emacs?

You can execute an external shell command from within Emacs using ` M-! ' ( 'shell-command' ). The output from the shell command is displayed in the minibuffer or in a separate buffer, depending on the output size.

How do I check bash aliases?

How to list existing Bash Alias? You can use the alias -p command to list all the alias currently defined. that contains the list of aliases as created by the Bash alias builtin. $BASH_ALIASES will lose its special properties when being unset.

Can you use alias in shell script?

Your alias is well defined, but you have to store it in ~/. bashrc , not in a shell script. Add it to that file and then source it with . . bashrc - it will load the file so that alias will be possible to use.


2 Answers

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

like image 75
Keith Flower Avatar answered Oct 12 '22 01:10

Keith Flower


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"

like image 34
glenn jackman Avatar answered Oct 12 '22 00:10

glenn jackman