Jake Scott

Useful Bash Aliases

Jan 6, 2025

Here are some bash aliases I’ve collected over the years.

Python Helpers

I work with python a lot. I made this alias to activate a projects virtual environment from anywhere in the directory structure. It uses fd to find the activate script, but you can tweak it to use find pretty easily if you don’t want to install fd.

alias activate='source $(fd -IHp1 ".venv/bin/activate$" $(git rev-parse --show-toplevel))'

This uses git to find the top level directory of the project so it only works if your project is in a git repo. But you’d never have your project without source control right?

Command Shorthand

Shortening some commands I use a lot.

alias mkd='mkdir -pv'
alias v='nvim'
alias q='exit'
alias la='ls -Agoh'
alias ll='ls -goh'

I came across these little snippets in some article many years ago and found them incredibly useful.

alias ..='cd ..'
alias ...='cd ../..'
alias ....='cd ../../..'

These allow me to go up several directories with an extra ..

This takes me to some often used directories, like my dotfiles, or neovim config.

alias dots='pushd ~/.config/home-manager && ll'
alias nvc='pushd ~/.config/nvim && ll'
alias doc='pushd ~/Documents && ll'
alias dow='pushd ~/Downloads && ll'

You’ll notice pushd, this allows me to just type popd to get back to where I was.

FZF

Some commands that leverage fzf. These predate my switch to fd and I haven’t bothered updating them.

Fuzzy find a file to edit.

alias vf='nvim $(find . -maxdepth 3 -type f | fzf)'

Jump to a project in my ~/Projcets/ directory.

alias pr='cd $(find ~/Projects -maxdepth 1 -type d | fzf)'

Prompt

I’ll leave you with my bash prompt, shows my current git branch and displays a handy icon when I’m in a nix shell. Although I’ve been eyeing starship lately.

# Prompt Colors (color: \[\e[XX;YYm\])
RESET="\[\e[0m\]"
RED="\[\e[01;91m\]"
GREEN="\[\e[01;92m\]"
YELLOW="\[\e[01;93m\]"
BLUE="\[\e[01;94m\]"
PURPLE="\[\e[01;95m\]"
CYAN="\[\e[01;96m\]"

# Function for git branch in prompt
git_branch() {
    git branch 2> /dev/null | awk '/^\*/ {print " ("$2")"}'
}

in_nix_shell() {
    [ -n "${IN_NIX_SHELL}" ] && echo '󱄅 '
}

# Set Prompt
PS1="${PURPLE}\$(in_nix_shell)${RESET}${BLUE}\w${YELLOW}\$(git_branch)${RESET}$ "
PROMPT_DIRTRIM=2 # Sets depth of directory displayed in prompt