DEV Community

Cover image for A Quick Guide to My .bashrc Setup
16bit Paladin
16bit Paladin

Posted on

A Quick Guide to My .bashrc Setup

Hey there! If you're like me, spending a good chunk of time in the terminal, you know how important it is to have a smooth and personalised Bash environment. Your .bashrc file is the secret sauce that can make your command-line experience not just bearable, but enjoyable. In this guide, I’m going to walk you through my .bashrc setup, sharing some tips and tricks that help keep things tidy, efficient, and just plain fun to use.

1. Keeping Things Tidy with XDG Directories

Let’s start with a little housekeeping. By default, configuration files can clutter up your home directory. To avoid that, I use the XDG Base Directory Specification, which organizes everything neatly into specific folders.

# ================= XDG =================
export XDG_CONFIG_HOME="$HOME/.config"
export XDG_CACHE_HOME="$HOME/.cache"
export XDG_DATA_HOME="$HOME/.local/share"
export XDG_STATE_HOME="$HOME/.local/state"
Enter fullscreen mode Exit fullscreen mode

XDG_CONFIG_HOME: This is where all my config files live.
XDG_CACHE_HOME: Perfect spot for temporary cached data.
XDG_DATA_HOME: Holds user-specific data, like application data.
XDG_STATE_HOME: Keeps state files, like session info or history.
By using these variables, I keep my home directory clean, and it makes things easier to back up or transfer when moving to a new machine.

2. Setting Up Environment Defaults

Next up, I set a few default environment variables to make sure everything runs smoothly across different setups.

# ================= DEFAULT =================
case $- in
  *i*) ;;
    *) return;;
esac

export LANG=en_US.UTF-8
export SSH_AUTH_SOCK="$HOME/.ssh/agent.sock"
export HISTFILE="$XDG_STATE_HOME/bash/history"
export GIT_EDITOR=vim
export EDITOR=vim
alias wget=wget --hsts-file="$XDG_DATA_HOME/wget-hsts"
Enter fullscreen mode Exit fullscreen mode

LANG=en_US.UTF-8: Ensures all my text handling is in UTF-8, which is a must for working with different languages.
SSH_AUTH_SOCK: This points to my custom SSH agent socket, keeping my SSH keys secure and easy to manage.
HISTFILE: I redirect my Bash history file to $XDG_STATE_HOME—it’s a small tweak, but it keeps my history file from cluttering up my home directory.
GIT_EDITOR and EDITOR: I’m a big fan of Vim, so it’s my go-to editor for Git and other text editing tasks.
wget alias: This ensures that wget uses a custom history file stored in the XDG data directory—another small way to keep things organised, because wget does not support XDG by default.

3. Supercharging Bash with Oh-My-Bash

If you haven't heard of Oh-My-Bash yet, you're in for a treat. It’s a framework that makes your Bash environment way more powerful (instead of doing everything manually). Here’s how I set it up:

# ================= OH-MY-BASH =================
export OSH="$XDG_DATA_HOME/oh-my-bash"
OSH_THEME="agnoster"
COMPLETION_WAITING_DOTS="true"
OMB_USE_SUDO=true
OMB_PROMPT_SHOW_PYTHON_VENV=true
completions=(git composer ssh)
aliases=(general ls)
plugins=(git bashmarks pyenv)
source "$OSH"/oh-my-bash.sh
Enter fullscreen mode Exit fullscreen mode

OSH: Just like with other config files, I keep Oh-My-Bash in the XDG data directory.
Theme and Options:
OSH_THEME="agnoster": I use the agnoster theme, which is both visually appealing and informative. It shows your Git branch and Python virtual environment, among other things.
COMPLETION_WAITING_DOTS="true": This shows dots while Bash completes commands—giving you a little visual feedback.
OMB_USE_SUDO=true: Allows sudo commands to play nicely with Oh-My-Bash.
OMB_PROMPT_SHOW_PYTHON_VENV=true: If you work with Python, this will show your active virtual environment in the prompt, which is super handy.
Completions, Aliases, and Plugins:
completions: Enables autocompletion for Git, Composer, and SSH commands—this is a big time-saver.
aliases: Custom shortcuts for commands I use all the time.
plugins: Adds support for Git, Bashmarks (which is awesome for bookmarking directories), and Pyenv (for managing Python versions).

4. Making SSH Easy on WSL

I use Windows Subsystem for Linux (WSL) quite a bit, and managing SSH keys can be tricky. To make it seamless, I’ve added this little script:

# ================= WSL-SSH =================
ss -a | grep -q $SSH_AUTH_SOCK
if [ $? -ne 0 ]; then
  npiperelaypath=$XDG_DATA_HOME/npiperelay
  if [ -d $npiperelaypath ]; then
    rm -f $SSH_AUTH_SOCK
    (setsid socat UNIX-LISTEN:$SSH_AUTH_SOCK,fork EXEC:"$npiperelaypath/npiperelay.exe -ei -s //./pipe/openssh-ssh-agent",nofork &) >/dev/null 2>&1
  fi
fi
Enter fullscreen mode Exit fullscreen mode

This checks if the SSH agent socket is active, and if it’s not, it sets one up using npiperelay and socat. The result? Smooth SSH authentication in WSL without having to re-enter passphrases constantly through your Windows machine.

5. Leveling Up with FZF

Finally, I’ve added FZF to my setup, which is a fuzzy finder that makes searching through files, directories, and command history a breeze.

# ================ FZF ===================
source $XDG_DATA_HOME/bash/completion.bash
source $XDG_DATA_HOME/bash/key-bindings.bash
Enter fullscreen mode Exit fullscreen mode

These scripts enable FZF’s autocompletion and key bindings, letting me quickly find what I need without a lot of typing.

Wrapping Up

And that’s it! My .bashrc is all about keeping things neat, efficient, and fun to use. By organising my config files with XDG, using Oh-My-Bash, and FZF, I’ve created a command-line environment that feels just right.

I hope this guide gives you some ideas for your own setup. Remember, Bash is incredibly customisable, so don’t be afraid to tweak things and make your environment work best for you. If you know some more tips and tricks please let me know in the comments section.

P.S.: You can keep your .bashrc file in your git repository, to pull it quickly into any new machine that you want to start working.

Top comments (0)