Skip to content

Instantly share code, notes, and snippets.

@kota65535
Forked from yonchu/chpwd_for_zsh.sh
Created November 26, 2016 08:11
Show Gist options
  • Save kota65535/3fff9cb30485641a11931fc037f705f7 to your computer and use it in GitHub Desktop.
Save kota65535/3fff9cb30485641a11931fc037f705f7 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