Organize your terminal aliases and functions

Veröffentlicht von

By the time using different programming languages, frameworks, tools and workflows, my terminal files ~/.bashrc and ~/.bash_profile grew larger and larger. Maintenance and readability were crap. Additionally I wanted to simply share parts of my aliases and functions with coworkers to speed up development and have unified workflows.

How to simply split up terminal files and organize them in a simple maintainable way:

Create a folder to store the new partials: mkdir ~/.dotfiles and version control it.
Next, split your current .bashrc and .bash_profile based on your desired scheme, mine looks like this:

ls -a ~/.dotfiles/
.bashrc // some leftovers like 
.dockerfunc
.dockerrc
.gitrc
.gorc
.phprc

At this step, .bashrc also got moved to .dotfiles folder and ~/.bash_profile is empty.

Now we need to load (source) all of our new dot files to still have them available when opening a terminal window. For this purpose I created a small loader that iterates over a given list of file names that will be loaded from the .dotfiles folder.

Save the following code to your ~/.bash_profile and update it to your needs.

# Load the shell dotfiles, and then some:
# * ~/.path can be used to extend `$PATH`.
# * ~/.extra can be used for other settings you don’t want to commit.
for file in ~/.dotfiles/.{bashrc,gitrc,gorc,dockerfunc,dockerrc,phprc}; do
  [[ -r "$file" ]] && [[ -f "$file" ]] && source "$file"
done
unset file

As you can guess, you may change the file names, if you have a different grouping: {bashrc,gitrc,gorc,dockerfunc,dockerrc,phprc} .

From now on, if you want to look up some aliases for complex commands or modify your terminal files, you may open the ~/.dotfiles folder with your IDE. I am using VSCode and just type code ~/.dotfiles in my terminal.