mirror of
https://github.com/skywind3000/z.lua
synced 2026-03-22 15:49:47 +00:00
Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
92fc98d392 | ||
|
|
0b159b6c25 | ||
|
|
6e330ab46f | ||
|
|
1c9c5eb444 | ||
|
|
c4a9376c0c | ||
|
|
eea474d3ea |
23
README.cn.md
23
README.cn.md
@@ -341,8 +341,31 @@ alias zh='z -I -t .'
|
|||||||
|
|
||||||
第一列数据代表该路径多少秒以前访问过,第二列是路径名称。此时,你可以试着敲几个字母,用 fzf 的字符串模糊匹配进行定位,或者用光标键的上和下(CTRL+J/K 也可以)来上下移动,最后按回车 cd 过去,或者 ESC 放弃。
|
第一列数据代表该路径多少秒以前访问过,第二列是路径名称。此时,你可以试着敲几个字母,用 fzf 的字符串模糊匹配进行定位,或者用光标键的上和下(CTRL+J/K 也可以)来上下移动,最后按回车 cd 过去,或者 ESC 放弃。
|
||||||
|
|
||||||
|
版本 `v1.5.1` 引入了模拟 `dirstack` 可以通过: `z -`,`z --` 和 `z -{num}` 来操作:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# 显示当前的 dir stack
|
||||||
|
$ z --
|
||||||
|
0 /home/skywind/work/match/memory-match
|
||||||
|
1 /home/skywind/.local/etc
|
||||||
|
2 /home/skywind/software/vifm-0.9.1
|
||||||
|
3 /home/skywind/work
|
||||||
|
4 /home/skywind/work/match
|
||||||
|
|
||||||
|
# cd 到栈里 2 号路径
|
||||||
|
$ z -2
|
||||||
|
=> cd /home/skywind/software/vifm-0.9.1
|
||||||
|
|
||||||
|
# 弹出栈顶 (cd 到上一次的老路径),和 "z -0" 相同
|
||||||
|
$ z -
|
||||||
|
=> cd -
|
||||||
|
```
|
||||||
|
|
||||||
|
这个 `dirstack` 是根据 z.lua 的路径历史数据库计算的出来的,所以和具体的 shell 或者操作系统都没有关系。你退出再登陆不会丢失这些路径历史,不同的 shell 之间也可以共享同一份历史数据。
|
||||||
|
|
||||||
注意:最好把增强匹配模式打开,这样查看历史路径时可以把当前路径过滤掉。
|
注意:最好把增强匹配模式打开,这样查看历史路径时可以把当前路径过滤掉。
|
||||||
|
|
||||||
|
|
||||||
## Tips
|
## Tips
|
||||||
|
|
||||||
推荐一些常用的命令别名:
|
推荐一些常用的命令别名:
|
||||||
|
|||||||
29
README.md
29
README.md
@@ -343,7 +343,29 @@ NOTE: To enable this, command `fzf` must be found in `$PATH` before initializati
|
|||||||
|
|
||||||
## Most Recently Accessed Path
|
## Most Recently Accessed Path
|
||||||
|
|
||||||
We always use `cd -` to go back to last accessed directory, but with the help of `fzf` we can achieve this productively:
|
`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}`:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# display current dir stack
|
||||||
|
$ z --
|
||||||
|
0 /home/skywind/work/match/memory-match
|
||||||
|
1 /home/skywind/.local/etc
|
||||||
|
2 /home/skywind/software/vifm-0.9.1
|
||||||
|
3 /home/skywind/work
|
||||||
|
4 /home/skywind/work/match
|
||||||
|
|
||||||
|
# cd to the 2nd directory in the stack
|
||||||
|
$ z -2
|
||||||
|
=> cd /home/skywind/software/vifm-0.9.1
|
||||||
|
|
||||||
|
# popup stacktop (cd to previous directory), same as "z -0"
|
||||||
|
$ z -
|
||||||
|
=> cd -
|
||||||
|
```
|
||||||
|
|
||||||
|
The `dirstack` is calculated from z.lua's database, and has no dependency on shells or systems. You will not lost records after re-login, and history can be shared across shells and sessions.
|
||||||
|
|
||||||
|
There is another way to access MRU directories interactively by utilizing parameter `-I` (fzf) and `-t` (sort by time):
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
alias zh='z -I -t .'
|
alias zh='z -I -t .'
|
||||||
@@ -355,11 +377,12 @@ The new alias `zh` (jump to history) uses a parameter `-t` to sort the result by
|
|||||||
|
|
||||||
The first column indicates how many seconds ago you have visited, and the second column is the path name. With `zh`, you can type some character to use string matching in fzf, or use `<Up>`/`<Down>` (as well as `CTRL+j/k`) to move the selector (red `>`) up and down.
|
The first column indicates how many seconds ago you have visited, and the second column is the path name. With `zh`, you can type some character to use string matching in fzf, or use `<Up>`/`<Down>` (as well as `CTRL+j/k`) to move the selector (red `>`) up and down.
|
||||||
|
|
||||||
At last, press `<enter>` to accept or `<ESC>` to give up.
|
At last, press `<enter>` to accept or `<ESC>` to give up.
|
||||||
|
|
||||||
Remember to enable the [enhanced matching](#enhanced-matching) algorithm, the current working directory can be skipped with it.
|
Remember to enable the [enhanced matching](#enhanced-matching) algorithm, the current working directory can be skipped with it.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
## Tips
|
## Tips
|
||||||
|
|
||||||
Recommended aliases you may find useful:
|
Recommended aliases you may find useful:
|
||||||
@@ -427,6 +450,8 @@ As you see, z.lua is the fastest one and requires less resource.
|
|||||||
|
|
||||||
## History
|
## History
|
||||||
|
|
||||||
|
- 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.
|
- 1.5.0 (2019-02-14): fixed minor issues in backward jumping.
|
||||||
- 1.4.7 (2019-02-13): Don't use regex in backward jumping (use plain text instead).
|
- 1.4.7 (2019-02-13): Don't use regex in backward jumping (use plain text instead).
|
||||||
- 1.4.6 (2019-02-12): change: `_ZL_EXCLUDE_DIRS` to a comma separated list of dirs to exclude.
|
- 1.4.6 (2019-02-12): change: `_ZL_EXCLUDE_DIRS` to a comma separated list of dirs to exclude.
|
||||||
|
|||||||
56
z.lua
56
z.lua
@@ -4,7 +4,7 @@
|
|||||||
-- z.lua - a cd command that learns, by skywind 2018, 2019
|
-- z.lua - a cd command that learns, by skywind 2018, 2019
|
||||||
-- Licensed under MIT license.
|
-- Licensed under MIT license.
|
||||||
--
|
--
|
||||||
-- Version 1.5.0, Last Modified: 2019/02/14 22:57
|
-- Version 1.5.2, Last Modified: 2019/02/16 14:25
|
||||||
--
|
--
|
||||||
-- * 10x faster than fasd and autojump, 3x faster than z.sh
|
-- * 10x faster than fasd and autojump, 3x faster than z.sh
|
||||||
-- * available for posix shells: bash, zsh, sh, ash, dash, busybox
|
-- * available for posix shells: bash, zsh, sh, ash, dash, busybox
|
||||||
@@ -124,7 +124,6 @@ Z_SKIPPWD = false
|
|||||||
os.LOG_NAME = os.getenv('_ZL_LOG_NAME')
|
os.LOG_NAME = os.getenv('_ZL_LOG_NAME')
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
-----------------------------------------------------------------------
|
-----------------------------------------------------------------------
|
||||||
-- string lib
|
-- string lib
|
||||||
-----------------------------------------------------------------------
|
-----------------------------------------------------------------------
|
||||||
@@ -779,8 +778,16 @@ function os.getopt(argv)
|
|||||||
if head ~= '-' then
|
if head ~= '-' then
|
||||||
break
|
break
|
||||||
end
|
end
|
||||||
local part = arg:split('=')
|
if arg == '-' then
|
||||||
options[part[1]] = part[2] ~= nil and part[2] or ''
|
options['-'] = ''
|
||||||
|
elseif arg == '--' then
|
||||||
|
options['-'] = '-'
|
||||||
|
elseif arg:match('^-%d+$') then
|
||||||
|
options['-'] = arg:sub(2)
|
||||||
|
else
|
||||||
|
local part = arg:split('=')
|
||||||
|
options[part[1]] = part[2] ~= nil and part[2] or ''
|
||||||
|
end
|
||||||
end
|
end
|
||||||
index = index + 1
|
index = index + 1
|
||||||
end
|
end
|
||||||
@@ -1540,16 +1547,28 @@ end
|
|||||||
|
|
||||||
|
|
||||||
-----------------------------------------------------------------------
|
-----------------------------------------------------------------------
|
||||||
-- cd forward
|
-- cd minus: "z -", "z --", "z -2"
|
||||||
-----------------------------------------------------------------------
|
-----------------------------------------------------------------------
|
||||||
function cd_forward(args, options)
|
function cd_minus(args, options)
|
||||||
end
|
Z_SKIPPWD = true
|
||||||
|
local M = z_match({}, 'time', Z_SUBDIR)
|
||||||
|
local size = #M
|
||||||
-----------------------------------------------------------------------
|
if options['-'] == '-' then
|
||||||
-- cd detour
|
for i, item in ipairs(M) do
|
||||||
-----------------------------------------------------------------------
|
if i > 10 then break end
|
||||||
function cd_forward(args, options)
|
io.stderr:write(' ' .. tostring(i - 1) .. ' ' .. item.name .. '\n')
|
||||||
|
end
|
||||||
|
else
|
||||||
|
local level = 0
|
||||||
|
local num = options['-']
|
||||||
|
if num and num ~= '' then
|
||||||
|
level = tonumber(num)
|
||||||
|
end
|
||||||
|
if level >= 0 and level < size then
|
||||||
|
return M[level + 1].name
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return nil
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
@@ -1588,10 +1607,8 @@ function main(argv)
|
|||||||
local path = ''
|
local path = ''
|
||||||
if options['-b'] then
|
if options['-b'] then
|
||||||
path = cd_backward(args, options)
|
path = cd_backward(args, options)
|
||||||
elseif options['-f'] then
|
elseif options['-'] then
|
||||||
path = cd_forward(args, options)
|
path = cd_minus(args, options)
|
||||||
elseif options['-d'] then
|
|
||||||
path = cd_detour(args, options)
|
|
||||||
elseif #args == 0 then
|
elseif #args == 0 then
|
||||||
path = nil
|
path = nil
|
||||||
else
|
else
|
||||||
@@ -1883,8 +1900,7 @@ if [ "$TERM" != "dumb" ] && command -v fzf >/dev/null 2>&1; then
|
|||||||
# To redraw line after fzf closes (printf '\e[5n')
|
# To redraw line after fzf closes (printf '\e[5n')
|
||||||
bind '"\e[0n": redraw-current-line'
|
bind '"\e[0n": redraw-current-line'
|
||||||
_zlua_fzf_complete() {
|
_zlua_fzf_complete() {
|
||||||
local query="${COMP_WORDS[COMP_CWORD]}"
|
local selected=$(_zlua -l "${COMP_WORDS[@]:1}" | sed "s|$HOME|\~|" | $zlua_fzf | sed 's/^[0-9,.]* *//')
|
||||||
local selected=$(_zlua | sed "s|$HOME|\~|" | $zlua_fzf --query "$query" | sed 's/^[0-9,.]* *//')
|
|
||||||
if [ -n "$selected" ]; then
|
if [ -n "$selected" ]; then
|
||||||
COMPREPLY=( "$selected" )
|
COMPREPLY=( "$selected" )
|
||||||
fi
|
fi
|
||||||
@@ -2049,8 +2065,8 @@ function _zlua
|
|||||||
else
|
else
|
||||||
_zlua_call "$_ZL_CD" "$dest"
|
_zlua_call "$_ZL_CD" "$dest"
|
||||||
end
|
end
|
||||||
|
if test -n "$_ZL_ECHO"; pwd; end
|
||||||
end
|
end
|
||||||
if test -n "$_ZL_ECHO"; pwd; end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user