Skip to content

Instantly share code, notes, and snippets.

@yonchu
Created October 23, 2012 00:41
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save yonchu/3935922 to your computer and use it in GitHub Desktop.
Save yonchu/3935922 to your computer and use it in GitHub Desktop.
zshにてcd後に自動的にlsを行うchpwd関数。ファイル数が多い場合は省略表示します。(.zshrcに設定して使用します)
chpwd() {
ls_abbrev
}
ls_abbrev() {
# -a : Do not ignore entries starting with ..
# -C : Force multi-column output.
# -F : Append indicator (one of */=>@|) to entries.
local cmd_ls='ls'
local -a opt_ls
opt_ls=('-aCF' '--color=always')
case "${OSTYPE}" in
freebsd*|darwin*)
if type gls > /dev/null 2>&1; then
cmd_ls='gls'
else
# -G : Enable colorized output.
opt_ls=('-aCFG')
fi
;;
esac
local ls_result
ls_result=$(CLICOLOR_FORCE=1 COLUMNS=$COLUMNS command $cmd_ls ${opt_ls[@]} | sed $'/^\e\[[0-9;]*m$/d')
local ls_lines=$(echo "$ls_result" | wc -l | tr -d ' ')
if [ $ls_lines -gt 10 ]; then
echo "$ls_result" | head -n 5
echo '...'
echo "$ls_result" | tail -n 5
echo "$(command ls -1 -A | wc -l | tr -d ' ') files exist"
else
echo "$ls_result"
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment