Alias that allows to edit ~/.bashrc and then "sources" it automatically - Why it doesn't work?

2

I use MacBook Air with OSX 10.7.2.

I would like to create an alias that does the following:

  • Opens TextMate with ~/.bashrc and allows me to edit it
  • Once I close TextMate, "sources" ~/.bashrc (so if I add a new alias, for example, it will be available immediately)

I tried the following:

alias b="/usr/bin/mate -w ~/.bashrc; source ~/.bashrc"

but it doesn't work: when I close TextMate, the shell doesn't return (I don't see the shell prompt).

Any ideas?

Misha Moroshko

Posted 2011-11-22T22:10:27.177

Reputation: 5 573

Works for me. Is your bashrc syntax okay? – slhck – 2011-11-22T22:15:49.140

Yes, for sure.. – Misha Moroshko – 2011-11-22T22:55:02.070

Well, I tested on 10.6 with TextMate 1.5.10 (latest, afaik). Maybe you found a bug? Am I reading this right, your shell just does not work anymore? – slhck – 2011-11-22T22:58:25.250

Once you solve the non-returning shell issue, you may want to put an unalias -a command before the source command in the definition so any deletions you make in the editor will be reflected in the current shell's new alias set. F/ex, if you found you'd accidentally made a dangerous alias and you use your b alias to edit it out, the present definition would leave the bad alias in the current shell, waiting to bite you. – JRobert – 2011-11-23T01:11:43.390

Answers

0

I don't have TextMate, but using your example with emacs works as expected. Possibly a problem with TextMate? Could you try another editor?

Mats Ekberg

Posted 2011-11-22T22:10:27.177

Reputation: 329

It definitely works with TextMate, I just tried it exactly as written in the question. I presume it's a syntax error in bashrc. – slhck – 2011-11-22T22:19:03.180

There is no error in ~/.bashrc. I deleted it completely, and put only the one line above. I tried to do the same with emacs: /usr/bin/emacs ~/.bashrc; source ~/.bashrc (no -w here), and it works as expected. – Misha Moroshko – 2011-11-22T22:54:23.833

0

Okay, here are a few angles of attack:

1) Simplify. Have you tried the commands outside of the alias to see if it works?

/usr/bin/mate -w ~/.bashrc; source ~/.bashrc

A better test might be with a file that doesn't exist yet:

FILE=$TMPDIR/matewaiting-$(uuidgen); /usr/bin/mate -w $FILE; ls -al $FILE

If you get a file listing, it waited. If you get an error even though you typed something in the file and saved it, then it didn't.

2) Multiple binaries in $PATH. You didn't mention what version you're using, we should check...

$ mate --version
mate 2.4 (2013-11-03 revision 9495)

If you didn't get that but instead something like mate r1577 (2012-07-11), then you have multiple mate binaries in your $PATH and you're using the old one. In that case, run the following repeatedly and move/rename/trash any older mate/mate_wait's until you finally see the latest...OR run out of matching commands in your $PATH:

for MATE in mate{,_wait}; do
    p="$(which $MATE)"
    echo "$p: $("$p" --version)"
done

Actually, you should probably remove new ones too, since they may have been cp'ed in (you can never be too paranoid with your OS :-). The following command will hard-link the commands into your /usr/local/bin so that they are updated automagically when/if they get updated by a TextMate 2 update:

# This is just in case you changed the TM2 bundle name in /Applications
# Using Spotlight...might as well, since it turns every Mac into a heliport...
[[ -n "$TM2" ]] || TM2="$(mdfind '(kMDItemCFBundleIdentifier=com.macromates.TextMate.preview)')"

if [[ -n "$TM2" ]]; then
    # Search for the mate binary within the TM2 directory
    MATE="$(find "$TM2" -name mate)"
    if [[ -n "$MATE" ]]; then
        # Hard-link both mate and mate_wait to the
        # mate binary into /usr/local/bin
        for cmd in mate{,_wait}; do
            ln -f "$MATE" /usr/local/bin/$cmd
            echo "Linked /usr/local/bin/$cmd to $MATE"
        done
    else
        echo "ERROR: No mate found in $TM2"
    fi
else
    echo "ERROR: Could not find TextMate 2 app bundle in /Applications."
    echo "Please set TM2 environment variable with full path"
fi

This, of course, assumes you HAVE a /usr/local/bin and that it's actually in your $PATH (hopefully in FRONT of /usr/bin and /usr/sbin)...

3) Ditch the alias! I also have a realias but mine is a function (read: better for anything more than an alias as the dictionary defines it) and it was crafted with slightly more defensive programming (i.e. against my greatest enemy: me!). This from my .aliases file

# Remove all aliases
unalias -a

alias aliases='alias '

# Re-read the aliases
realias() {
    local editor="${VISUAL:-emacs}"
    if [[ -z $editor ]]; then
        echo "realias: VISUAL environment variable unset" > /dev/stderr
        return
    fi
    [[ "$editor" =~ .*mate$ ]] && editor="$editor -w "
    "$editor" $HOME/.aliases
    source $HOME/.aliases
}

$VISUAL is set in my .bashrc to emacs, though on Mac OS X, it gets reset later to $(which mate 2>/dev/null).

Jay Allen

Posted 2011-11-22T22:10:27.177

Reputation: 285