zshの補完をさらに強力にするzsh-completionsの設定

普段、iTerm2をデュアルディスプレイの出力側に常に表示させて3分割にして利用しているのですが、zsh-completionsを設定してからサブコマンドを利用するコマンドがとても便利になったので設定方法を記録しておきます。

設定

GitHubで公開されているスクリプト集をダウンロードしましょう。

This projects aims at gathering/developing new completion scripts that are not available in Zsh yet. The scripts are meant to be contributed to the Zsh project when stable enough.

[yuki@beaune.local]$ git clone https://github.com/zsh-users/zsh-completions.git

ダウンロードスクリプトをホームディレクトリに適当にリネームして配置します。

~/.zsh-completions/

最後に「.zshrc」の先頭にリネームして配置したディレクトリ追記しパスを通します。

# zsh-completionsを利用する Github => zsh-completions  
fpath=(~/.zsh-completions $fpath)

尚、.zshrcには下記のような補完関連の設定が既にされている事を想定しています!

autoload -U compinit; compinit
zstyle ':completion:*:sudo:*' command-path /usr/local/sbin /usr/local/bin \
                             /usr/sbin /usr/bin /sbin /bin /usr/X11R6/bin \
                             /usr/local/git/bin

操作方法

私はAndroidSDKに同梱されているadbコマンドをよく使います。adbコマンドもサブコマンドで構成されているので、補完が効いているとlogcatひとつにしても便利です。

例えば

[yuki@beaune.local]$ adb <ここでTAB>

とすると

[yuki@beaune.local]$ adb bugreport 
bugreport       -- return all information from the device
connect         -- connect to a device via TCP/IP
devices         -- list all connected devices
disconnect      -- disconnect from a TCP/IP device
emu             -- run emulator console command
forward         -- forward socket connections
get-serialno       -- prints: <serial-number>
get-state          -- prints: offline | bootloader | device
help            -- show help message
install         -- push this padbage file to the device and install it
jdwp            -- list PIDs of processes hosting a JDWP transport
kill-server        -- kill the server if it is running
logcat          -- view device log
ppp                -- run PPP over USB
pull            -- copy file/dir from device
push            -- copy file/dir to device
reboot             -- reboots the device, optionally into the bootloader or recovery program
reboot-bootloader  -- reboots the device into the bootloader
remount            -- remounts the /system partition on the device read-write
root               -- restarts the adbd daemon with root permissions
shell           -- run remote shell interactively or command
start-server       -- ensure that there is a server running
status-window      -- continuously print device status for a specified device
sync            -- copy host->device only if changed
tcpip              -- restarts the adbd daemon listening on TCP on the specified port
uninstall       -- remove this app padbage from the device
usb                -- restarts the adbd daemon listening on USB
version         -- show version number

という感じでサブコマンド一覧が表示され、さらにタブを押すとサブコマンドがRING循環で表示されます。

さらに、サブコマンドも同様にタブで補完できて

[yuki@beaune.local]$ adb shell <ここでTAB>
zsh: do you wish to see all 162 possibilities (41 lines)? 
acceld                    hostapd                   mm-aenc-omxqcelp13-test   run-as                  
am                        hostapd_cli               mm-audio-native-test      schedtest               
app_process               hostapd_wps               mm-jpeg-dec-test          schedtop                
applypatch                id                        mm-jpeg-enc-test          sdptool                 
athtestcmd                ifconfig                  mm-vdec-omx-property-mgr  sendevent               
battery_charging          iftop                     mm-vdec-omx-test          service                 
bluetoothd                ime                       mm-venc-omx-test          servicemanager          
bmgr                      input                     monkey                    setconsole              
bootanimation             insmod                    mount                     setprop                 
bugreport                 installd                  mtpd                      sh                      
cat                       ioctl                     mv                        sleep                   
chmod                     ionice                    mwmainte                  smd                     
chown                     iperf                     nandread                  start                   
cmp                       iptables                  ndc                       stop                    
cnd                       iwconfig                  netcfg                    surfaceflinger          
dalvikvm                  iwevent                   netd                      svc^M                   
date                      iwgetid                   netmgrd                   sync                    
dbus-daemon               iwlist                    netstat                   system_server           
dd                        iwmulticall               newfs_msdos               tc                      
debuggerd                 iwpriv                    nl_listener               test_diag               
dexopt                    iwspy                     notify                    tgmd                    
df                        keystore                  omx_tests                 tmsd                    
dgstmgrd                  kill                      orientationd              toolbox                 
dhcpcd                    linker                    pand                      top                     
diag_klog                 ln                        ping                      trpd                    
diag_mdlog                loc_api_app               pm                        uimd                    
dmesg                     log                       port-bridge               umount                  
dnsmasq                   logcat                    pppd                      vdc                     
dumpstate                 logwrapper                printenv                  vmstat                  
dumpsys                   ls                        ps                        vold                    
dvz                       lsmod                     qmuxd                     watchprops              
fotad                     mediaserver               racoon                    wipe                    
fsck_msdos                mkdir                     reboot                    wiperiface              
geomagneticd              mm-abl-test               recEvent                  wlan_tool               
getevent                  mm-adec-omxQcelp13-test   renice                    wmiconfig               
getprop                   mm-adec-omxaac-test       rild                      wpa_supplicant          
gzip                      mm-adec-omxevrc-test      rm                                                
handset-keypress          mm-adec-omxmp3-test       rmdir                             

利用できるコマンド一覧が補完されます。adb logcatの場合はFilterの種類が表示され、サブコマンドごとに適切な補完がされていることがわかります。

コマンドの補完だけでなくオプションも補完でき、すぐにオプションを忘れてしまう私のような人には便利です。

[yuki@beaune.local]$ adb logcat -<ここでTAB>
-b                           -- loads an alternate log buffer for viewing, such
-c                           -- clears (flushes) the entire log and exits
-d                           -- dumps the log to the screen and exits
-f                           -- writes log message output to file. The default 
-g                           -- prints the size of the specified log buffer and
-s                           -- sets the default filter spec to silent
-v                           -- sets the output format for log messages

[yuki@beaune.local]$ adb install -<ここでTAB>
-l                           -- forward-lock the app
-r                           -- reinstall the app, keeping its data
-s                           -- install on SD card instead of internal storage

adbコマンドの例をとりましたが他にも色々なコマンドに対応しています。

対応しているコマンド例

わりとよく使うコマンドの一例です。特にmvnコマンドなどはタカタカッターン!!と実行できて気持ちいいです。

コマンド

これ以外に色々もあります。

まとめ

  • 補完便利
  • 補完対象が豊富
  • たまに待ち時間が気になる補完もある
  • 設定が簡単という所が敷居が低くて良い

少しの設定で普段の操作が快適になるので、もしよければ試してみてください!