読者です 読者をやめる 読者になる 読者になる

もうこれなくしては、bashは使えないカラダになってしまったbash_completionがバージョンアップ

Linux Solaris bash

1年3ヶ月ぶりの2011/01/16にbash_completionが1.2にバージョンアップした。
もうこれなくしては、bashは使えないカラダになってしまった。
bash programmable completion Recent 【releases 1.2 16 Jan 2011 19:34】

CHANGLOGは、機能追加とバグフィックは分けて欲しいな。。。

bash-completion (1.2)

[ David Paleino ]
* Don't use pidof in _known_hosts_real() to detect whether Avahi is
available, since it's not available on MacOS X. Thanks to Rainer
Muller (bash-completion MacPorts maintainer)
* Fixed "freq" and "rate" completion for iwconfig
* contrib/munin-node fixed (Debian: #550943)
* contrib/dpkg fixed -W and --show completing on .?(u)deb's (Debian: #552109)
* contrib/aptitude: add @(add|remove)-user-tag
* Added munindoc completion to contrib/munin-node, thanks to Tom
Feiner (Debian: #553371)
* Added colordiff completion, same as diff
* contrib/cpio: added missing completions for -?, --help, --license, --usage,
--version and (-p) --to-stdout (Debian: #557436)
* Style policy: don't use fancy globbing in case labels
* Added .fdf completion to okular and evince
* Added .okular completion to okular (Debian: #545530)
* Added lintian completion
* Refreshed reportbug completion, added --from-buildd (Debian: #579471)
* Special-case "apt-get source" (Debian: #572000)
* Added lintian completion (Debian: #547361)
* contrib/dpkg: update completion to current API
* Styleguide: establish line wrapping and $() instead of ``

[ Ville Skytta ]
* Create bz2 dist tarball too.
* Include CHANGES in dist tarball.
* Include profile snippet in tarball, install it.
* Rename contrib/bluez-utils to contrib/bluez to follow bluez 4.x naming.
* Apply cardctl completion to pccardctl too.
* Apply pine completion to alpine too.
* Remove many unnecessary short option completions where long ones exist.
* Improve chsh, chgrp, chown, configure, curl, cvs, find, gkrellm, gzip,
iconv, lftp, look, lzma, make, man, mdadm, modprobe, mount, mplayer,
mysqladmin, perldoc, rsync, screen, service, scp, ssh, sshfs, unzip,
update-alternatives, vncviewer, wget, yp-tools, xine based players' and
general hostname completions.
* Add abook and wtf completion, based on work by Raphael Droz.
* Add cvsps, dragon, fusermount, jarsigner, k3b, lftpget, modplug123,
pm-utils, rtcwake, pack200, unpack200, pbzip2, pbunzip2, pbzcat, pigz,
unpigz, and wol completions.
* Don't overwrite other host completions when completing from multiple
SSH known hosts files.
* Speed up installed rpm package completion on SUSE, based on work by
Marco Poletti (Alioth: #312021).
* Improve sourcing snippets from completion dirs.
* Drop support for bash < 3. The compatiblity global variables $bashN,
$default, $dirnames, $filenames, $compopt, $nospace, $bashdefault, and
$plusdirs have been dropped too. 3rd party completions should switch
to using the complete/compgen features directly, and BASH_VERSINFO
for bash version checks.
* Protect various completions from unusual user input by not embedding the
input in external command arguments (Debian: #552631).
* Add /sbin to $PATH when invoking ifconfig and iwconfig.
* Combine dcop and qdbus completions into the latter.
* awk and sed usage portability fixes (Alioth: #311393, Debian: #501479).
* Fix leaking local variables from various completions.
* Turn on -o filenames in _filedir on bash >= 4.
* Deprecate modules completion, upstream modules >= 3.2.7 ships one.
* Protect grep invocations from user aliases (Alioth: #312143).
* Split sshfs completion from contrib/ssh into contrib/sshfs.
* Split mount and umount completion into contrib/mount.
* Split service completion into contrib/service.
* Split chown, chgrp, and id completions into contrib/coreutils.
* Split kill, look, and renice completions into contrib/util-linux.
* Split killall, pkill, pgrep and related completions into contrib/procps.
* Split ipsec completion into contrib/ipsec.
* Split ifup and ifdown completions into contrib/ifupdown.
* Do basic HTML file completion with Firefox and Chrome and friends,
and Epiphany.
* Do basic diff/patch completion with cdiff and kompare.
* Don't install mock completion by default, it's in upstream mock > 1.1.0.
* Do basic text editor completion with xemacs, sxemacs, kate, and kwrite.
* Do meta-command completion for aoss and padsp.

[ Freddy Vulto ]
* Prevent root PATH expansion prolifering in _root_command (bash >= 4.1.4)
* Only complete xhost if (_)xhost is available.
* Added _get_comp_words_by_ref to replace both _get_cword & _get_pword.
Also additional variables `words' and `cword' can be returned.
* Added _upvar & _upvars helper functions to aid in passing variables
by reference.
* Make _filedir emulate `-o filenames'
* Fixed completion perl modules containing colons.
* Merged __get_cword3 & __get_cword4 to _get_cword.
* Added __expand_tilde_by_ref helper function.
* Added __ltrim_colon_completions to fix completions containing colons
* Improved mutt completion
* Added _get_pword helper function, thanks to Sung Pae (Alioth: #312030)
* Improved mutt completion
* Added _get_pword helper function, thanks to Sung Pae (Alioth: #312030)

[ Ted Stern ]
* Fix modules completion for "(default)" entries.

[ Jeremie Lasalle Ratelle ]
* Fix rsync remote path completion (Alioth: #312173, Gentoo: #297818).

[ Leonard Crestez ]
* Improve ssh -o suboption completion (Alioth: #312122).
* Fix NFS mounts completion (Alioth: #312285).
* Fix completion of usernames (Alioth: #311396, Debian: #511788).
* Fix chown test crashing on systems with no root group (Alioth: #312306).
* Fixed tests when BASH_COMPLETION or TESTDIR contain spaces.
* Fix mount handling of escapes (Alioth: #311410, Launchpad: #219971,
Debian: #511149).
* Cleanup scripts to run tests. Make runUnit and runCompletion use test/run.
Make it possible to run tests from any directory.
* Add a --debug-xtrace option to test/run using BASH_XTRACEFD from bash-4.1.
* Add a --timeout option to test/run to override the default expect timeout.

[ Raphael Droz ]
* Add xsltproc completion (Alioth: #311843).

[ Adrian Friedli ]
* Add ipv6calc completion.

[ Ildar Mulyukov ]
* Add showmount completion (Alioth: #312285).

[ Neville Gao ]
* Fix mount completion error "bash: [: too many arguments" (Alioth #312381).

[ Austin English ]
* Make lookup of wine file completions case insensitive.

[ Igor Murzov ]
* Improve xz completion (Alioth: #312466).

[ Mario Schwalbe ]
* Update find completion (Alioth: #312491, Launchpad: #570113).

[ Mark van Rossum ]
* Add basic lyx completion.
* Add basic lyx completion.

-- David Paleino Wed, 16 Jun 2010 17:44:59 +0200


bash_completionを知ったのは、2007年の秋頃。
2006年の秋ごろからだろうか、「zsh最強!!」というのをブログでよく見かけるようになった。
マイコミジャーナルでも以下のコラムで連載として取り上げられた。
漢のzsh 最強のシェル、それは「zsh」


この辺を見ながらzsh便利そうだなぁー、乗り換えようかな。でも、使うテスト環境たくさんあるし、
zsh入ってないところもあるし、なかなか乗り換えられそうにはないなと諦めてた。


でも、SoftDesignの2007/10月号の巻末特集を見てうれしくなった。
『さらに使いこなすためのbash 逆引き実践TIPS集 4.コマンドを効率よく入力する』
で、bash_completionなる便利なものがあると知った。


bash_completionで、zshと同じようにコマンドのオプションを補完してくれるのだ。

設定

ダンロード

Bash Completion Official Homepage
bash-completion-1.2.tar.gzをダウンロード

[oc@centos5 PKG]$ wget http://bash-completion.alioth.debian.org/files/bash-completion-1.2.tar.gz
解凍
[oc@centos5 PKG]$ tar xvf bash-completion-1.2.tar.gz
make
[oc@centos5 PKG]$ cd bash-completion-1.2
[oc@centos5 bash-completion-1.2]$ ./configure
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for a thread-safe mkdir -p... /bin//mkdir -p
checking for gawk... gawk
checking whether make sets $(MAKE)... yes
configure: creating ./config.status
config.status: creating Makefile
config.status: creating test/Makefile
[oc@centos5 bash-completion-1.2]$ make
Making all in test
make[1]: ディレクトリ `/home/oc/work/PKG/bash-completion-1.2/test' に入ります
make[1]: `all' に対して行うべき事はありません.
make[1]: ディレクトリ `/home/oc/work/PKG/bash-completion-1.2/test' から出ます
make[1]: ディレクトリ `/home/oc/work/PKG/bash-completion-1.2' に入ります
sed -e 's|@sysconfdir[@]|/usr/local/etc|' <./bash_completion.sh.in >bash_completion.sh
make[1]: ディレクトリ `/home/oc/work/PKG/bash-completion-1.2' から出ます

make install はせずにhomeディレクトリにコピー。

[oc@centos5 bash-completion-1.2]$ cp bash_completion ~/.bash_completion

システムで共有するなら、/etc/bash_completion、/usr/local/etcなど置いて
(新しいのに置き換えて)使用すればいいだろう。
他人の環境に影響することはできるだけしたくないので、homeディレクトリにおいている。



.bashrcに以下を設定
 [ -n "${BASH_COMPLETION}" ] || \
      export BASH_COMPLETION="$HOME/.bash_completion"
  . $BASH_COMPLETION
 


厳密にサポートしているbash 2.04以降かどうかをチェックするには、以下のようにするようだ。
READMEの抄訳(未完)

bash=${BASH_VERSION%.*}; bmajor=${bash%.*}; bminor=${bash#*.}
if [ "$PS1" ] && [ $bmajor -eq 2 ] && [ $bminor '>' 04 ] \
   && [ -f /etc/bash_completion ]; then # interactive shell
        # Source completion code
        . /etc/bash_completion
fi
unset bash bmajor bminor

使い方

コマンドの引数の補完

cdの引数であるディレクトリだけを一覧表示してくれる

[oc@centos5 ~]$ cd
.Trash/          .eshell/         .gconfd/         .gnome2_private/ .mozilla/        .subversion/     Desktop/
.anthy/          .evolution/      .gem/            .gnupg/          .nautilus/       .svn/            bin/
.eggcups/        .fontconfig/     .gnome/          .gstreamer-0.10/ .redhat/         .systemtap/      lib/
.emacs.d/        .gconf/          .gnome2/         .metacity/       .scim/           .w3m/            work/

以下のlsの結果と比較すれば、ディレクトリだけが表示されてることが分かる。

[oc@centos5 ~]$ ls -a -F
./                .bash_logout     .emacs.d/     .gnome2/           .lesshst             .subversion/      lib/
../               .bash_profile    .esd_auth     .gnome2_private/   .metacity/           .svn/             locate.db
.ICEauthority     .bashrc*         .eshell/      .gnupg/            .mozilla/            .systemtap/       work/
.Trash/           .colorsvnrc      .evolution/   .gnuplot_history   .nautilus/           .viminfo
.Xauthority       .cvspass         .fontconfig/  .gstreamer-0.10/   .recently-used.xbel  .w3m/
.anthy/           .dmrc            .gconf/       .gtkrc-1.2-gnome2  .redhat/             .xsession-errors
.bash_completion  .eggcups/        .gconfd/      .home.filelist     .scim/               Desktop/
.bash_dirs        .elisp.filelist  .gem/         .inputrc           .screenrc            bin/
.bash_history*    .emacs*          .gnome/       .kkcrc             .session             dead.letter
コマンドオプションの補完 例1 svn
[oc@centos5 bash-completion-1.2]$ svn
?         checkout  cp        h         log       pd        pl        proplist  rename    status
add       ci        del       help      ls        pdel      plist     propset   resolved  sw
ann       cleanup   delete    import    merge     pe        praise    ps        revert    switch
annotate  co        di        info      mkdir     pedit     propdel   pset      rm        unlock
blame     commit    diff      list      move      pg        propedit  remove    st        up
cat       copy      export    lock      mv        pget      propget   ren       stat      update
コマンドオプションの補完 例2 grep

grepって多すぎて覚えられないよね

[oc@centos5 bash-completion-1.2]$ grep --
--after-context=       --count                --fixed-strings        --max-count=           --silent
--basic-regexp         --devices=             --help                 --mmap                 --unix-byte-offsets
--before-context=      --directories=         --ignore-case          --no-filename          --version
--binary               --exclude-from=        --include=             --no-messages          --with-filename
--binary-files=        --exclude=             --invert-match         --null                 --word-regexp
--byte-offset          --extended-regexp      --label=               --null-data
--color                --file=                --line-buffered        --only-matching
--colour               --files-with-matches   --line-number          --perl-regexp
--context=             --files-without-match  --line-regexp          --regexp=
コマンドオプションの補完 例3 chown

ユーザ名の一覧を表示してくれる

[oc@centos5 bash-completion-1.2]$ chown -R
adm:        dbus:       gopher:     mailnull:   nscd:       postfix:    rpm:        uucp:
apache:     default:    haldaemon:  mysql:      ntp:        postgres:   shutdown:   vcsa:
avahi:      ftp:        halt:       news:       oc:         root:       smmsp:      xfs:
bin:        games:      lp:         nfsnobody:  operator:   rpc:        sshd:
daemon:     gdm:        mail:       nobody:     pcap:       rpcuser:    sync:


ユーザ名までいれると、グループ名の一覧を表示してくれる

[oc@centos5 bash-completion-1.2]$ chown -R oc:
adm        default    gdm        mail       nfsnobody  postdrop   rpm        stapusr    uucp
apache     dip        gopher     mailnull   nobody     postfix    screen     sys        vcsa
avahi      disk       haldaemon  man        nscd       postgres   slocate    tty        wheel
bin        floppy     kmem       mem        ntp        root       smmsp      users      xfs
daemon     ftp        lock       mysql      oc         rpc        sshd       utempter
dbus       games      lp         news       pcap       rpcuser    stapdev    utmp
homeディレクトリの展開
[oc@centos5 bash-completion-1.2]$ cd ~/
.Trash/          .evolution/      .gnome/          .metacity/       .subversion/     bin/
.anthy/          .fontconfig/     .gnome2/         .mozilla/        .svn/            lib/
.eggcups/        .gconf/          .gnome2_private/ .nautilus/       .systemtap/      work/
.emacs.d/        .gconfd/         .gnupg/          .redhat/         .w3m/
.eshell/         .gem/            .gstreamer-0.10/ .scim/           Desktop/
[oc@centos5 bash-completion-1.2]$ cd /home/oc/


~が展開されるのをやめるには以下の設定をする。

if [ -n "${BASH_COMPLETION}" ] || \
      export BASH_COMPLETION="$HOME/.bash_completion"
  . $BASH_COMPLETION
  _expand() {
     :
  } 
fi

自分でbash completionを拡張する

自分でbash completionを拡張することもできる。自分ではやってないので、以下を参考に。

気になるモノ






ジュニア,ヘンリー・S. ウォーレン、Jr.,Henry S. Warren、滝沢 徹、玉井 浩、鈴木 貢、赤池 英夫、葛 毅、藤波 順久

価格: ¥ 3,570
価格は記載時点のものです。購入前にAmazonでご確認ください。