Alias to source .bashrc after it's been edited?

3

I would like to add an alias (for convenience sake) that would allow me to edit my bashrc, and if changes were made, source it, but if no changes were made, don't source (in case I changed my mind).

I initially tried alias vb='vi ~/.bashrc && source ~/.bashrc' but then I realized that even just quitting out of vi :q or :q! probably still returns a success return value which is why it still sources.

Is there some way to make an alias such that quitting vi without making any changes to the file doesn't source .bashrc but if something is changed, it does?

psycho9o

Posted 2019-04-30T20:55:38.910

Reputation: 33

Note that you can exit vim with nonzero status with :cq (but be very careful—this exits immediately without prompting to save changes). Also, for what it's worth, I source my aliases after every command (with PROMPT_COMMAND), and haven't had any problems. Just make sure that whatever you source is idempotent. – wchargin – 2019-05-01T04:02:07.880

1What's the problem with sourcing .bashrc even if no changes were made? It's not a file that should contain anything but alias or function definitions, anyway... – user1686 – 2019-05-01T09:12:53.690

Take into account that if you have multiple terminals open, the new changes will only be sourced in the one that you edit the file. You'll have to manually source it in all other opened terminals. – Carlos Campderrós – 2019-05-01T10:21:51.347

@grawity There's no real problem with doing it. I do have some updates to my PATH variable, so every time I source it, it adds those updates to PATH again and again. But I don't think that's a huge deal, just a personal annoyance (unless it actually has side effects that I don't know about). – psycho9o – 2019-05-01T14:39:22.427

Use :cq to quit vi with false exit status – Tom Hale – 2019-05-03T08:43:55.343

Answers

7

What about:

alias vb='SUM1=$(md5sum ~/.bashrc) && vi ~/.bashrc && SUM2=$(md5sum ~/.bashrc) && if [ "$SUM1" != "$SUM2" ] ; then source ~/.bashrc ; fi'

Comar

Posted 2019-04-30T20:55:38.910

Reputation: 211

Why the indirection via md5sum? Just diff (cmp) the files directly. – Konrad Rudolph – 2019-05-01T08:59:07.287

@KonradRudolph: Because it's the same file that differs across time (note how one md5sum is run before $EDITOR and the other afterwards). You cannot cmp today's .bashrc and yesterday's .bashrc unless you make a copy before editing, and that's actually more work than the current approach. – user1686 – 2019-05-01T09:11:31.283

@grawity My comment was abbreviated. Of course I meant storing the contents of the file in a variable, same as is currently done with the MD5 sum. It’s not more work than storing the md5sum. old="$(<~/.bashrc)". – Konrad Rudolph – 2019-05-01T09:12:31.187

There are always alternative ways of doing stuff :) – Comar – 2019-05-01T13:48:17.440

Thanks @Comar That worked nicely. – psycho9o – 2019-05-01T14:40:42.117

4

How about using a bash function instead of an alias? The following works in my tests:

  • Add the following function to ~/.bashrc
  • source ~/.bashrc
  • when you want to change ~/.bashrc type bashmod at the prompt

    function bashmod () {
    
            pre=$(shasum ~/.bashrc);
            vim mybuf;
            post=$(shasum ~/.bashrc);
    
            if [ "$pre" = "$post" ]; then
                    echo "~/.bashrc unchanged";
            else
                    . ~/.bashrc;
            fi
    }
    

DC Slagel

Posted 2019-04-30T20:55:38.910

Reputation: 97

Thanks @Caqe. Yours is pretty much the same as the accepted answer, and it seems to work the same – psycho9o – 2019-05-01T14:42:31.473

1

Same idea as the other answers, but comparing the files directly instead of going the detour via checksums:

vb() {
    local old="$(<~/.bashrc)"
    vim ~/.bashrc
    if ! cmp -s ~/.bashrc <<< "$old"; then
        source ~/.bashrc
    fi
}

Konrad Rudolph

Posted 2019-04-30T20:55:38.910

Reputation: 7 043

How big can the contents of a variable be? – Tom Hale – 2019-05-02T15:07:02.340

1

@TomHale Good question, the answer seems to be “reasonably big”: https://stackoverflow.com/q/5076283/1968. But note that (exported) environment variables have a much smaller limit, since the overall size of the exec environment is typically fairly limited.

– Konrad Rudolph – 2019-05-02T15:18:13.070