Skip to content

Instantly share code, notes, and snippets.

@ichizok
Last active March 9, 2017 10:33
Show Gist options
  • Select an option

  • Save ichizok/8629dfae3f06f33402cc3fac3ca9b62c to your computer and use it in GitHub Desktop.

Select an option

Save ichizok/8629dfae3f06f33402cc3fac3ca9b62c to your computer and use it in GitHub Desktop.
peco.bash
if type peco &>/dev/null; then
# pd (peco-change-directory)
function pd() {
local dir_tmp=
local dir_path=${1:-.}
local dot_line_show="[show dot]"
local dot_line_hide="[hide dot]"
local dot_line=${dot_line_show}
while true; do
dir_path=$(cd "${dir_path/\/\///}"; pwd)
if ! [[ "${dir_path}" = / ]]; then
dir_path=${dir_path%/}
fi
if [[ "${dir_path}" =~ ^${PWD} ]]; then
dir_path=.${dir_path#${PWD}}
fi
dir_tmp=$(
shopt -s nullglob
dir_list=("${dir_path}"/*/)
if [[ "${dot_line}" = "${dot_line_hide}" ]]; then
dir_list=("${dir_path}"/.*/ "${dir_list[@]}")
fi
{
echo "[cd ${dir_path}]"
if ! [[ "${dir_path}" = / ]]; then
echo ..
fi
for d in "${dir_list[@]%/}"; do
if ! [[ "${d}" =~ /\.\.?$ ]]; then
echo "${d##*/}"
fi
done
echo "${dot_line}"
} | peco --on-cancel error)
if [[ $? -ne 0 ]]; then
return
elif [[ "${dir_tmp}" = "[cd ${dir_path}]" ]]; then
cd "${dir_path}"
return
elif [[ "${dir_tmp}" = "${dot_line}" ]]; then
if [[ "${dot_line}" = "${dot_line_show}" ]]; then
dot_line=${dot_line_hide}
else
dot_line=${dot_line_show}
fi
else
dir_path=${dir_path}/${dir_tmp}
fi
done
}
# select command from history
function peco-select-history() {
local buffer=$(\history | tac | sed 's/^\s*[[:digit:]]\+\s*//' | peco --query "${READLINE_LINE}")
READLINE_LINE=${buffer}
READLINE_POINT=${#buffer}
}
bind -x '"\C-r": peco-select-history'
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment