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

前端 未结 5 1337
南笙
南笙 2020-12-02 20:30

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-!

5条回答
  •  执念已碎
    2020-12-02 21:04

    To get shell-command to read ~/.bashrc before executing the command per the technique described in https://stackoverflow.com/a/12228646/8869495, without potential side effects due to $BASH_ENV being defined for your entire Emacs session, try this in your ~/.emacs.d/init.el (or .emacs) file:

    ;; I want M-! (shell-command) to pick up my aliases and so run .bashrc:
    (setq shell-file-name "bash-for-emacs.sh")  ; Does this: BASH_ENV="~/.bashrc" exec bash "$@"
    (setq shell-command-switch "-c")
    

    As described by the comment in the second line, also install (in your $PATH) a script named bash-for-emacs.sh that contains:

    #!/bin/bash
    
    BASH_ENV="~/.bashrc" exec bash "$@"
    

    Note that your ~/.bashrc might need to be changed to define non-interactive aliases even when [ -z "$PS1" ] is true (which indicates a non-interactive shell). That's the approach I'm taking for my own environment (such as it is), which is at https://github.com/jcburley/UnixHome.

    Also, this assumes you want to use Bash as your Emacs-spawned shell, as I do.

提交回复
热议问题