1
0
mirror of https://github.com/skywind3000/z.lua synced 2026-03-21 15:19:49 +00:00

27 Commits

Author SHA1 Message Date
skywind3000
6025da3ae5 fix: don't reassign for-loop variable 2026-03-09 21:15:58 +08:00
Linwei
1ebe5bcaa7 Merge pull request #228 from botantony/lua-5.5-patch
fix: don't reassign for-loop variable
2025-12-24 17:47:50 +08:00
botantony
e1eb7a2104 fix: don't reassign for-loop variable
In Lua 5.5 you can no longer reassign for-loop variables, f.e.

```lua
for k, v in pairs(tbl) do k = "foobar" end
```

This PR should fix this problem

Signed-off-by: botantony <antonsm21@gmail.com>
2025-12-23 22:49:39 +01:00
Linwei
f80e22498c Merge pull request #223 from fabiob/patch-1
Adds NixOS instructions to README.md
2025-08-01 14:42:55 +08:00
Fábio D. Batista
ce1ee31529 Adds NixOS instructions to README.md 2025-07-31 18:18:15 -03:00
Linwei
aebea97d8e Merge pull request #222 from brglng/feature/nushell
fix: installation instructions in README.md
2025-05-29 21:52:11 +08:00
Zhaosheng Pan
314dd0a29d fix: installation instructions in README.md 2025-05-29 17:37:09 +08:00
skywind3000
3482460bd2 update version number 2025-05-24 23:05:00 +08:00
Linwei
bcb5599c12 Merge pull request #221 from brglng/feature/nushell
feat(nushell)!: improve nushell support for nushell v0.96+
2025-05-21 23:37:24 +08:00
Zhaosheng Pan
ef8767c5f0 feat(nushell)!: improve nushell support for nushell v0.96+ 2025-05-21 22:45:12 +08:00
skywind3000
23aeabedd9 update version number 2025-05-18 00:18:33 +08:00
Linwei
27dceb9899 Merge pull request #219 from chrisant996/allow_interactive_with_no_args
Fix when _ZL_MATCH_MODE=1.
2025-05-18 00:16:39 +08:00
Chris Antos
19b2d1747e Fix when _ZL_MATCH_MODE=1. 2025-05-17 04:29:40 -07:00
skywind3000
7c1c73af33 1. upgrade internal batch script
2. made _ZL_FZF_HEIGHT available on windows.
2025-05-17 13:57:37 +08:00
Linwei
b63a38b11a Merge pull request #218 from chrisant996/allow_interactive_with_no_args
Make `z -i` and `z -I` with no arguments be interactive.
2025-05-16 15:12:21 +08:00
Chris Antos
c524e28851 Make z -i and z -I with no arguments be interactive.
In skywind3000/z.lua#122 there's a note that z.lua intentionally doesn't
support `z -i` being interactive to maintain consistency with how z.sh
behaves with `z -i`.  But z.sh doesn't have a `-i` option, so I'm not
sure what is the intent.

This commit makes `z -i` and `z -I` go interactive even with no args.
2025-05-15 09:47:19 -07:00
Linwei
c3a182c6c9 Merge pull request #216 from chrisant996/fix_invalid_path_on_window
Fix invalid pathname in `z -I xyz` on Windows.
2025-05-15 22:14:44 +08:00
Chris Antos
771c72de5f Fix invalid pathname in z -I xyz on Windows.
The fzf interactive mode (`-I`) constructs a `tmpname` pathname and
tries to sanitize it by stripping all `\` characters.  But it missed
stripping all `:` characters, and the command printed this error output:

The filename, directory name, or volume label syntax is incorrect.

The `tmpname` looked like "c:\tmp\Temp\zlua_c:tmpTempsh2k0.txt", and
the embedded `:` caused the error output.

This commit adds `:` to the list of characters to be stripped (only on
Windows).
2025-05-14 16:49:50 -07:00
Linwei
4bbd0f1e58 Merge pull request #214 from russell7/patch-1
fix(readme): fix typo
2025-02-21 00:55:49 +08:00
洪运
db1fb1f256 fix(readme): fix typo 2025-02-20 22:46:25 +08:00
Linwei
19cb43d1ac Merge pull request #212 from ShalokShalom/patch-1
Remove Fig
2025-01-14 17:28:48 +08:00
ShalokShalom
787da9512c Remove Fig
Fig is no more (see https://app.fig.io/plugins/z.lua)
2025-01-14 10:23:57 +01:00
skywind3000
3875d774cb fixed nushell completion issue 2024-10-18 10:35:14 +08:00
skywind3000
dff590adc1 update docs 2024-10-18 03:09:39 +08:00
skywind3000
20c0494d44 update docs 2024-10-17 08:04:13 +08:00
skywind3000
67405d57e8 update gitignore 2024-10-16 00:31:50 +08:00
skywind3000
6e67a32520 fix: indentation consistency 2024-10-14 15:42:46 +08:00
5 changed files with 75 additions and 53 deletions

1
.gitignore vendored
View File

@@ -39,3 +39,4 @@ luac.out
*.x86_64
*.hex
/.vscode/*

View File

@@ -14,7 +14,7 @@ z.lua 是一个快速路径切换工具,它会跟踪你在 shell 下访问过
- 性能比 **z.sh** 快三倍,比 **fasd** / **autojump** 快十倍以上。
- 支持 Posix Shellbash, zsh, dash, sh, ash, busybox 等等。
- 支持 Fish ShellPower Shell 和 Windows cmd。
- 支持 Fish ShellNushell, Power Shell 和 Windows cmd。
- 使用增强匹配算法,更准确的带你去到你想去的地方。
- 低占用,能够仅在当前路径改变时才更新数据库(将 `$_ZL_ADD_ONCE` 设成 1
- 交互选择模式,如果有多个匹配结果的话,跳转前允许你进行选择。
@@ -93,7 +93,6 @@ z -b foo # 跳转到父目录中名称以 foo 开头的那一级
然后在 `config.nu` 中加入如下代码:
source ~/.cache/zlua.nu
alias z = _zlua
- Power Shell:
@@ -123,7 +122,7 @@ z -b foo # 跳转到父目录中名称以 foo 开头的那一级
- 设置 `$_ZL_NO_PROMPT_COMMAND` 为 1 来跳过钩子函数初始化(方便自己处理)。
- 设置 `$_ZL_EXCLUDE_DIRS` 逗号分隔的路径列表,列表内的路径不会被收集。
- 设置 `$_ZL_ADD_ONCE` 为 '1' 时,仅在当前路径 `$PWD` 改变时才更新数据库。
- 设置 `$_ZL_MAXAGE` 来确定一个数据老化的值 (默认为 5000)。
- 设置 `$_ZL_MAXAGE` 来确定一个数据老化的值 (默认为 5000)。
- 设置 `$_ZL_CD` 用来指定你想用的 cd 命令,比如有人用 cd_func 。
- 设置 `$_ZL_ECHO` 为 1 可以在跳转后显示目标路径名称。
- 设置 `$_ZL_MATCH_MODE` 为 1 可以打开 “增强匹配模式”。
@@ -212,7 +211,7 @@ Frecency 是一个由 'recent' 和 'frequency' 组成的合成词,这个术语
cd foo
因此,在增强匹配算法中,你总可以 cd 命令一样使用 z 命令,而不必当心目标路径是否被记录过。
因此,在增强匹配算法中,你总可以 cd 命令一样使用 z 命令,而不必当心目标路径是否被记录过。
- 忽略当前路径:
@@ -225,7 +224,7 @@ Frecency 是一个由 'recent' 和 'frequency' 组成的合成词,这个术语
我当然可以每次使用`z env gems` 来精确指明,但是每当我输入 `z xxx` 我必然是想进行路径跳转的,而不是呆在原地,所以使用增强匹配模式,即便当前目录是最佳匹配,它也能懂得你想跳转的心思。
我最初实现 z.lua 时,只有一个和 z.sh 类似的默认匹配算法,在网友的建议下,我陆续学习了来自 fasd / autojump 中的优秀理念,并加以完善改进,成为如今集三家之长的 “增强匹配算法” ,给它取个昵称,叫做 “更懂你的匹配算法”。
我最初实现 z.lua 时,只有一个和 z.sh 类似的默认匹配算法,在网友的建议下,我陆续学习了来自 fasd / autojump 中的优秀理念,并加以完善改进,成为如今集三家之长的 “增强匹配算法” ,给它取个昵称,叫做 “更懂你的匹配算法”。
## Add once
@@ -455,15 +454,13 @@ sys 0m0.030s
描述力强,可以更好的实现核心功能,同时速度更快,纯 shell 开发的话,太多语句是通过子进程 shell 的模式运行,所以性能很差,而 Python 开发的话启动速度又太慢,我在 Cygwin/msys 下用 z.sh 都觉得很卡autojump/fasd 卡到不能用。
最关键的一点Lua 速度很快 200 KB 的可执行程序,启动速度是 python 的 3倍perl 的 2 倍,很多命令行工具 go/rust 写成,动不动就 2MB / 3MB他们都还没有完成加载lua 脚本可能都运行完了。
最关键的一点Lua 速度很快 200 KB 的可执行程序,启动速度是 python 的 3 perl 的 2 倍,很多命令行工具 go/rust 写成,动不动就 2MB / 3MB他们都还没有完成加载lua 脚本可能都运行完了。
## Credit
Releated projects:
- [rupa/z](https://github.com/rupa/z): origin z.sh implementation
- [JannesMeyer/z.ps](https://github.com/JannesMeyer/z.ps): z for powershell
我的推特https://x.com/skywind3000
个人博客: https://skywind.me/blog
## License

View File

@@ -28,7 +28,7 @@ From people using z.lua:
- **10x** times faster than **fasd** and **autojump**, **3x** times faster than **z.sh**.
- Gain the ultimate speed with an optional [native module](https://github.com/skywind3000/czmod) written in C.
- Available for **posix shells**: bash, zsh, dash, sh, ash, ksh, busybox and etc.
- Available for Fish Shell, Power Shell and Windows cmd.
- Available for Fish Shell, Nushell, Power Shell and Windows cmd.
- [Enhanced matching algorithm](#enhanced-matching) takes you to where ever you want precisely.
- Allow updating database only if `$PWD` changed with "$_ZL_ADD_ONCE" set to 1.
- Interactive selection enables you to choose where to go before cd.
@@ -58,15 +58,6 @@ z -b foo bar # replace foo with bar in cwd and cd there
## Install
- Fig (works with all shells)
[Fig](https://fig.io) adds apps, shortcuts, and autocomplete to your existing terminal.
Install `z.lua` in just one click.
<a href="https://fig.io/plugins/other/z.lua" target="_blank"><img src="https://fig.io/badges/install-with-fig.svg" /></a>
- Bash:
put something like this in your `.bashrc`:
@@ -87,6 +78,12 @@ z -b foo bar # replace foo with bar in cwd and cd there
eval "$(lua /path/to/z.lua --init bash enhanced once fzf)"
NixOS users using home-manager can add this to their user profile:
programs.z-lua.enable = true;
programs.z-lua.enableBashIntegration = true;
programs.z-lua.options = [ "enhanced" "once" "fzf" ]; # modify as needed
**NOTE**: For wsl-1 users, `lua-filesystem` must be installed:
sudo apt-get install lua-filesystem
@@ -101,6 +98,12 @@ z -b foo bar # replace foo with bar in cwd and cd there
Options like "enhanced", "once" and "fzf" can be used after `--init` too. It can also be initialized from "skywind3000/z.lua" with your zsh plugin managers (antigen / oh-my-zsh).
NixOS users using home-manager can add this to their user profile:
programs.z-lua.enable = true;
programs.z-lua.enableZshIntegration = true;
programs.z-lua.options = [ "enhanced" "once" "fzf" ]; # modify as needed
**NOTE**: for wsl-1 users, `lua-filesystem` must be installed.
- Posix Shells:
@@ -127,6 +130,12 @@ z -b foo bar # replace foo with bar in cwd and cd there
into the same file.
NixOS users using home-manager can add this to their user profile:
programs.z-lua.enable = true;
programs.z-lua.enableFishIntegration = true;
programs.z-lua.options = [ "enhanced" "once" "fzf" ]; # modify as needed
- Nushell
Put something like this in your `env.nu`:
@@ -136,7 +145,8 @@ z -b foo bar # replace foo with bar in cwd and cd there
Then put something like this in your `config.nu`:
source ~/.cache/zlua.nu
alias z = _zlua
Note: Only Nushell v0.96+ is supported
- Power Shell:
@@ -607,6 +617,7 @@ This project needs help for the tasks below:
- Thanks to [@manhong2112](https://github.com/manhong2112) for Power Shell porting.
- Thanks to [@BarbUk](https://github.com/BarbUk) for fzf completion in Bash.
- Thanks to [@barlik](https://github.com/barlik) for many improvements.
- Thanks to [@brglng](https://github.com/brglng) for nushell porting.
And many others.

4
z.cmd
View File

@@ -89,7 +89,9 @@ if /i "%1"=="--purge" (
:check
if /i "%1"=="" (
set "RunMode=-l"
if /i "%InterMode%"=="" (
set "RunMode=-l"
)
)
for /f "delims=" %%i in ('cd') do set "PWD=%%i"

73
z.lua
View File

@@ -4,7 +4,7 @@
-- z.lua - a cd command that learns, by skywind 2018-2022
-- Licensed under MIT license.
--
-- Version 1.8.18, Last Modified: 2024/04/30 17:11
-- Version 1.8.25, Last Modified: 2026/03/09 21:15:46
--
-- * 10x faster than fasd and autojump, 3x faster than z.sh
-- * available for posix shells: bash, zsh, sh, ash, dash, busybox
@@ -235,7 +235,8 @@ end
function dump(o)
if type(o) == 'table' then
local s = '{ '
for k,v in pairs(o) do
for key, v in pairs(o) do
local k = key
if type(k) ~= 'number' then k = '"'..k..'"' end
s = s .. '['..k..'] = ' .. dump(v) .. ','
end
@@ -1612,21 +1613,23 @@ function z_cd(patterns)
if patterns == nil then
return nil
end
if #patterns == 0 then
return nil
end
local last = patterns[#patterns]
if last == '~' or last == '~/' then
return os.path.expand('~')
elseif windows and last == '~\\' then
return os.path.expand('~')
end
if os.path.isabs(last) and os.path.isdir(last) then
local size = #patterns
if size <= 1 then
return os.path.norm(last)
elseif last ~= '/' and last ~= '\\' then
return os.path.norm(last)
if Z_INTERACTIVE == 0 then
if #patterns == 0 then
return nil
end
local last = patterns[#patterns]
if last == '~' or last == '~/' then
return os.path.expand('~')
elseif windows and last == '~\\' then
return os.path.expand('~')
end
if os.path.isabs(last) and os.path.isdir(last) then
local size = #patterns
if size <= 1 then
return os.path.norm(last)
elseif last ~= '/' and last ~= '\\' then
return os.path.norm(last)
end
end
end
local M = z_match(patterns, Z_METHOD, Z_SUBDIR)
@@ -1668,15 +1671,15 @@ function z_cd(patterns)
local flag = os.environ('_ZL_FZF_FLAG', '')
flag = (flag == '' or flag == nil) and '+s -e' or flag
cmd = ((fzf == '') and 'fzf' or fzf) .. ' ' .. cmd .. ' ' .. flag
local height = os.environ('_ZL_FZF_HEIGHT', '35%')
if height ~= nil and height ~= '' and height ~= '0' then
cmd = cmd .. ' --height ' .. height
end
if not windows then
tmpname = os.tmpname()
local height = os.environ('_ZL_FZF_HEIGHT', '35%')
if height ~= nil and height ~= '' and height ~= '0' then
cmd = cmd .. ' --height ' .. height
end
cmd = cmd .. ' < "' .. tmpname .. '"'
else
tmpname = os.tmpname():gsub('\\', ''):gsub('%.', '')
tmpname = os.tmpname():gsub('[\\:]', ''):gsub('%.', '')
tmpname = os.environ('TMP', '') .. '\\zlua_' .. tmpname .. '.txt'
cmd = 'type "' .. tmpname .. '" | ' .. cmd
end
@@ -1873,7 +1876,7 @@ function cd_breadcrumbs(pwd, interactive)
if not windows then
tmpname = os.tmpname()
else
tmpname = os.tmpname():gsub('\\', ''):gsub('%.', '')
tmpname = os.tmpname():gsub('[\\:]', ''):gsub('%.', '')
tmpname = os.environ('TMP', '') .. '\\zlua_' .. tmpname .. '.txt'
end
fp = io.open(tmpname, 'w')
@@ -1971,13 +1974,13 @@ function main(argv)
end
elseif options['-'] then
path = cd_minus(args, options)
elseif #args == 0 then
elseif #args == 0 and Z_INTERACTIVE == 0 then
path = nil
else
path = z_cd(args)
if path == nil and Z_MATCHMODE ~= 0 then
local last = args[#args]
if os.path.isdir(last) then
if last and os.path.isdir(last) then
path = os.path.abspath(last)
path = os.path.norm(path)
end
@@ -2008,7 +2011,7 @@ function main(argv)
elseif opts.fish then
z_fish_init(opts)
elseif opts.powershell then
z_windows_init(opts)
z_windows_init(opts)
else
z_shell_init(opts)
end
@@ -2020,9 +2023,14 @@ function main(argv)
z_print(M, true, false)
end
elseif options['--complete'] then
local line = args[1] and args[1] or ''
local head = line:sub(Z_CMD:len()+1):gsub('^%s+', '')
local M = z_match({head}, Z_METHOD, Z_SUBDIR)
local M = {}
if options['-m1'] then
M = z_match(args and args or {}, Z_METHOD, Z_SUBDIR)
else
local line = args[1] and args[1] or ''
local head = line:sub(Z_CMD:len()+1):gsub('^%s+', '')
M = z_match({head}, Z_METHOD, Z_SUBDIR)
end
for _, item in pairs(M) do
print(item.name)
end
@@ -2805,7 +2813,7 @@ end
-- nushell
-----------------------------------------------------------------------
local script_zlua_nushell = [[
def _zlua --env --wrapped [...args: string] {
export def _zlua --env --wrapped [...args: string] {
if ($args | length) != 0 and $args.0 == "--add" {
with-env { _ZL_RANDOM: (random int) } { ^$env.ZLUA_LUAEXE $env.ZLUA_SCRIPT --add ...($args | skip 1) }
} else if ($args | length) != 0 and $args.0 == "--complete" {
@@ -2864,7 +2872,7 @@ $env.config = ($env.config | update hooks.env_change.PWD ($env.config.hooks.env_
]]
local script_complete_nushell = [[
let zlua_completer = {|spans| $spans | skip 1 | _zlua --complete ...$in | lines | where {|x| $x != $env.PWD}}
let zlua_completer = {|spans| $spans | skip 1 | _zlua --complete -m1 ...$in | lines | where {|x| $x != $env.PWD}}
$env.config = ($env.config | default {} completions)
$env.config = ($env.config | update completions ($env.config.completions | default {} external))
@@ -2890,6 +2898,9 @@ if completer in $env.config.completions.external {
}
})
}
export alias z = _zlua
]]
-----------------------------------------------------------------------