mirror of
https://github.com/skywind3000/z.lua
synced 2026-03-22 15:49:47 +00:00
Compare commits
20 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b691d537f2 | ||
|
|
472925ac92 | ||
|
|
5ba0e50894 | ||
|
|
c769dba4dd | ||
|
|
bbca83c9a5 | ||
|
|
53c127a4a2 | ||
|
|
5f7fa928b6 | ||
|
|
68de23ecef | ||
|
|
5ea08c5dac | ||
|
|
9dd0fec7ed | ||
|
|
fe11dd30c5 | ||
|
|
db92fa0a1c | ||
|
|
b1702839d2 | ||
|
|
1a0428f54e | ||
|
|
31e9d42d08 | ||
|
|
14b942634c | ||
|
|
6cfe2150e9 | ||
|
|
a4c589a724 | ||
|
|
cb8021599c | ||
|
|
91a4d75e62 |
17
README.cn.md
17
README.cn.md
@@ -250,7 +250,9 @@ PS:如果你使用 Fish shell,需要 2.7.0 以上才支持该功能。
|
||||
|
||||

|
||||
|
||||
当你使用 `"z -I vim"` 时,12 条路径被筛选出来,并按照 frecent 排序,他们都包含 "vim" 关键字,在实际 cd 改变路径前,z.lua 会调用 fzf 来让你更方便的选择你想去的地方,每条记录包含左边的 frecent 权重和右边的路径名,权重越高的排在越前面,你可以按回车选择,或者 ESC 退出。
|
||||
当你使用 `"z -I vim"` 时,12 条路径被筛选出来,并按照 frecent 排序,他们都包含 "vim" 关键字,在实际 cd 改变路径前,z.lua 会调用 fzf 来让你更方便的选择你想去的地方,每条记录包含左边的 frecent 权重和右边的路径名,权重越高的排在越前面。
|
||||
|
||||
你可以在 fzf 里输入一些**空格分隔**的关键字(不需要先后顺序),或者按 `CTRL+J` / `CTRL+K` (方向键的上下也可以)进行选择,`ESC` 或者 `CTRL`+`D`/`G` 放弃。
|
||||
|
||||
你仍然可以用老方法,通过在 `z` 命令后面添加更多关键词来精确的匹配你想去的地方,这个特性给了你一个可视化的方式来做这件事情。为了方便起见,通常把 `z -I` alias 成 `zf` (z + fuzzy finder)。如果搜索结果只有一项,那么 z.lua 会直接跳转过去,不需要启动 fzf 再选择一遍,只有多项结果要选择时,才会启动 fzf。
|
||||
|
||||
@@ -267,7 +269,7 @@ PS:你可以使用 `$_ZL_FZF` 环境变量来精确指明 fzf 的可执行路
|
||||
- **(单个参数)**:`cd` 到离当前目录最近的以关键字开头的父目录,如果找不到就尝试跳到包含关键字的父目录。
|
||||
- **(两个参数)**:将当前路径中的第一个关键词替换为第二个关键词。
|
||||
|
||||
为了使用简便,我们继续将 `z -b` 取个别名成 `zb`:
|
||||
先将 `z -b` 别名成 `zb`:
|
||||
|
||||
```bash
|
||||
# 一直向上退到项目根目录(就是里面有一个 .git 目录的地方)
|
||||
@@ -348,23 +350,22 @@ $ z -
|
||||
=> cd -
|
||||
```
|
||||
|
||||
这个 `dirstack` 是根据 z.lua 的路径历史数据库计算的出来的,所以和具体的 shell 或者操作系统都没有关系。你退出再登陆不会丢失这些路径历史,不同的 shell 之间也可以共享同一份历史数据。
|
||||
这个 `dirstack` 是根据 z.lua 的路径历史数据库计算的出来的,和具体的 shell 或者操作系统无关。你退出再登陆不会丢失这些记录,不同的 shell 之间也可以共享同一份记录。
|
||||
|
||||
此外,还有另外一种方法,通过使用前面提到的 `-I` 和 `-t` 参数组和,可以使用 fzf 进行交互式选择:
|
||||
此外,还能通过前面提到的 `-I` 和 `-t` 参数组和,使用 fzf 选择最近去过的目录:
|
||||
|
||||
```bash
|
||||
alias zh='z -I -t .'
|
||||
```
|
||||
|
||||
首先定义个新的别名 `zh`(回到历史路径的意思),我们使用了 `-t` 参数来告诉 `z.lua` 按照时间戳作为权重进行排序,同时 `-I` 启用 fzf 进行搜索,最后的句号代表任意路径。
|
||||
方便起见,定义个新的别名 `zh`(回到历史路径的意思),我们用 `-t` 参数来告诉 `z.lua` 按时间戳为权重排序,同时 `-I` 启用 fzf 搜索,最后句号代表任意路径。
|
||||
|
||||
那么当我们在命令行敲入 zh 时,就可以用 fzf 选择最近呆过的路径了:
|
||||
那么当我们在命令行敲入 zh 时,就可以用 fzf 进行历史路径操作了:
|
||||
|
||||

|
||||
|
||||
第一列数据代表该路径多少秒以前访问过,第二列是路径名称。此时,你可以试着敲几个字母,用 fzf 的字符串模糊匹配进行定位,或者用光标键的上和下(CTRL+J/K 也可以)来上下移动,最后按回车 cd 过去,或者 ESC 放弃。
|
||||
第一列上次访问距今多少秒,第二列是目录名。你可以试着敲几个字母,用 fzf 的字符串模糊匹配进行定位,或者用光标键的上和下(CTRL+J/K 也可以)来上下移动,最后按回车 cd 过去,或者 ESC 放弃。
|
||||
|
||||
注意:最好把增强匹配模式打开,这样查看历史路径时可以把当前路径过滤掉。
|
||||
|
||||
|
||||
## Tips
|
||||
|
||||
15
README.md
15
README.md
@@ -269,13 +269,15 @@ From version 1.1.0, a new option `"-I"` will allow you to use fzf to select when
|
||||
|
||||

|
||||
|
||||
When we use `"z -I vim"`,12 paths contains keyword "vim" has been matched and ordered by their frecent value, the higher frecent comes with the higher rank. Then without cd to the highest ranked path, z.lua passes all the candidates to fzf. And you can use fzf to select where you want to go, or ESC to quit.
|
||||
When we use `"z -I vim"`,12 paths contains keyword "vim" has been matched and ordered by their frecent value, the higher frecent comes with the higher rank. Then without cd to the highest ranked path, z.lua passes all the candidates to fzf.
|
||||
|
||||
Now you can input some space separated keywords (no order required) or use `CTRL+J`/`CTRL+K` (same as `UP`/`DOWN`) to select where you want to go, or `ESC` / `CTRL`+`D`/`G` to give up.
|
||||
|
||||
Of course, you can always give more keywords to `z` command to match your destination precisely. `"z -I"` is similar to `"z -i"`, but use fzf. Both `"-i"` and `"-I"` provide you another way for path navigation.
|
||||
|
||||
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.
|
||||
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.
|
||||
For more information about this, please visit [wiki - effective with fzf](https://github.com/skywind3000/z.lua/wiki/Effective-with-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.
|
||||
|
||||
@@ -284,7 +286,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 the nearest 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, go to the parent containing keyword.
|
||||
- **(Two arguments)**: replace the first value with the second one (in the current path).
|
||||
|
||||
@@ -456,6 +458,11 @@ As you see, z.lua is the fastest one and requires less resource.
|
||||
|
||||
## History
|
||||
|
||||
- 1.5.9 (2019-02-25): `z -b` should not match current directory (close #56).
|
||||
- 1.5.8 (2019-02-21): new `$_ZL_FZF_HEIGHT` to control `--height` parameter in fzf.
|
||||
- 1.5.7 (2019-02-21): rename `$_ZL_FZF_SORT` to `$_ZL_INT_SORT` it will affect both `-i` and `-I`.
|
||||
- 1.5.6 (2019-02-20): set `$_ZL_FZF_SORT` to 1 to sort directories by alphabet in fzf.
|
||||
- 1.5.5 (2019-02-20): `$_ZL_FZF_FLAG` can be used to override fzf flags, default to "+s -e".
|
||||
- 1.5.4 (2019-02-19): fixed: file/path existence detection fails on read-only fs (closed [#49](https://github.com/skywind3000/z.lua/issues/49) by [@contrun](https://github.com/contrun)).
|
||||
- 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.
|
||||
|
||||
BIN
images/step1.png
Normal file
BIN
images/step1.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 28 KiB |
BIN
images/step2.png
Normal file
BIN
images/step2.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 13 KiB |
BIN
images/step3.png
Normal file
BIN
images/step3.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 7.6 KiB |
BIN
images/step4.png
Normal file
BIN
images/step4.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 4.8 KiB |
33
z.lua
33
z.lua
@@ -4,7 +4,7 @@
|
||||
-- z.lua - a cd command that learns, by skywind 2018, 2019
|
||||
-- Licensed under MIT license.
|
||||
--
|
||||
-- Version 1.5.4, Last Modified: 2019/02/19 11:18
|
||||
-- Version 1.5.9, Last Modified: 2019/02/25 23:17
|
||||
--
|
||||
-- * 10x faster than fasd and autojump, 3x faster than z.sh
|
||||
-- * available for posix shells: bash, zsh, sh, ash, dash, busybox
|
||||
@@ -770,6 +770,10 @@ end
|
||||
-----------------------------------------------------------------------
|
||||
function os.environ(name, default)
|
||||
local value = os.getenv(name)
|
||||
if os.envmap ~= nil and type(os.envmap) == 'table' then
|
||||
local t = os.envmap[name]
|
||||
value = (t ~= nil and type(t) == 'string') and t or value
|
||||
end
|
||||
if value == nil then
|
||||
return default
|
||||
elseif type(default) == 'boolean' then
|
||||
@@ -1427,6 +1431,9 @@ function z_cd(patterns)
|
||||
elseif Z_INTERACTIVE == 0 then
|
||||
return M[1].name
|
||||
end
|
||||
if os.environ('_ZL_INT_SORT', false) then
|
||||
table.sort(M, function (a, b) return a.name < b.name end)
|
||||
end
|
||||
local retval = nil
|
||||
if Z_INTERACTIVE == 1 then
|
||||
PRINT_MODE = '<stderr>'
|
||||
@@ -1448,13 +1455,15 @@ function z_cd(patterns)
|
||||
elseif Z_INTERACTIVE == 2 then
|
||||
local fzf = os.environ('_ZL_FZF', 'fzf')
|
||||
local tmpname = '/tmp/zlua.txt'
|
||||
local cmd = '--nth 2.. --reverse --inline-info +s --tac -e'
|
||||
cmd = ((fzf == '') and 'fzf' or fzf) .. ' ' .. cmd
|
||||
cmd = cmd .. ' ' .. os.environ('_ZL_FZF_FLAG', '') .. ' '
|
||||
local cmd = '--nth 2.. --reverse --inline-info --tac '
|
||||
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
|
||||
if not windows then
|
||||
tmpname = os.tmpname()
|
||||
if not os.environ('_ZL_FZF_FULLSCR', false) then
|
||||
cmd = cmd .. ' --height 35%'
|
||||
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
|
||||
@@ -1546,6 +1555,7 @@ function cd_backward(args, options, pwd)
|
||||
end
|
||||
return os.path.normpath(path)
|
||||
else
|
||||
pwd = os.path.split(pwd)
|
||||
local test = windows and pwd:gsub('\\', '/') or pwd
|
||||
local key = windows and args[1]:lower() or args[1]
|
||||
if not key:match('%u') then
|
||||
@@ -1996,11 +2006,14 @@ 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 -e"
|
||||
if not os.environ('_ZL_FZF_FULLSCR', false) then
|
||||
fzf_cmd = fzf_cmd .. ' --height 35%'
|
||||
fzf_cmd = "fzf --nth 2.. --reverse --inline-info --tac "
|
||||
local height = os.environ('_ZL_FZF_HEIGHT', '35%')
|
||||
if height ~= nil and height ~= '' and height ~= '0' then
|
||||
fzf_cmd = fzf_cmd .. ' --height ' .. height .. ' '
|
||||
end
|
||||
fzf_cmd = fzf_cmd .. ' ' .. os.environ('_ZL_FZF_FLAG', '') .. ' '
|
||||
local flag = os.environ('_ZL_FZF_FLAG', '')
|
||||
flag = (flag == '' or flag == nil) and '+s -e' or flag
|
||||
fzf_cmd = fzf_cmd .. ' ' .. flag .. ' '
|
||||
print('zlua_fzf="' .. fzf_cmd .. '"')
|
||||
print(script_fzf_complete_bash)
|
||||
end
|
||||
|
||||
@@ -6,14 +6,14 @@ ZLUA_SCRIPT="${0:A:h}/z.lua"
|
||||
|
||||
# search lua executable
|
||||
if [[ -z "$ZLUA_EXEC" ]]; then
|
||||
if [[ -x "$(which lua)" ]]; then
|
||||
ZLUA_EXEC="$(which lua)"
|
||||
elif [[ -x "$(which lua5.3)" ]]; then
|
||||
ZLUA_EXEC="$(which lua5.3)"
|
||||
elif [[ -x "$(which lua5.2)" ]]; then
|
||||
ZLUA_EXEC="$(which lua5.2)"
|
||||
elif [[ -x "$(which lua5.1)" ]]; then
|
||||
ZLUA_EXEC="$(which lua5.1)"
|
||||
if [[ -x "$(command which lua)" ]]; then
|
||||
ZLUA_EXEC="$(command which lua)"
|
||||
elif [[ -x "$(command which lua5.3)" ]]; then
|
||||
ZLUA_EXEC="$(command which lua5.3)"
|
||||
elif [[ -x "$(command which lua5.2)" ]]; then
|
||||
ZLUA_EXEC="$(command which lua5.2)"
|
||||
elif [[ -x "$(command which lua5.1)" ]]; then
|
||||
ZLUA_EXEC="$(command which lua5.1)"
|
||||
else
|
||||
echo "Not find lua in your $PATH, please install it."
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user