mirror of
https://github.com/skywind3000/z.lua
synced 2026-03-21 23:29:48 +00:00
Compare commits
7 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2e658dd379 | ||
|
|
f638730d5f | ||
|
|
b9e8501b71 | ||
|
|
31fd92f2da | ||
|
|
7af0290e11 | ||
|
|
8334b1adb7 | ||
|
|
f068e8597f |
39
README.cn.md
39
README.cn.md
@@ -18,7 +18,7 @@ z.lua 是一个快速路径切换工具,它会跟踪你在 shell 下访问过
|
||||
- 使用增强匹配算法,更准确的带你去到你想去的地方。
|
||||
- 低占用,能够仅在当前路径改变时才更新数据库(将 `$_ZL_ADD_ONCE` 设成 1)。
|
||||
- 交互选择模式,如果有多个匹配结果的话,跳转前允许你进行选择。
|
||||
- 集成 fzf (可选),可以用来做可视化选择或者 bash 参数补全。
|
||||
- 集成 fzf (可选),可以用来做可视化选择或者参数补全。
|
||||
- 快速跳转到父目录,或者项目根目录,代替反复 “cd ../../.." 。
|
||||
- 兼容 lua 5.1, 5.2 和 5.3 以上版本。
|
||||
- 自包含且无额外依赖,单个 `z.lua` 文件完成所有工作。
|
||||
@@ -263,7 +263,7 @@ PS:你可以使用 `$_ZL_FZF` 环境变量来精确指明 fzf 的可执行路
|
||||
|
||||
`"-b"` 选项可以快速回到某一级父目录,避免重复的输入 "cd ../../.."。
|
||||
|
||||
- **(没有参数)**:`cd` 到项目根目录,即跳转到父目录中包含 (.git/.svn/.hg) 的地方。
|
||||
- **(没有参数)**:`cd` 到项目根目录,即跳转到最近的包含 (.git/.svn/.hg) 的父目录。
|
||||
- **(单个参数)**:`cd` 到离当前目录最近的以关键字开头的父目录,如果找不到就尝试跳到包含关键字的父目录。
|
||||
- **(两个参数)**:将当前路径中的第一个关键词替换为第二个关键词。
|
||||
|
||||
@@ -321,27 +321,14 @@ eval "$(lua /path/to/z.lua --init bash enhanced once echo fzf)"
|
||||
|
||||
有了 fzf 的帮助,bash 下补全也非常方便了。注意看左边的权重,fzf 搜索过滤的顺序是有讲究的,Frecent 权重越高的越靠前,不是乱排序的,更不是简单的按字符串字母排序。这里完全保证权重越高的路径越靠前。
|
||||
|
||||
`z.lua` 可以同 [fz](https://github.com/changyuheng/fz) 协作以提供**更好的补全结果**,详细见 [FAQ](https://github.com/skywind3000/z.lua/wiki/FAQ#fzsh-for-better-completion)。
|
||||
|
||||
注意:该功能在初始化 z.lua 之前,会检测 $PATH 中是否有 fzf 这个程序,有的话才启用。
|
||||
|
||||
|
||||
## MRU
|
||||
|
||||
以往我们经常使用 `cd -` 命令来回到刚才访问过的目录,而有了 fzf 的帮助,我们可以更高效的来完成这件事情:
|
||||
|
||||
|
||||
```bash
|
||||
alias zh='z -I -t .'
|
||||
```
|
||||
|
||||
首先定义个新的别名 `zh`(回到历史路径的意思),我们使用了 `-t` 参数来告诉 `z.lua` 按照时间戳作为权重进行排序,同时 `-I` 启用 fzf 进行搜索,最后的句号代表任意路径。
|
||||
|
||||
那么当我们在命令行敲入 zh 时,就可以用 fzf 选择最近呆过的路径了:
|
||||
|
||||

|
||||
|
||||
第一列数据代表该路径多少秒以前访问过,第二列是路径名称。此时,你可以试着敲几个字母,用 fzf 的字符串模糊匹配进行定位,或者用光标键的上和下(CTRL+J/K 也可以)来上下移动,最后按回车 cd 过去,或者 ESC 放弃。
|
||||
|
||||
版本 `v1.5.1` 引入了模拟 `dirstack` 可以通过: `z -`,`z --` 和 `z -{num}` 来操作:
|
||||
`z.lua` 提供 `dirstack` 让你更便捷的访问最近刚刚去过的目录,而不需要输入任何关键字。这个方法叫做 `dirstack`,它记录着最近你刚刚去过的 10 条最新路径,然后是用 `z -`,`z --` 和 `z -{num}` 来操作:
|
||||
|
||||
```bash
|
||||
# 显示当前的 dir stack
|
||||
@@ -363,6 +350,20 @@ $ z -
|
||||
|
||||
这个 `dirstack` 是根据 z.lua 的路径历史数据库计算的出来的,所以和具体的 shell 或者操作系统都没有关系。你退出再登陆不会丢失这些路径历史,不同的 shell 之间也可以共享同一份历史数据。
|
||||
|
||||
此外,还有另外一种方法,通过使用前面提到的 `-I` 和 `-t` 参数组和,可以使用 fzf 进行交互式选择:
|
||||
|
||||
```bash
|
||||
alias zh='z -I -t .'
|
||||
```
|
||||
|
||||
首先定义个新的别名 `zh`(回到历史路径的意思),我们使用了 `-t` 参数来告诉 `z.lua` 按照时间戳作为权重进行排序,同时 `-I` 启用 fzf 进行搜索,最后的句号代表任意路径。
|
||||
|
||||
那么当我们在命令行敲入 zh 时,就可以用 fzf 选择最近呆过的路径了:
|
||||
|
||||

|
||||
|
||||
第一列数据代表该路径多少秒以前访问过,第二列是路径名称。此时,你可以试着敲几个字母,用 fzf 的字符串模糊匹配进行定位,或者用光标键的上和下(CTRL+J/K 也可以)来上下移动,最后按回车 cd 过去,或者 ESC 放弃。
|
||||
|
||||
注意:最好把增强匹配模式打开,这样查看历史路径时可以把当前路径过滤掉。
|
||||
|
||||
|
||||
@@ -391,7 +392,7 @@ FN="$HOME/.local/share/autojump/autojump.txt"
|
||||
awk -F '\t' '{print $2 "|" $1 "|" 0}' $FN >> ~/.zlua
|
||||
```
|
||||
|
||||
|
||||
要更好的使用 `z.lua`,别忘记阅读:[Frequently Asked Questions](https://github.com/skywind3000/z.lua/wiki/FAQ)。
|
||||
|
||||
|
||||
## Benchmark
|
||||
|
||||
26
README.md
26
README.md
@@ -19,11 +19,12 @@ For example, `z foo bar` would match `/foo/bar` but not `/bar/foo`.
|
||||
- **10x** times faster than **fasd** and **autojump**, **3x** times faster than **z.sh**.
|
||||
- Available for **posix shells**: bash, zsh, dash, sh, ash, ksh, busybox and etc.
|
||||
- Available for Fish Shell, Power Shell and Windows cmd.
|
||||
- Enhanced matching mode takes you to where ever you want precisely.
|
||||
- [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.
|
||||
- Intergrated with FZF (optional) for interactive selection and bash completion.
|
||||
- Intergrated with FZF (optional) for interactive selection and completion.
|
||||
- Quickly go back to a parent directory instead of typing "cd ../../..".
|
||||
- Cooperate with [fz](https://github.com/changyuheng/fz) to provide better completion, see [FAQ](https://github.com/skywind3000/z.lua/wiki/FAQ#fzsh-for-better-completion).
|
||||
- Corresponding experience in different shells and operating systems.
|
||||
- Compatible with Lua 5.1, 5.2 and 5.3+
|
||||
- Self contained, distributed as a single `z.lua` script, no other dependence.
|
||||
@@ -243,7 +244,8 @@ eval "$(lua /path/to/z.lua --init zsh once)"
|
||||
source (lua /path/to/z.lua --init fish once | psub)
|
||||
````
|
||||
|
||||
It could be much faster on slow hardware or Cygwin/MSYS.
|
||||
With `add once` mode off (default), z.lua will consider the time you spent in the directory (like z.sh). When this mode is on, consider the times you accessed the directory (like autojump), and that could be much faster on slow hardware.
|
||||
|
||||
|
||||
## Interactive Selection
|
||||
|
||||
@@ -261,6 +263,7 @@ And then you can input the number and choose where to go before actual cd. eg. i
|
||||
|
||||
NOTE: for fish shell, this feature requires fish 2.7.0 or above.
|
||||
|
||||
|
||||
## FZF Supports
|
||||
|
||||
From version 1.1.0, a new option `"-I"` will allow you to use fzf to select when there are multiple matches.
|
||||
@@ -273,7 +276,7 @@ Of course, you can always give more keywords to `z` command to match your destin
|
||||
|
||||
Usually, `z -I` can be aliased to `zf` (z + fuzzy finder) for convenience. If there are only one path matched, `z -I` will jump to it directly, fzf will only be invoked for multiple matches.
|
||||
|
||||
`"z -I ."` or `"zf ."` can be used to use fzf select from entire database.
|
||||
`"z -I ."` or `"zf ."` can be used to use fzf select from entire database. Environment variable `_ZL_FZF_FLAG` is for passing additional arguments to fzf.
|
||||
|
||||
NOTE: For fish shell, this feature requires fish 2.7.0 or above. You can specify fzf executable in `$_ZL_FZF` environment variable, `"fzf"` will be called by default.
|
||||
|
||||
@@ -282,7 +285,7 @@ NOTE: For fish shell, this feature requires fish 2.7.0 or above. You can specify
|
||||
|
||||
New option `"-b"` can quickly go back to a specific parent directory in bash instead of typing "cd ../../.." redundantly.
|
||||
|
||||
- **(No argument)**: `cd` into the project root, the project root is one of the parent directory with `.git`/`.hg`/`.svn` in it
|
||||
- **(No argument)**: `cd` into the project root, the project root the nearest parent directory with `.git`/`.hg`/`.svn` in it
|
||||
- **(One argument)**: `cd` into the closest parent starting with keyword, if not find, goto the parent containing keyword.
|
||||
- **(Two arguments)**: replace the first value with the second one (in the current path).
|
||||
|
||||
@@ -337,10 +340,13 @@ Then press `<tab>` after `z xxx`:
|
||||
|
||||

|
||||
|
||||
With the help of fzf, completion in bash is much easier now.
|
||||
With the help of fzf, completion in bash is much easier now.
|
||||
|
||||
`z.lua` can cooperate with [fz](https://github.com/changyuheng/fz) for **better completion** result in both bash and zsh, for more information see [FAQ](https://github.com/skywind3000/z.lua/wiki/FAQ#fzsh-for-better-completion).
|
||||
|
||||
NOTE: To enable this, command `fzf` must be found in `$PATH` before initialization.
|
||||
|
||||
|
||||
## Most Recently Accessed Path
|
||||
|
||||
`z.lua` provides a fast way to visit MRU directories without typing any keyword. That is `dirstack`, which records recently visited paths and can be manipulated by `z -`, `z --` and `z -{num}`:
|
||||
@@ -371,7 +377,7 @@ There is another way to access MRU directories interactively by utilizing parame
|
||||
alias zh='z -I -t .'
|
||||
```
|
||||
|
||||
The new alias `zh` (jump to history) uses a parameter `-t` to sort the result by time and `-I` to use `fzf` for selection. the result is:
|
||||
The new alias `zh` (jump to history) is very easy to input:
|
||||
|
||||

|
||||
|
||||
@@ -388,8 +394,8 @@ Remember to enable the [enhanced matching](#enhanced-matching) algorithm, the cu
|
||||
Recommended aliases you may find useful:
|
||||
|
||||
```bash
|
||||
alias zc='z -c' # restrict matches to subdirs of $PWD
|
||||
alias zz='z -i' # cd with interactive selection
|
||||
alias zz='z -c' # restrict matches to subdirs of $PWD
|
||||
alias zi='z -i' # cd with interactive selection
|
||||
alias zf='z -I' # use fzf to select in multiple matches
|
||||
alias zb='z -b' # quickly cd to the parent directory
|
||||
```
|
||||
@@ -408,6 +414,7 @@ FN="$HOME/.local/share/autojump/autojump.txt"
|
||||
awk -F '\t' '{print $2 "|" $1 "|" 0}' $FN >> ~/.zlua
|
||||
```
|
||||
|
||||
Don't forget to read the [Frequently Asked Questions](https://github.com/skywind3000/z.lua/wiki/FAQ).
|
||||
|
||||
|
||||
## Benchmark
|
||||
@@ -450,6 +457,7 @@ As you see, z.lua is the fastest one and requires less resource.
|
||||
|
||||
## History
|
||||
|
||||
- 1.5.3 (2019-02-17): new `$_ZL_FZF_FLAG` for passing additional flags to fzf, add `-e` argument to fzf.
|
||||
- 1.5.2 (2019-02-16): be aware of all arguments in fzf completion.
|
||||
- 1.5.1 (2019-02-15): new: simulated dir stack by `z -`, `z --` and `z -{num}`.
|
||||
- 1.5.0 (2019-02-14): fixed minor issues in backward jumping.
|
||||
|
||||
59
z.lua
59
z.lua
@@ -4,7 +4,7 @@
|
||||
-- z.lua - a cd command that learns, by skywind 2018, 2019
|
||||
-- Licensed under MIT license.
|
||||
--
|
||||
-- Version 1.5.2, Last Modified: 2019/02/16 14:25
|
||||
-- Version 1.5.3, Last Modified: 2019/02/17 16:22
|
||||
--
|
||||
-- * 10x faster than fasd and autojump, 3x faster than z.sh
|
||||
-- * available for posix shells: bash, zsh, sh, ash, dash, busybox
|
||||
@@ -757,6 +757,37 @@ function os.scriptname()
|
||||
end
|
||||
|
||||
|
||||
-----------------------------------------------------------------------
|
||||
-- get environ
|
||||
-----------------------------------------------------------------------
|
||||
function os.environ(name, default)
|
||||
local value = os.getenv(name)
|
||||
if value == nil then
|
||||
return default
|
||||
elseif type(default) == 'boolean' then
|
||||
value = value:lower()
|
||||
if value == '0' or value == '' or value == 'no' then
|
||||
return false
|
||||
elseif value == 'false' or value == 'n' or value == 'f' then
|
||||
return false
|
||||
else
|
||||
return true
|
||||
end
|
||||
elseif type(default) == 'number' then
|
||||
value = tonumber(value)
|
||||
if value == nil then
|
||||
return default
|
||||
else
|
||||
return value
|
||||
end
|
||||
elseif type(default) == 'string' then
|
||||
return value
|
||||
elseif type(default) == 'table' then
|
||||
return value:sep(',')
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
-----------------------------------------------------------------------
|
||||
-- parse option
|
||||
-----------------------------------------------------------------------
|
||||
@@ -1406,19 +1437,20 @@ function z_cd(patterns)
|
||||
end
|
||||
retval = M[index].name
|
||||
elseif Z_INTERACTIVE == 2 then
|
||||
local fzf = os.getenv('_ZL_FZF')
|
||||
local fzf = os.environ('_ZL_FZF', 'fzf')
|
||||
local tmpname = '/tmp/zlua.txt'
|
||||
local cmd = '--nth 2.. --reverse --inline-info +s --tac'
|
||||
local cmd = ((not fzf) and 'fzf' or fzf) .. ' ' .. cmd
|
||||
local cmd = '--nth 2.. --reverse --inline-info +s --tac -e'
|
||||
cmd = ((fzf == '') and 'fzf' or fzf) .. ' ' .. cmd
|
||||
cmd = cmd .. ' ' .. os.environ('_ZL_FZF_FLAG', '') .. ' '
|
||||
if not windows then
|
||||
tmpname = os.tmpname()
|
||||
if not os.getenv('_ZL_FZF_FULLSCR') then
|
||||
if not os.environ('_ZL_FZF_FULLSCR', false) then
|
||||
cmd = cmd .. ' --height 35%'
|
||||
end
|
||||
cmd = cmd .. ' < "' .. tmpname .. '"'
|
||||
else
|
||||
tmpname = os.tmpname():gsub('\\', ''):gsub('%.', '')
|
||||
tmpname = os.getenv('TMP') .. '\\zlua_' .. tmpname .. '.txt'
|
||||
tmpname = os.environ('TMP', '') .. '\\zlua_' .. tmpname .. '.txt'
|
||||
cmd = 'type "' .. tmpname .. '" | ' .. cmd
|
||||
end
|
||||
PRINT_MODE = tmpname
|
||||
@@ -1710,7 +1742,7 @@ function z_init()
|
||||
table.insert(Z_EXCLUDE, name)
|
||||
end
|
||||
end
|
||||
if _zl_cmd ~= nil then
|
||||
if _zl_cmd ~= nil and _zl_cmd ~= '' then
|
||||
Z_CMD = _zl_cmd
|
||||
end
|
||||
if _zl_matchname ~= nil then
|
||||
@@ -1740,7 +1772,7 @@ end
|
||||
-- initialize clink hooks
|
||||
-----------------------------------------------------------------------
|
||||
function z_clink_init()
|
||||
local once = os.getenv("_ZL_ADD_ONCE")
|
||||
local once = os.environ("_ZL_ADD_ONCE", false)
|
||||
local previous = ''
|
||||
function z_add_to_database()
|
||||
pwd = clink.get_cwd()
|
||||
@@ -1940,12 +1972,12 @@ function z_shell_init(opts)
|
||||
print(script)
|
||||
end
|
||||
|
||||
local prompt_hook = (os.getenv("_ZL_NO_PROMPT_COMMAND") == nil)
|
||||
local once = (os.getenv("_ZL_ADD_ONCE") ~= nil) or opts.once ~= nil
|
||||
local prompt_hook = (not os.environ("_ZL_NO_PROMPT_COMMAND", false))
|
||||
local once = os.environ("_ZL_ADD_ONCE", false) or opts.once ~= nil
|
||||
|
||||
if opts.bash ~= nil then
|
||||
if prompt_hook then
|
||||
if opts.once then
|
||||
if once then
|
||||
print(script_init_bash_once)
|
||||
elseif opts.fast then
|
||||
print(script_init_bash_fast)
|
||||
@@ -1955,10 +1987,11 @@ function z_shell_init(opts)
|
||||
end
|
||||
print(script_complete_bash)
|
||||
if opts.fzf ~= nil then
|
||||
fzf_cmd = "fzf --nth 2 --reverse --inline-info +s --tac"
|
||||
if not os.getenv('_ZL_FZF_FULLSCR') then
|
||||
fzf_cmd = "fzf --nth 2 --reverse --inline-info +s --tac -e"
|
||||
if not os.environ('_ZL_FZF_FULLSCR', false) then
|
||||
fzf_cmd = fzf_cmd .. ' --height 35%'
|
||||
end
|
||||
fzf_cmd = fzf_cmd .. ' ' .. os.environ('_ZL_FZF_FLAG', '') .. ' '
|
||||
print('zlua_fzf="' .. fzf_cmd .. '"')
|
||||
print(script_fzf_complete_bash)
|
||||
end
|
||||
|
||||
@@ -20,6 +20,7 @@ if [[ -z "$ZLUA_EXEC" ]]; then
|
||||
fi
|
||||
fi
|
||||
|
||||
export _ZL_FZF_FLAG="-e"
|
||||
|
||||
eval "$($ZLUA_EXEC $ZLUA_SCRIPT --init zsh once enhanced)"
|
||||
|
||||
|
||||
Reference in New Issue
Block a user