bash 上的 auto completion

最後更新: 2015-02-23

 

 


Auto completion 的原理

Auto completion 分別是由 Bash 的三個 buildin cmd 去實現的 - "complete, compopt, compgen"

查看現在會 auto 什麼

complete [-p]

complete -F _minimal
complete -F _filedir_xspec oodraw
complete -F _filedir_xspec elinks
complete -F _filedir_xspec freeamp
complete -F _longopt split
complete -F _longopt sed
complete -F _longopt ld
.............................

compgen

# Generate possible completion matches for word according to the options
compgen [option] [word]

compopt

# Modify completion options for each name according to the options
compopt [-o option] [-DE] [+o option] [name]

complete

# Specify how arguments to each name should be completed.

# function "_known_hosts" 會 handle ping

# -F function
# shell function function is executed in the current shell environment
# $1 is the name of the command whose arguments are being completed

complete -F _known_hosts fping

安裝:

apt-get install bash-completion

設定:

在 ~/.bashrc 加上

if [ -f /etc/bash_completion ]; then
 . /etc/bash_completion
fi

 

/etc/bash_completion 內容:

. /usr/share/bash-completion/bash_completion

"/usr/share/bash-completion/bash_completion" 內放了一大堆常用的設定:

  • apache2ctl
  • ssh
  • sshfs
  • smartctl
  • dpkg
  • apt
  • lftp
  • strace
  • rsync
  • tcpdump
  • findutils
  • vzctl
  • mdadm
  • tar
  • mysqladmin
  • nmap
  • dd
  • cryptsetup
  • mutt
  • coreutils
  • mount
  • ipmitool
  • openssl
  • ipsec

後記:

/etc/bash_completion.d/                              <--- 自定 auto completion 的程式(按 tab 有反應)

/usr/share/bash-completion/completions/     <--- default 有的 auto complete program

 


Debug:

 

Error1:

-bash: /dev/fd/60: No such file or directory

原因:

/dev/fd/60 真的不存在

解決方式1:

sudo ln -s /proc/self/fd /dev/fd

解決方式1:

aptitude install udev

 

Error2:

Auto complete not work

原因:

用錯 shell

查看:

server:tim# echo $SHELL

/bin/bash

server:tim# su tim

$ echo $SHELL

/bin/sh

 


系統上的所有 alias

 

compgen -a

cp
l.
ll
ls
mv
rm
which