From d0cf5282dfad1c39c3da135fd4280a14837a711d Mon Sep 17 00:00:00 2001 From: rupa Date: Mon, 25 Jan 2016 19:43:16 -0500 Subject: [PATCH] add -e flag to echo best match only (no cd) -l lists and does not cd, but has enough extra stuff going on to make it a bit of a pain if you want to parse out just the best match for your own nefarious purposes - therefore this. h/t @synthetiv. Closes #161 --- README | 2 ++ z.1 | 3 +++ z.sh | 12 +++++++++--- 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/README b/README index 7de82a4..62b3ae1 100644 --- a/README +++ b/README @@ -23,6 +23,8 @@ DESCRIPTION OPTIONS -c restrict matches to subdirectories of the current directory + -e echo the best match, don't cd + -h show a brief help message -l list only diff --git a/z.1 b/z.1 index cc99910..54f8ad7 100644 --- a/z.1 +++ b/z.1 @@ -22,6 +22,9 @@ OPTIONS \fB\-c\fR restrict matches to subdirectories of the current directory .TP +\fB\-e\fR +echo the best match, don't cd +.TP \fB\-h\fR show a brief help message .TP diff --git a/z.sh b/z.sh index d0eeb97..6b35064 100644 --- a/z.sh +++ b/z.sh @@ -106,11 +106,12 @@ _z() { --) while [ "$1" ]; do shift; local fnd="$fnd${fnd:+ }$1";done;; -*) local opt=${1:1}; while [ "$opt" ]; do case ${opt:0:1} in c) local fnd="^$PWD $fnd";; - h) echo "${_Z_CMD:-z} [-chlrtx] args" >&2; return;; - x) sed -i -e "\:^${PWD}|.*:d" "$datafile";; + e) local nocd=1;; + h) echo "${_Z_CMD:-z} [-cehlrtx] args" >&2; return;; l) local list=1;; r) local typ="rank";; t) local typ="recent";; + x) sed -i -e "\:^${PWD}|.*:d" "$datafile";; esac; opt=${opt:1}; done;; *) local fnd="$fnd${fnd:+ }$1";; esac; local last=$1; [ "$#" -gt 0 ] && shift; done @@ -198,7 +199,12 @@ _z() { } ')" [ $? -gt 0 ] && return - [ "$cd" ] && cd "$cd" + [ "$cd" ] || return + if [ "$nocd" ]; then + echo "$cd" + else + cd "$cd" + fi fi }