Skip to content

Instantly share code, notes, and snippets.

@legeiger
Last active July 18, 2025 07:58
Show Gist options
  • Select an option

  • Save legeiger/fc4a70780aeea3bc7bef33abeed0adb4 to your computer and use it in GitHub Desktop.

Select an option

Save legeiger/fc4a70780aeea3bc7bef33abeed0adb4 to your computer and use it in GitHub Desktop.
aliases for use with bash or zsh. git clone https://gist.github.com/fc4a70780aeea3bc7bef33abeed0adb4.git ~/.
# ~/.aliases
# Main aliases file - to be sourced by .bashrc or .zshrc
# --- General Aliases ---
# List files
alias ls='ls --color=auto'
alias la='ls -a'
alias ll='ls -lh --color=auto'
alias llh='ls -lh'
alias lt='ls --human-readable --size -S --classify' # For sorting by size
# Diff
alias diffs='diff --side-by-side --suppress-common-lines'
# Misc
alias vidinfo="ffprobe -v quiet -print_format json -show_format -show_streams -print_format json" # From Gist & initial list
## Calc using bc with decimal precision of 4
calc() {
if [ -z "$*" ]; then
echo "Usage: calc <expression>"
echo "Example: calc 2+2/3"
echo "Note: For division, bc's default scale is 0."
echo "This function uses 'scale=4' by default for division results."
echo "You can also use bc interactively: calc"
bc -l # Start bc in interactive mode if no arguments
return
fi
echo "scale=4; $@" | bc
}
# --- Utility Functions ---
function trash() {
# Moves specified files/directories to ~/.Trash
# Create ~/.Trash if it doesn't exist: mkdir -p ~/.Trash
if [ $# -eq 0 ]; then
echo "Usage: trash <file1> [file2 ...]"
return 1
fi
mv "$@" ~/.Trash && echo "Moved to ~/.Trash: $@"
}
# --- Source other alias and function files ---
# Ensure these files exist in $HOME or adjust paths accordingly.
if [ -f "$HOME/.docker-aliases.sh" ]; then
. "$HOME/.docker-aliases.sh"
echo "Docker aliases loaded."
fi
if [ -f "$HOME/.tmux-aliases.sh" ]; then
. "$HOME/.tmux-aliases.sh"
echo "Tmux aliases loaded."
fi
# Source custom functions
if [ -f "$HOME/.bash_functions.sh" ]; then
. "$HOME/.bash_functions.sh"
echo "Bash functions loaded."
fi
# You can add a final message to confirm loading if you like:
# echo "All custom aliases and functions sourced."
#!/bin/bash
# ~/.bash_functions.sh
# Collection of bash functions
# --- Docker Helper Functions ---
# d-aws-cli-fn: Placeholder for your AWS CLI Docker function
# Example: docker run --rm -it -v ~/.aws:/root/.aws amazon/aws-cli "$@"
function d-aws-cli-fn() {
echo "INFO: d-aws-cli-fn is a placeholder."
echo "Please define its behavior (e.g., run AWS CLI in a Docker container)."
echo "Example: docker run --rm -v ~/.aws:/root/.aws amazon/aws-cli \"\$@\""
# Implement your actual command here, e.g.:
# docker run --rm -it -v "${HOME}/.aws:/root/.aws" -v "$(pwd):/aws" amazon/aws-cli "$@"
}
# dex-fn: Execute a command in a running container (defaults to bash)
# Usage: dex <container_name_or_id> [command_and_args...]
function dex-fn() {
if [ -z "$1" ]; then
echo "Usage: dex <container_name_or_id> [command_and_args...]" >&2
echo "If command is not provided, it defaults to 'bash'." >&2
return 1
fi
docker exec -it "$1" "${@:2:-bash}" # Uses second arg onwards, or 'bash' if only one arg
}
# di-fn: Inspect a Docker object (container, image, volume, etc.)
# Usage: di <object_name_or_id>
function di-fn() {
if [ -z "$1" ]; then
echo "Usage: di <docker_object_name_or_id>" >&2
return 1
fi
docker inspect "$1"
}
# dnames-fn: List names of all running containers
function dnames-fn() {
docker ps --format "{{.Names}}"
}
# dip-fn: Display IP addresses of all named running containers
function dip-fn() {
echo "IP addresses of all named running containers:"
local OUT=""
local DOCS
DOCS=$(dnames-fn)
if [ -z "$DOCS" ]; then
echo "No running containers found."
return 0
fi
# Save and change IFS for reliable loop over lines
local OLD_IFS="$IFS"
IFS=$'\n'
for DOC in $DOCS; do
# Restore IFS for commands inside the loop if necessary, though not strictly here for docker inspect
IFS="$OLD_IFS"
# Attempt to get IP address; handle cases where it might be empty or container has multiple networks
IP=$(docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}} {{end}}' "$DOC" 2>/dev/null | awk '{$1=$1};1') # awk to trim
if [ -z "$IP" ]; then
IP="Not assigned or N/A"
fi
OUT+="${DOC}\t${IP}\n"
IFS=$'\n' # Re-ensure IFS for next loop iteration
done
IFS="$OLD_IFS" # Restore original IFS
if [ -n "$OUT" ]; then
# Use printf for better formatting control than echo -e
printf "%s" "$OUT" | column -t
else
echo "Could not retrieve IP addresses for listed containers."
fi
unset OUT DOCS IP DOC OLD_IFS # Clean up local variables
}
# dl-fn: Follow logs of a container
# Usage: dl <container_name_or_id>
function dl-fn() {
if [ -z "$1" ]; then
echo "Usage: dl <container_name_or_id>" >&2
return 1
fi
docker logs -f "$1"
}
# drun-fn: Run a command in a new, temporary container interactively
# Usage: drun <image_name> [command_and_args...]
function drun-fn() {
if [ -z "$1" ]; then
echo "Usage: drun <image_name> [command_and_args...]" >&2
return 1
fi
# --rm ensures the container is removed on exit
docker run -it --rm "$@"
}
# dcr-fn: Run a one-off command on a service in Docker Compose (V2 syntax)
# Usage: dcr <service_name> [command_and_args...]
function dcr-fn() {
if [ -z "$1" ]; then
echo "Usage: dcr <service_name> [command_and_args...]" >&2
return 1
fi
# --rm ensures the container is removed on exit
docker compose run --rm "$@"
}
# dsr-fn: Stop AND remove a container
# Usage: dsr <container_name_or_id_1> [container_name_or_id_2...]
function dsr-fn() {
if [ $# -eq 0 ]; then
echo "Usage: dsr <container_name_or_id_1> [container_name_or_id_2...]" >&2
return 1
fi
echo "Stopping and removing container(s): $@"
docker stop "$@" && docker rm "$@"
}
# drmc-fn: Remove all exited Docker containers
function drmc-fn() {
local exited_containers
exited_containers=$(docker ps -a -q -f status=exited)
if [ -n "$exited_containers" ]; then
echo "Removing all exited containers..."
# Use xargs to handle potentially empty list and pass to docker rm
echo "$exited_containers" | xargs docker rm
echo "Exited containers removed."
else
echo "No exited containers to remove."
fi
}
# drmid-fn: Remove dangling (unused and untagged) Docker images
function drmid-fn() {
local dangling_images
dangling_images=$(docker images -f "dangling=true" -q)
if [ -n "$dangling_images" ]; then
echo "Removing dangling images..."
echo "$dangling_images" | xargs docker rmi
echo "Dangling images removed."
else
echo "No dangling images to remove."
fi
}
# dstats-fn: Display live resource usage statistics for containers
# Usage: dstats [container_name_or_id...] (if no args, shows all running)
function dstats-fn() {
echo "Displaying Docker container stats. Press Ctrl+C to exit."
if [ $# -eq 0 ]; then
docker stats # Show stats for all running containers
else
docker stats "$@" # Show stats for specified containers
fi
}
# dtop-fn: Display the running processes of a container
# Usage: dtop <container_name_or_id> [ps_options]
function dtop-fn() {
if [ -z "$1" ]; then
echo "Usage: dtop <container_name_or_id> [ps_options]" >&2
return 1
fi
docker top "$@"
}
# Placeholder for dc-fn, if you had a custom function for 'docker compose'
# function dc-fn() {
# echo "INFO: dc-fn is a placeholder."
# echo "If you intended 'dc' to call a custom function, define it here."
# # Your custom 'docker compose' related commands
# }
# --- Other Utility Functions from your Gist ---
# (The 'trash' function was simple enough to keep in .aliases,
# but if you have more complex general functions, they can go here)
# ~/.docker-aliases.sh
# Docker and Docker Compose Aliases
# --- Docker Basic Commands ---
alias dps='docker ps'
alias dpsa='docker ps -a'
alias dim='docker images'
alias dvol='docker volume' # Corrected typo from 'dvol="docker volumn"'
alias dser='docker service' # For Docker Swarm operations
# dsp: 'docker system --all' from your list is ambiguous.
# Common uses are 'docker system df' or 'docker system prune -a'.
# Defaulting to 'df' (disk usage). Change if you prefer prune.
alias dsp='docker system df'
# alias dsp='docker system prune -a' # Alternative: prune all unused docker resources
# alias dsp='docker system prune -a --volumes' # Alternative: prune including volumes
# Note: 'docker machine' (dm, dmx) is largely deprecated in favor of Docker Desktop.
# If you still use it, uncomment these:
# alias dm='docker machine'
# alias dmx='docker machine ssh'
# --- Docker Compose ---
# Your Gist used 'dc="docker compose"', initial list had 'dc="dc-fn"'.
# Using 'docker compose' for consistency with other dc* aliases.
# If 'dc-fn' is a custom function you need, you might rename this alias or the function.
alias dc='docker compose'
alias dcbu='docker compose up --build -d' # Build and run in detached mode
alias dcu='docker compose up -d' # Run in detached mode
alias dcd='docker compose down' # Stop and remove containers, networks
alias dcl='docker compose logs -f' # Follow logs
alias dcps='docker compose ps' # List containers for services
# Updated to use 'docker compose' consistently (V2 syntax).
alias dcpu='docker compose pull && docker compose up -d' # Pull images and update services
# --- Aliases calling Docker functions (defined in .bash_functions.sh) ---
# These expect corresponding functions (e.g., daws -> d-aws-cli-fn)
alias daws='d-aws-cli-fn' # For AWS CLI in Docker
# dbash: Your initial list had 'dbash="docker exec -it"'.
# Your Gist had 'alias dbash="docker exec -it"' and also a function:
# 'function dbash() { docker exec -it "$@" bash; }'
# The 'dex-fn' function (see .bash_functions.sh) is more robust and achieves the same.
# 'dex-fn container_name' will exec into bash.
# 'dex-fn container_name other_command' will exec other_command.
alias dbash='dex-fn'
alias dcr='dcr-fn' # docker compose run (function)
alias dex='dex-fn' # docker exec (function)
alias di='di-fn' # docker inspect (function)
alias dip='dip-fn' # display Docker container IPs (function)
alias dl='dl-fn' # docker logs -f (function)
alias dnames='dnames-fn' # list running container names (function)
alias drmc='drmc-fn' # remove exited containers (function)
alias drmid='drmid-fn' # remove dangling/unused images (function)
alias drun='drun-fn' # docker run -it --rm (function)
alias dsr='dsr-fn' # docker stop and rm (function)
alias dstats='dstats-fn' # docker stats (function)
alias dtop='dtop-fn' # docker top (function)
# ~/.tmux-aliases.sh
# tmux-related shell aliases
# Attaches tmux to the last session; creates a new session if none exists.
alias t='tmux attach || tmux new-session'
# Attaches tmux to a specific session (example: ta work)
alias ta='tmux attach -t'
# Creates a new tmux session
alias tn='tmux new-session'
# Lists all ongoing tmux sessions
alias tls='tmux list-sessions'
# Start configuration added by Zim install {{{
#
# This is not sourced during shell startup, and it's only used to configure the
# zimfw plugin manager.
#
#
# Modules
#
# Sets sane Zsh built-in environment options.
zmodule environment
# Provides handy git aliases and functions.
zmodule git
# Applies correct bindkeys for input events.
zmodule input
# Sets a custom terminal title.
zmodule termtitle
# Utility aliases and functions. Adds colour to ls, grep and less.
zmodule utility
#
# Prompt
#
# Exposes to prompts how long the last command took to execute, used by asciiship.
zmodule duration-info
# Exposes git repository status information to prompts, used by asciiship.
zmodule git-info
# A heavily reduced, ASCII-only version of the Spaceship and Starship prompts.
zmodule asciiship
#
# Completion
#
# Additional completion definitions for Zsh.
zmodule zsh-users/zsh-completions --fpath src
# Enables and configures smart and extensive tab completion.
# completion must be sourced after all modules that add completion definitions.
zmodule completion
zmodule sorin
zmodule prompt-pwd
#
# Modules that must be initialized last
#
# Fish-like syntax highlighting for Zsh.
# zsh-users/zsh-syntax-highlighting must be sourced after completion
zmodule zsh-users/zsh-syntax-highlighting
# Fish-like history search (up arrow) for Zsh.
# zsh-users/zsh-history-substring-search must be sourced after zsh-users/zsh-syntax-highlighting
zmodule zsh-users/zsh-history-substring-search
# Fish-like autosuggestions for Zsh.
zmodule zsh-users/zsh-autosuggestions
# }}} End configuration added by Zim install
# Start configuration added by Zim install {{{
#
# User configuration sourced by interactive shells
#
# -----------------
# Zsh configuration
# -----------------
#
# History
#
# Remove older command from the history if a duplicate is to be added.
setopt HIST_IGNORE_ALL_DUPS
#
# Input/output
#
# Set editor default keymap to emacs (`-e`) or vi (`-v`)
bindkey -e
# Prompt for spelling correction of commands.
setopt CORRECT
# Customize spelling correction prompt.
# SPROMPT='zsh: correct %F{red}%R%f to %F{green}%r%f [nyae]? '
# Remove path separator from WORDCHARS.
WORDCHARS=${WORDCHARS//[\/]}
# -----------------
# Zim configuration
# -----------------
# Use degit instead of git as the default tool to install and update modules.
#zstyle ':zim:zmodule' use 'degit'
# --------------------
# Module configuration
# --------------------
#
# git
#
# Set a custom prefix for the generated aliases. The default prefix is 'G'.
#zstyle ':zim:git' aliases-prefix 'g'
#
# input
#
# Append `../` to your input for each `.` you type after an initial `..`
#zstyle ':zim:input' double-dot-expand yes
#
# termtitle
#
# Set a custom terminal title format using prompt expansion escape sequences.
# See http://zsh.sourceforge.net/Doc/Release/Prompt-Expansion.html#Simple-Prompt-Escapes
# If none is provided, the default '%n@%m: %~' is used.
#zstyle ':zim:termtitle' format '%1~'
#
# zsh-autosuggestions
#
# Disable automatic widget re-binding on each precmd. This can be set when
# zsh-users/zsh-autosuggestions is the last module in your ~/.zimrc.
ZSH_AUTOSUGGEST_MANUAL_REBIND=1
# Customize the style that the suggestions are shown with.
# See https://github.com/zsh-users/zsh-autosuggestions/blob/master/README.md#suggestion-highlight-style
#ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE='fg=242'
#
# zsh-syntax-highlighting
#
# Set what highlighters will be used.
# See https://github.com/zsh-users/zsh-syntax-highlighting/blob/master/docs/highlighters.md
ZSH_HIGHLIGHT_HIGHLIGHTERS=(main brackets)
# Customize the main highlighter styles.
# See https://github.com/zsh-users/zsh-syntax-highlighting/blob/master/docs/highlighters/main.md#how-to-tweak-it
#typeset -A ZSH_HIGHLIGHT_STYLES
#ZSH_HIGHLIGHT_STYLES[comment]='fg=242'
# ------------------
# Initialize modules
# ------------------
ZIM_HOME=${ZDOTDIR:-${HOME}}/.zim
# Download zimfw plugin manager if missing.
if [[ ! -e ${ZIM_HOME}/zimfw.zsh ]]; then
if (( ${+commands[curl]} )); then
curl -fsSL --create-dirs -o ${ZIM_HOME}/zimfw.zsh \
https://github.com/zimfw/zimfw/releases/latest/download/zimfw.zsh
else
mkdir -p ${ZIM_HOME} && wget -nv -O ${ZIM_HOME}/zimfw.zsh \
https://github.com/zimfw/zimfw/releases/latest/download/zimfw.zsh
fi
fi
# Install missing modules, and update ${ZIM_HOME}/init.zsh if missing or outdated.
if [[ ! ${ZIM_HOME}/init.zsh -nt ${ZIM_CONFIG_FILE:-${ZDOTDIR:-${HOME}}/.zimrc} ]]; then
source ${ZIM_HOME}/zimfw.zsh init
fi
# Initialize modules.
source ${ZIM_HOME}/init.zsh
# ------------------------------
# Post-init module configuration
# ------------------------------
#
# zsh-history-substring-search
#
zmodload -F zsh/terminfo +p:terminfo
# Bind ^[[A/^[[B manually so up/down works both before and after zle-line-init
for key ('^[[A' '^P' ${terminfo[kcuu1]}) bindkey ${key} history-substring-search-up
for key ('^[[B' '^N' ${terminfo[kcud1]}) bindkey ${key} history-substring-search-down
for key ('k') bindkey -M vicmd ${key} history-substring-search-up
for key ('j') bindkey -M vicmd ${key} history-substring-search-down
unset key
# }}} End configuration added by Zim install
# Keypad
# 0 . Enter
bindkey -s "^[Op" "0"
bindkey -s "^[On" "."
bindkey -s "^[OM" "^M"
# 1 2 3
bindkey -s "^[Oq" "1"
bindkey -s "^[Or" "2"
bindkey -s "^[Os" "3"
# 4 5 6
bindkey -s "^[Ot" "4"
bindkey -s "^[Ou" "5"
bindkey -s "^[Ov" "6"
# 7 8 9
bindkey -s "^[Ow" "7"
bindkey -s "^[Ox" "8"
bindkey -s "^[Oy" "9"
# + - * /
bindkey -s "^[OQ" "/"
bindkey -s "^[OR" "*"
bindkey -s "^[OS" "-"
bindkey -s "^[Ol" "+"
#END Keypad
# Load custom aliases and functions
if [ -f ~/.aliases ]; then
source ~/.aliases
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment