2

In an attempt to go around an annoying aspect of tmux, I have the following code in my .bashrc file:

alias emcs="command emacs"

# Fix emacs in tmux
emacs () {
    if [ $TERM != "xterm" ]
    then
        TERM=xterm emacs "$@"
    else
        emacs "$@"
    fi
    return;
}

The alias is simply for easier access to the original emacs command.

The function is supposed to replace emacs . . . with TERM-xterm emacs . . ., regardless of the arguments listed afterward.

My problem is that when I run emacs, it hangs on the command line. If I change the function to "emaacs" or anything other than "emacs" then it works flawlessly. Why is it hanging when I'm using the actual name of the command, and what can I do to make it work?


(If you are wondering why I am doing this, it's because tmux changes the terminal to screen, which for some reason changes the emacs colors where comments and variable names are the same color.)

2
  • 4
    Your function is recursive: you need to use command emacs in there. Apr 25, 2018 at 16:27
  • 1
    Also, you seem to always want to use xterm, so just always use it: emacs() { TERM=xterm command emacs "$@"; } or alias emacs='TERM=xterm command emacs' -- actually aliases do not recursively call themselves, so you can get away with alias emacs='TERM=xterm emacs' Apr 25, 2018 at 16:29

2 Answers 2

3

Take a look at your function. In this example, I have changed some names to indict the guilty, and struck some irrelevancies to your problem:

recurse{} (
    recurse "$@"
}

What do you think this will do when invoked?

To fix this, you can call out the explicit binary:

emacs () {
    if [ $TERM != "xterm" ]
    then
        TERM=xterm /usr/bin/emacs "$@"
    else
        /usr/bin/emacs "$@"
    fi
    return;
}

Or you can rely on your path being properly set:

emacs () {
    if [ $TERM != "xterm" ]
    then
        TERM=xterm command emacs "$@"
    else
        command emacs "$@"
    fi
    return;
}
1
  • 1
    Better to use command (to allow path lookup while bypassing functions and aliases) than to hard-code the path.
    – chepner
    Apr 25, 2018 at 16:42
2
alias emacs='TERM=xterm emacs'

I believe this is all you need. It sets TERM to xterm unconditionally and runs emacs.

If you wish to only do this in tmux:

emacs () {
    if [ -n "$TMUX" ]; then
        TERM=xterm command emacs "$@"
    else
        command emacs "$@"
    fi
}

This uses command to not call the function recursively. This is not needed in the alias. There is also no need to return in this function.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .