macOS ◆ xterm-color ◆ bash 1454 views

use Ctrl+T for k8s CLI

First, this idea is stolen from fzf(https://github.com/junegunn/fzf).

Currently k8s CLI has not perfect bash completion for printing resources, like pods, svcs names. Sometimes we need to copy & paste their names but this is very annoying when using CLI. So, I add shortcut key for bash user and this is for helping to select k8s resources and exactly print to shell.

\<Ctrl+T> is for transposing last two characters. But nobody use this shortcut a lot. ;) So, You can replace \<Ctrl+T> key binding for custom action like example.

  • First you need to install fzf for selecting pod,svc,deployment

    brew install fzf

  • Set this functions and binding key first

    __kgaselect\_() {

    local map=$(kubectl get all --show-labels=true --all-namespaces | grep -v '^NAMESPACE')
    echo "${map}" | fzf -x -e +s --reverse --bind=left:page-up,right:page-down --no-mouse | awk '{print $2" --namespace "$1}'

    } __kgawidget\_() {

    local selected="$(\_\_kga_select\_\_)"
    READLINE_LINE="${READLINE_LINE:0:$READLINE_POINT}$selected${READLINE_LINE:$READLINE_POINT}"
    READLINE_POINT=$(( READLINE_POINT + ${#selected} ))

    }

    bind ‘“\er”: redraw-current-line’ if [ $BASH_VERSINFO -gt 3 ]; then

    bind -x '"\C-t": "\_\_kga_widget\_\_"'

    else

    bind '"\C-t": " \C-u \C-a\C-k\`\_\_kga_select\_\_\`\e\C-e\C-y\C-a\C-y\ey\C-h\C-e\er \C-h"'

    fi

  • Then use like below

    kubectl get \<Ctrl+T> kubectl describe \<Ctrl+T>