Skip to main content

Tips and Tricks

This guide shares a collection of useful aliases, and shell customizations to improve your command-line productivity on Linux.

Useful Aliases​

Aliases can significantly shorten commonly used commands.

Kubectl Aliases​

To use these aliases, you need kubectl, kubectx, and kubens installed.

AliasCommandDescription
kkubectlShortens the primary kubectl command.
kckubectxSwitches between Kubernetes contexts.
knkubensSwitches between Kubernetes namespaces.

Add the following lines to your ~/.bashrc or ~/.zshrc file:

alias k='kubectl'
alias kc='kubectx'
alias kn='kubens'

Custom Prompt (PS1)​

Customize your Bash prompt (PS1) to display useful information. Add one of the following examples to your ~/.bashrc file.

Example 1: Git Branch​

This prompt displays the current Git branch and adds custom colors.

PROMPT_COMMAND='__git_ps1 "${debian_chroot:+($debian_chroot)}\[\033[1;31m\]\u\[\033[1;36m\]@\[\033[01;32m\]\h\[\033[01;34m\]:/\W\[\033[00m\]" "\[\033[01;34m\]\$ \[\033[00m\]"'

Example 2: Git, Kubernetes, and Virtual Environment​

This advanced prompt shows the Git branch, active Python virtual environment, and current Kubernetes context and namespace.

info

This script assumes you have ~/.git-prompt.sh configured. It also shows Kubernetes context only when inside a specific project path, which you should customize ($HOME/<project_path>).

enablegitprompt() {
# Source the git-prompt script
if [ -f ~/.git-prompt.sh ]; then
. ~/.git-prompt.sh
fi

# Configure git-prompt settings
GIT_PS1_SHOWCOLORHINTS=true
GIT_PS1_SHOWDIRTYSTATE=true
GIT_PS1_SHOWUNTRACKEDFILES=true
GIT_PS1_SHOWUPSTREAM="auto"

# Function to display the active Kubernetes context
__k8s_cluster_prompt() {
if [[ "$(pwd)" == "$HOME/<project_path>"* ]]; then
local context=$(kubectl config current-context)
if [[ -n "$context" ]]; then
echo "($(echo $context | awk '{print $1}'))"
fi
fi
}

# Function to display the active Kubernetes namespace
__k8s_namespace_prompt() {
if [[ "$(pwd)" == "$HOME/<project_path>"* ]]; then
local ns=$(kubectl config view --minify --output 'jsonpath={..namespace}')
if [[ -n "$ns" ]]; then
echo "/$ns"
fi
fi
}

# Function to display the active Python virtual environment
__venv_prompt() {
if [[ -n "$VIRTUAL_ENV" ]]; then
echo "($(basename "$VIRTUAL_ENV")) "
fi
}

# Assemble the prompt
VENV_PROMPT="\$(__venv_prompt)"
K8S_PROMPT="\$(__k8s_cluster_prompt)\$(__k8s_namespace_prompt)"

PROMPT_COMMAND='__git_ps1 "${VENV_PROMPT}${debian_chroot:+($debian_chroot)}\[\033[1;31m\]\u\[\033[1;36m\]@\[\033[01;32m\]\h\[\033[01;34m\]:/\W\[\033[00m\]" "${K8S_PROMPT} \\\[\033[01;34m\]\$ \\\[\033[00m\]"'
}

enablegitprompt