Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for using pushd instead of cd #312

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions README
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ Z(1) User Commands Z(1)


NAME
z - jump around
z|zz - jump around

SYNOPSIS
z [-chlrtx] [regex1 regex2 ... regexn]
{z|zz} [-chlprtx] [regex1 regex2 ... regexn]

AVAILABILITY
bash, zsh
Expand All @@ -20,6 +20,9 @@ DESCRIPTION

For example, z foo bar would match /foo/bar but not /bar/foo.

zz command is an alias to z -p. It behaves exactly like z, only uses
pushd instead of cd.

OPTIONS
-c restrict matches to subdirectories of the current directory

Expand All @@ -29,6 +32,9 @@ OPTIONS

-l list only

-p Use pushd instead of cd. zz command has this option set by de-
fault

-r match by rank only

-t match by recent access only
Expand All @@ -46,6 +52,8 @@ EXAMPLES

z -l foo list all dirs matching foo (by frecency)

zz foo pushd to most frecent dir matching foo

NOTES
Installation:
Put something like this in your $HOME/.bashrc or $HOME/.zshrc:
Expand All @@ -57,7 +65,8 @@ NOTES
PROFIT!!

Optionally:
Set $_Z_CMD to change the command name (default z).
Set $_Z_CMD to change the "cd" command name (default z).
Set $_ZZ_CMD to change the "pushd" command name (default zz).
Set $_Z_DATA to change the datafile (default $HOME/.z).
Set $_Z_MAX_SCORE lower to age entries out faster (default
9000).
Expand Down Expand Up @@ -110,6 +119,9 @@ ENVIRONMENT
The contents of the variable $_Z_CMD is aliased to _z 2>&1. If not set,
$_Z_CMD defaults to z.

In similar fashion, the contents of the variable $_ZZ_CMD is aliased to
_z -p 2>&1. If not set, $_ZZ_CMD defaults to zz.

The environment variable $_Z_DATA can be used to control the datafile
location. If it is not defined, the location defaults to $HOME/.z.

Expand Down
20 changes: 17 additions & 3 deletions z.1
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
.TH "Z" "1" "January 2013" "z" "User Commands"
.SH
NAME
z \- jump around
z|zz \- jump around
.SH
SYNOPSIS
z [\-chlrtx] [regex1 regex2 ... regexn]
{z|zz} [\-chlprtx] [regex1 regex2 ... regexn]
.SH
AVAILABILITY
bash, zsh
Expand All @@ -16,6 +16,8 @@ After a short learning phase, \fBz\fR will take you to the most 'frecent'
directory that matches ALL of the regexes given on the command line, in order.

For example, \fBz foo bar\fR would match \fB/foo/bar\fR but not \fB/bar/foo\fR.

\fBzz\fR command is an alias to \fBz -p\fR. It behaves exactly like z, only uses pushd instead of cd.
.SH
OPTIONS
.TP
Expand All @@ -31,6 +33,9 @@ show a brief help message
\fB\-l\fR
list only
.TP
\fB\-p\fR
Use \fBpushd\fR instead of \fBcd\fR. zz command has this option set by default
.TP
\fB\-r\fR
match by rank only
.TP
Expand All @@ -55,6 +60,9 @@ cd to most recently accessed dir matching foo
.TP 14
\fBz -l foo\fR
list all dirs matching foo (by frecency)
.TP 14
\fBzz foo\fR
pushd to most frecent dir matching foo
.SH
NOTES
.SS
Expand All @@ -72,7 +80,10 @@ PROFIT!!
.P
Optionally:
.RS
Set \fB$_Z_CMD\fR to change the command name (default \fBz\fR).
Set \fB$_Z_CMD\fR to change the "cd" command name (default \fBz\fR).
.RE
.RS
Set \fB$_ZZ_CMD\fR to change the "pushd" command name (default \fBzz\fR).
.RE
.RS
Set \fB$_Z_DATA\fR to change the datafile (default \fB$HOME/.z\fR).
Expand Down Expand Up @@ -139,6 +150,9 @@ A function \fB_z()\fR is defined.
The contents of the variable \fB$_Z_CMD\fR is aliased to \fB_z 2>&1\fR. If not
set, \fB$_Z_CMD\fR defaults to \fBz\fR.
.P
In similar fashion, the contents of the variable \fB$_ZZ_CMD\fR is aliased to
\fB_z -p 2>&1\fR. If not set, \fB$_ZZ_CMD\fR defaults to \fBzz\fR.
.P
The environment variable \fB$_Z_DATA\fR can be used to control the datafile
location. If it is not defined, the location defaults to \fB$HOME/.z\fR.
.P
Expand Down
13 changes: 9 additions & 4 deletions z.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
# * z -c foo # restrict matches to subdirs of $PWD
# * z -x # remove the current directory from the datafile
# * z -h # show a brief help message
# * zz [all of the above] # same as with "z", but use pushd instead of cd

[ -d "${_Z_DATA:-$HOME/.z}" ] && {
echo "ERROR: z.sh's datafile (${_Z_DATA:-$HOME/.z}) is a directory."
Expand Down Expand Up @@ -118,14 +119,16 @@ _z() {

else
# list/go
local echo fnd last list opt typ
local echo fnd last list opt typ cd_cmd
cd_cmd="cd"
while [ "$1" ]; do case "$1" in
--) while [ "$1" ]; do shift; fnd="$fnd${fnd:+ }$1";done;;
-*) opt=${1:1}; while [ "$opt" ]; do case ${opt:0:1} in
c) fnd="^$PWD $fnd";;
e) echo=1;;
h) echo "${_Z_CMD:-z} [-cehlrtx] args" >&2; return;;
h) echo "{${_Z_CMD:-z}|${_ZZ_CMD:-zz}} [-cehlprtx] args" >&2; return;;
l) list=1;;
p) cd_cmd="pushd";;
r) typ="rank";;
t) typ="recent";;
x) sed -i -e "\:^${PWD}|.*:d" "$datafile";;
Expand All @@ -137,7 +140,7 @@ _z() {
# if we hit enter on a completion just go there
case "$last" in
# completions will always start with /
/*) [ -z "$list" -a -d "$last" ] && builtin cd "$last" && return;;
/*) [ -z "$list" -a -d "$last" ] && builtin $cd_cmd "$last" && return;;
esac

# no file yet
Expand Down Expand Up @@ -216,7 +219,7 @@ _z() {

if [ "$?" -eq 0 ]; then
if [ "$cd" ]; then
if [ "$echo" ]; then echo "$cd"; else builtin cd "$cd"; fi
if [ "$echo" ]; then echo "$cd"; else builtin $cd_cmd "$cd"; fi
fi
else
return $?
Expand All @@ -225,6 +228,7 @@ _z() {
}

alias ${_Z_CMD:-z}='_z 2>&1'
alias ${_ZZ_CMD:-zz}='_z -p 2>&1'

[ "$_Z_NO_RESOLVE_SYMLINKS" ] || _Z_RESOLVE_SYMLINKS="-P"

Expand Down Expand Up @@ -258,6 +262,7 @@ elif type complete >/dev/null 2>&1; then
# bash
# tab completion
complete -o filenames -C '_z --complete "$COMP_LINE"' ${_Z_CMD:-z}
complete -o filenames -C '_z --complete "$COMP_LINE"' ${_ZZ_CMD:-zz}
[ "$_Z_NO_PROMPT_COMMAND" ] || {
# populate directory list. avoid clobbering other PROMPT_COMMANDs.
grep "_z --add" <<< "$PROMPT_COMMAND" >/dev/null || {
Expand Down