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?
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 (withPROMPT_COMMAND
), and haven't had any problems. Just make sure that whatever you source is idempotent. – wchargin – 2019-05-01T04:02:07.8801What'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 quitvi
with false exit status – Tom Hale – 2019-05-03T08:43:55.343