A while ago I made a few functions to handle all my bash files in a more automated way. Yes, “all”… I have a few.
When you setup your bash environment, you add your stuff to ~/.bashrc
or perhaps ~/.bash_profile
etc. Since they can get quite cluttered, I also have separate files for ~/.bash_aliases
and ~/.bash_functions
. It’s a great way to make things cleaner, better control/overview.
So, I hade a few aliases to handle the bash files. One (each) for open
, reload (source
file) and then to edit the file with nano
. Of course, not so hard to type manually, but it has a simple pattern to it and one get use to it quite fast.
Example:
alias openBash='open ~/.bashrc'
alias openProfile='open ~/.bash_profile'
alias openAlias='open ~/.bash_aliases'
alias openFunc='open ~/.bash_functions'
alias reBash='. ~/.bashrc'
# etc.
alias nanoBash='nano ~/.bashrc'
# etc.
I also had that for .inputrc
, .bash_history
, .bash_logout
etc… Then, by time, I’ve added a few others. I have separate files for git related stuff: .bash_git
and two others for .bash_pacman
and .bash_dnf
. Not in my Mac but on each computer that runs Arch and Fedora.
As you can see, the list of aliases can grow quite large if you have an open-/re-/nano- for each file. I don’t mind that list and the pattern is easy to work with. But anyway…
Functions
Then I decided to make a few generic functions instead. Both in hope of reducing the code, and to make it more generic.
So, these are my new 3 functions… (in .bash_functions
, of course ^^)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
|
That’s all…
The syntax/pattern is really simple. FUNCNAME [<letter>]
. No letter defaults to .bashrc
, and each letter is representing the first letter after the underscore.
openBash # Opens .bashrc
openBash a # Opens .bash_aliases
openBash f # Opens .bash_functions
# etc..
And with a couple of special cases with nanorc
and .inputrc
. They’re manually coded to i
and n
. An of course… .bash_history
is not included in reBash()
. Could be devastating, mildly speaking.
Differences
What differs this (OS X) version from Linux/*BSD version is the command open
. I have xgd-open
installed, and aliased that to open
, so one have to remove/change:
open -e -> open or xgd-open
Or you can of course use gedit
or what program you use instead.
-e
= open with TextEditIf you’re on OS X you can replace that with:
-a FavoriteProgram
Example:
open -a BBEdit
Bonus
While it might look a bit “overkill”… The bonus part - it’s “scalable”. It can grow.
If I want to add another bash file I don’t need to add anything to it. Only thing is to keep track of the names so they won’t conflict:
.bash_functions
.bash_foo
Can’t have openBash f
for both.
.bash_srv
Would work fine with openBash s
.
Happy hacking…