Last active
August 28, 2016 16:37
-
-
Save ckorn/4999102 to your computer and use it in GitHub Desktop.
Bash auto-completion with aliases. Aliases are nice to shorten often used commands. But ever got frustrated when bash's tab completion did not work? Instead of "git checkout" I just type "go". But I also want to type "go m<tab>" to get completed to "go master" automatically.
This script creates a wrapper function to be used with the alias. (Also…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/usr/local/bin/make-completion-wrapper: | |
#!/bin/sh | |
# Author.: Ole J | |
# Date...: 23.03.2008 | |
# License: Whatever | |
# Wraps a completion function | |
# make-completion-wrapper <actual completion function> <name of new func.> <alias> | |
# <command name> <list supplied arguments> | |
# eg. | |
# alias agi='apt-get install' | |
# make-completion-wrapper _apt_get _apt_get_install apt-get install | |
# defines a function called _apt_get_install (that's $2) that will complete | |
# the 'agi' alias. (complete -F _apt_get_install agi) | |
# | |
#make-completion-wrapper _git_checkout _git_checkout_shortcut go git checkout | |
function make-completion-wrapper () { | |
local comp_function_name="$1" | |
local function_name="$2" | |
local alias_name="$3" | |
local arg_count=$(($#-4)) | |
shift 3 | |
local args="$*" | |
local function=" | |
function $function_name { | |
COMP_LINE=\"$@\${COMP_LINE#$alias_name}\" | |
let COMP_POINT+=$((${#args}-${#alias_name})) | |
((COMP_CWORD+=$arg_count)) | |
COMP_WORDS=("$@" \"\${COMP_WORDS[@]:1}\") | |
local cur words cword prev | |
_get_comp_words_by_ref -n =: cur words cword prev | |
"$comp_function_name" | |
return 0 | |
}" | |
eval "$function" | |
# echo $function_name | |
# echo "$function" | |
} | |
__git_shortcut () { | |
# Because cherry-pick has the function _git_cherry_pick | |
n2=${2//-/_} | |
type _git_${n2}_shortcut &>/dev/null || make-completion-wrapper _git_$n2 _git_${n2}_shortcut $1 git $2 | |
complete -o bashdefault -o default -o nospace -F _git_${n2}_shortcut $1 | |
} | |
__apt_cache_shortcut () { | |
type _apt_cache_$2_shortcut &>/dev/null || make-completion-wrapper _apt_cache _apt_cache_$2_shortcut $1 apt-cache $2 | |
# is not executed automatically. Normally only on first apt-cache <tab> | |
[ ! -f /usr/share/bash-completion/completions/apt-cache ] || source /usr/share/bash-completion/completions/apt-cache | |
complete -F _apt_cache_$2_shortcut $1 | |
} | |
$HOME/.bash_aliases: | |
alias qhe="VISUAL=nano quilt header -e" | |
alias gs='git status ' | |
alias ga='git add ' | |
alias gb='git branch ' | |
alias gc='git commit' | |
alias gd='git diff' | |
alias gds='git diff --staged' | |
alias go="git checkout" | |
alias gcp='git cherry-pick' | |
alias apt-policy="apt-cache policy" | |
alias apt-show="apt-cache show" | |
$HOME/.bashrc: | |
if [ -f /usr/local/bin/make-completion-wrapper ]; then | |
. /usr/local/bin/make-completion-wrapper | |
# qhe | |
make-completion-wrapper _quilt_completion _qhe qhe quilt header -e | |
complete -o filenames -F _qhe qhe | |
# apt-policy | |
__apt_cache_shortcut apt-policy policy | |
# apt-show | |
__apt_cache_shortcut apt-show show | |
# __git_shortcut gs status # no completion for git status | |
__git_shortcut ga add | |
__git_shortcut gb branch | |
__git_shortcut gc commit | |
__git_shortcut gd diff | |
__git_shortcut go checkout | |
__git_shortcut gcp cherry-pick | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment