Don’t let the CD command waste your time

Time:2020-8-1

The most troublesome work under the terminal is path switching. Without the help of automatic path switching, it is like walking in the mud. So we invented various tools such as autojump / Z / FASD to improve the efficiency of path switching. Today, we introduce a better tool: z.lua. After being proficient, it will be like skating in the terminal, and the road force switching will be from where to where.

z. Lua is a person who will learn your habitscdCommand, which tracks the paths you’ve visited under the shell, through a mechanism called fresh (from Mozilla), after a short study,z.luaIt will help you jump to the path with the highest value of fresh among all the paths that match regular keywords.

The regular will match in order, “Z foo bar” can match to / foo / bar, but not to / bar / foo.

Characteristic Description:

  • Performance ratioz.shThree times faster thanfasd / autojumpMore than ten times faster.
  • Support POSIX shell: bash, Zsh, dash, SH, ash, busybox, etc.
  • Supports fish shell, power shell and windows CMD.
  • Use enhanced matching algorithm to take you to where you want to go more accurately.
  • Low footprint, able to update the database only when the current path changes (will$_ZL_ADD_ONCESet to 1).
  • Interactive selection mode, if there are multiple matching results, it allows you to select before jump.
  • Interactive selection mode, which supports visual result filtering using FZF (optional).
  • Quickly jump to the parent directory, or project root, instead of repeating “CD.. /…”.
  • For different operating systems, different shell programs to provide a unified experience.
  • Compatible with Lua 5.1, 5.2 and 5.3.
  • Self contained and no additional dependencies, singlez.luaDocument all the work.

Software installation:

Add the following corresponding statement to your.bashrc / .zshrc / .profileThe corresponding script at the end of the initialization or shell:

Eval "$(Lua / path / to / z.lua -- init bash enhanced once echo)" ා bash initialization
Eval "$(Lua / path / to / z.lua -- init Zsh enhanced once echo)" ා Zsh initialization
Eval "$(Lua / path / to / z.lua -- init POSIX enhanced once echo)" # other POSIX shell initialization
Source (Lua / path / to / z.lua -- init fish enhanced once echo | psub) # fish shell initialization
IEX ($(Lua / path / to / z.lua -- init PowerShell enhanced once echo) - join "` n") ා power shell initialization

Then log in again and you can use it.

User comments:

“I love how it works. I’m very regular at the command line, and I’m too lazy to manage and maintain a set of bookmarks or path aliases. “

“All in all, Z. Lua achieved its stated goal. If you need to quickly switch between directories, it may be the best choice. “

”Finally, I don’t have to wait 30 seconds on the autojump terminal like I used to do on my own

Basic use:

CD to a directory containing foo:

z foo

CD to a directory ending with foo:

z foo$

Use multiple keywords to match long paths:
Suppose there are two records in the path history database (~ /. Zlua)

    10   /home/user/work/inbox
    30   /home/user/mail/inbox

“Z in” will jump to / home / user / mail / box because it has a higher weight. At the same time, you can pass more parameters to z.lua to specify more precisely. For example, “Z in” will let you jump to / home / user / work / box.

Interactive selection:

When using the – I parameter to jump, if there are multiple matching results, z.lua will show you a list:

$ z -i soft
3: 0.25   /home/data/software
2: 3.75   /home/skywind/tmp/comma/software
1: 21     /home/skywind/software
>{cursor position}

Then you enter the place you want to go according to the first serial number. For example, enter 3 to enter/home/data/software。 If you press enter without entering anything, you will exit without jumping.

PS: interactive selection also supports “- I” parameter (capital I), so you can use FZF to quickly select multiple results (if installed in the system).

Quick return to parent directory:

The “- B” option can quickly return to a certain level of parent directory to avoid repeatedly entering “CD.. /…”. Generally, “Z – B” is alias as “ZB” command

  • (no parameters): CD to the project root directory, that is, jump to the parent directory containing (. Git /. SVN /. Hg).
  • (single parameter): CD to the nearest parent directory that starts with a keyword.
  • (two parameters): replace the current directory with a string and then CD it.

Examples of use:

#Go all the way up to the project root (where there is a. Git directory inside)
~/github/lorem/src/public$ zb
  => cd ~/github/lorem

#CD to the first parent directory that begins with G
~/github/vimium/src/public$ zb g
  => cd ~/github

#Quick return to site directory
~/github/demo/src/org/main/site/utils/file/reader/whatever$ zb si
  => cd ~/github/demo/src/org/main/site

#Replace Jekyll with ghost
~/github/jekyll/test$ zb jekyll ghost
  => cd ~/github/ghost/test

Jump back also supports environment variables$_ZL_ECHO(used to display jump results), which makes it possible to match other tools (without changing the current working directory), such asls $(zb)

Bonus“ZB..” is equivalent to “CD..”, “ZB…” is equivalent to “CD….”, while “ZB…” is equivalent to “CD.. /…” and so on. Finally, “ZB.. 20” is equivalent to calling “CD…” twenty times.

Common skills:

Some common aliases are recommended in your profile:

Alias zz'z - C 'ා strictly matches the subpath of the current path
Alias Zi ` Z - I 'ා using interactive selection mode
Alias ZF = - Z - I 'ා use FZF to select multiple results
Alias ZB'z - B 'ා quickly return to the parent directory

z. Lua can easily import data from z.sh (they have the same format)

cat ~/.z >> ~/.zlua

You can also import autojump data:

FN="$HOME/.local/share/autojump/autojump.txt"
awk -F '\t' '{print $2 "|" $1 "|" 0}' $FN >> ~/.zlua

For more information, refer to the project documentation.