Tuesday, October 7, 2025

Modularizing Your Bash Functions (Example: vicd with Vifm)

Step 1: Create a Modular Config Directory

Keep your Bash setup clean by separating custom scripts:


mkdir -p ~/.bashrc.d
This directory will store small .sh files instead of crowding your .bashrc.


Step 2: Add Your Function

Create a file just for your Vifm directory picker:


vim ~/.bashrc.d/vicd.sh
Paste this inside:

vicd() {
    local dst
    dst="$(command vifm --choose-dir - "$@")"
    if [ -z "$dst" ]; then
        echo 'Directory picking cancelled/failed'
        return 1
    fi
    cd "$dst"
}


Step 3: Source All Scripts Automatically

At the end of your ~/.bashrc, add:


for f in ~/.bashrc.d/*.sh; do
    [ -f "$f" ] && . "$f"
done
Reload your shell:

source ~/.bashrc
Now your vicd function (and any future ones) load automatically — tidy, modular, and easy to manage.

No comments:

Post a Comment