Some Fish Aliases for a More Productive Programming

marounmaroun

Maroun Maroun

Posted on January 12, 2020

Some Fish Aliases for a More Productive Programming

I believe that the shell prompt shouldn't be too much cluttered. Here's how mine looks:

myprompt

It contains the PWD, followed by the current Git branch and the K8s context I'm using. This is achieved by the following ~/.config/fish/config.fish:

function custom_prompt
    set_color normal
    set -l git_branch (git branch 2>/dev/null | sed -n '/\* /s///p')
    set -l kube_context (kubectl config current-context | cut -d. -f1)
    set_color 2eb82e
    set_color cyan
    echo -n (prompt_pwd)
    set_color normal
    echo -n ' ['
    set_color 0087ff
    echo -n "$git_branch"
    if [ ! -z "$git_branch" ]
      set_color normal
      echo -n "/"
    end
    set_color 00b386
    echo -n "$kube_context"
    set_color normal
    echo -n ']'
    set_color cyan
    echo -n '$ '
end

Addiotianly, sometimes I want to hide the prompt in order to get more space in my terminal. For that, I have the following aliases:

function fish_prompt --description "Custom fish prompt"
    custom_prompt
end

function show --description "Show fish prompt"
    function fish_prompt
        custom_prompt
    end
end

function hide --description "Hide fish prompt"
    function fish_prompt
        echo -n '> '
    end
end

which gives me the following:

showandhide

Additionally, here are some of my favorite aliases for K8s and Docker:

# switch between K8s contexes
abbr -a -g kubedev kubectl config use-context <dev-context>
abbr -a -g kubestaging kubectl config use-context <staging-context>

# edit deployments
abbr -a -g edit-deployment kubectl edit deployment <deployment>

# exec to your common pod
function exec-pod
    set pod_id (kubectl get pods -l app=<app> -o custom-columns=":metadata.name")
    kubectl exec -it $pod_id bash
end

# display the current deployed image
function current_image
    set image (kubectl get deployment <deployment> -o=jsonpath='{$.spec.template.spec.containers[:1].image}')
    printf "$image\n"
end

# docker stuff
abbr -a -g drm docker rm
abbr -a -g dri docker images
abbr -a -g dexec docker exec -it
abbr -a -g dstopall docker stop (docker ps -q)
abbr -a -g dps docker ps
abbr -a -g rmidang docker rmi -f (docker images -f "dangling=true" -q)

I have the same aliases also for Bash, if someone is interested I'll happily share them.

I'd appreciate it if you share your useful aliases too!

💖 💪 🙅 🚩
marounmaroun
Maroun Maroun

Posted on January 12, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related