Skip to content

Commit 0905165

Browse files
committed
Allow z -b to do fuzzy matching
If `z -b foo bar` finds no exact match in the path to the current directory, it tries a "fuzzy" substitution with a frecent directory. For example, if we are in `~/github/jekyll/test` and run `z -b jek gh`, it will try to substitute the entire `jekyll` path component with `gh`. The result will be equivalent to running `z ~/github gh test`.
1 parent 3009e33 commit 0905165

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,7 @@ New option `"-b"` can quickly go back to a specific parent directory in bash ins
319319
- **(No argument)**: `cd` into the project root, the project root the nearest parent directory with `.git`/`.hg`/`.svn` in it.
320320
- **(One argument)**: `cd` into the closest parent starting with keyword, if not find, go to the parent containing keyword.
321321
- **(Two arguments)**: replace the first value with the second one (in the current path).
322+
If simple substitution does not work, falls back to fuzzily replacing path components.
322323

323324
Let's start by aliasing `z -b` to `zb`:
324325

@@ -338,6 +339,11 @@ Let's start by aliasing `z -b` to `zb`:
338339
# substitute jekyll with ghost
339340
~/github/jekyll/test$ zb jekyll ghost
340341
=> cd ~/github/ghost/test
342+
343+
# same as above, but fuzzy
344+
~/github/jekyll/test$ zb jek gh
345+
=> z ~/github/ gh /test
346+
=> cd ~/github/ghost/test # Assuming that's the most frecent match
341347
```
342348

343349
Backward jumping can also be used with `$_ZL_ECHO` option (echo $PWD after cd), which makes it possible to combine them with other tools without actually changing the working directory (eg. ``ls `zb git` ``).

z.lua

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1759,7 +1759,7 @@ function cd_backward(args, options, pwd)
17591759
end
17601760
return nil
17611761
end
1762-
else
1762+
elseif nargs == 2 then
17631763
local test = windows and pwd:gsub('\\', '/') or pwd
17641764
local src = args[1]
17651765
local dst = args[2]
@@ -1770,10 +1770,21 @@ function cd_backward(args, options, pwd)
17701770
if not start then
17711771
return pwd
17721772
end
1773+
17731774
local lhs = pwd:sub(1, start - 1)
17741775
local rhs = pwd:sub(ends + 1)
1775-
return lhs .. dst .. rhs
1776+
local newpath = lhs .. dst .. rhs
1777+
if os.path.isdir(newpath) then
1778+
return newpath
1779+
end
1780+
1781+
lhs = lhs:gsub("[^/]*$", "")
1782+
rhs = rhs:gsub("^[^/]*", "")
1783+
return z_cd({lhs, dst, rhs})
17761784
end
1785+
1786+
io.stderr:write("Error: " .. Z_CMD .. " -b takes at most 2 arguments.\n")
1787+
return nil
17771788
end
17781789

17791790

@@ -1922,7 +1933,7 @@ function main(argv)
19221933
if options['--cd'] or options['-e'] then
19231934
local path = ''
19241935
if options['-b'] then
1925-
if Z_INTERACTIVE == 0 then
1936+
if #args > 0 or Z_INTERACTIVE == 0 then
19261937
path = cd_backward(args, options)
19271938
else
19281939
path = cd_breadcrumbs('', Z_INTERACTIVE)

0 commit comments

Comments
 (0)