cd by golang
C
hange Current workingD
irectory Fast ๐,I
ntelligently ๐ฌ.And Ergonomically ๐ฆ in the Aspect of Human Searching Preferences.
cd
ๅฝไปค golang ่ฟ้ถ็cdi
ใ็ฌฆๅไบบไฝๅทฅ็จๅญฆๆ็ดขไน ๆฏ็cd
ๅฝไปค
Use cd
$ cd mb
cd: no such file or directory: mb
๐ก
Use cdi
$ cdi mb
/Users/legend80s/workspace/mini-balance
โ mini-balance git:(master) โ
๐
- ๐ฌ Features
- Download
- Usage
- More interesting usage ๐ค
- Development
- Testing
- Build
- Publish
- Version History
- What does the
install.sh
do
๐ฌ Features
โ๏ธ ๐ฌ Intelligent and ergonomical matching. Designed with an emphasis on ergonomics. Your search comfort and speed is a priority.
โ๏ธ ๐ Speed! Powered by Golang.
โ๏ธ ๐ Speed! Histories will be stored in a db file (~/cdi-db-shortcuts.json
) for search speed.
โ๏ธ ๐ Speed! node_modules and other black hole directories wont be searched.
Download
To install cdi
, you should run the install script:
cd ~ && git clone --depth 1 https://github.com/legend80s/cdi-go.git && sh ~/cdi-go/scripts/install.sh ~/cdi-go/cdi-v5 && source ~/.zshrc
To update, you should run the update script:
cd ~ && rm -rf ~/cdi-go && git clone --depth 1 https://github.com/legend80s/cdi-go.git && sh ~/cdi-go/scripts/install.sh ~/cdi-go/cdi-v5 update
To uninstall just remove cdi and codi functions in ~/.zshrc
.
If the download takes too long or you are very security concerned, jump to FAQ Install mannually.
Usage
Suppose we have these directories in ~/workspace/
cli-aid
commit-msg-linter
gallery-server
js2schema
Just use cdi
instead of the builtin cd
command
$ cdi ca
will cd
into ~/workspace/cli-aid
Ergonomically
Ergonomics or human engineering, science dealing with the application of information on physical and psychological characteristics to the design of devices and systems for human use.
Weight or priority from highest to lowest:
- Full match basename:
cdi js2schema
equal tocd ~/workspace/js2schema
- Prefix match:
cdi cli
equal tocd ~/workspace/cli-aid
- Abbr match:
cdi ca
equal tocd ~/workspace/cli-aid
- Contains word match:
cdi msg
equal tocd ~/workspace/commit-msg-linter
Suppose we have these directories in ~/workspace/
:
dir-lab
โโโ amuser-low-info
โโโ long
โ โโโ ali
โ โโโ ali-test
โ โโโ alitest
โ โโโ hello-ali-test
โ โโโ hello-alitest
โโโ long-long-long-long-long
โโโ ali
cdi ali
will match:
dir-lab
โโโ amuser-low-info โ
โโโ long
โ โโโ ali โ
โ โโโ ali-test โ
โ โโโ alitest โ
โ โโโ hello-ali-test โ
โ โโโ hello-alitest โ
โโโ long-long-long-long-long
โโโ ali โ
After sorted by priority then by length:
Full match
โ dir-lab/long/ali
โ dir-lab/long-long-long-long-long/ali
Prefix
โ dir-lab/long/alitest
โ dir-lab/long/ali-test
Abbr
โ dir-lab/amuser-low-info
Contains word
โ dir-lab/long/hello-ali-test
So the best match is dir-lab/long/ali
.
Advanced Usage
1. Set search dir
The default search dir is ~/workspace
, change it to work
:
cdi set-root ~/work
2. List saved shortcuts
cdi stat
Outputs file content in ~/cdi-db-shortcuts.json
3. Clear saved shortcuts
cdi clear
4. Force search the dir tree instead of from cache
--walk
Example:
cdi --walk balance
More interesting usage ๐ค
code
command much more intelligent and convenient!
Make your VSCode What is code command? Read Launching VSCode from the command line.
You can
codi dir-you-want-to-open-in-vscode
instead of executing 2 commands
cd ~/the/long/long/and/hard-to-memorize-dir-that-you-want-to-open-in-vscode
code .
Put this in your ~/.zshrc:
# Intelligent `code` command `codi`
codi() {
target=$($cdipath "$@")
echo $target
if [[ $target == *"no such dirname"* ]]; then
# DO NOTHING WHEN NO DIRECTORY FOUND
else
code $(cdi-echo $1)
fi
}
Development
go run main.go -walk {directory-to-cd}
Testing
go test ./utils
Build
go build -v -o cdi
Publish
git tag v1.x.x && gp && gp --tags
Version History
V1 โ
Match against without priority
- full match
- abbr
- abbr contains
- basename letter contains
Then pick the shortest one.
V2
Priority or weight should be taken into account.
Match priority
- full match
- abbr
- abbr contains
- basename letter contains
Then pick the one with highest priority if priority equals then pick the shortest one.
V3
Match priority
- full match
- prefix
- abbr
- basename word contains
Then pick the one with highest priority if priority equals then pick the shortest one.
V4 โ
Directory nested level should be taken into account.
Match priority
- full match
- prefix
- abbr
- basename word contains
Then conditionally sort by length or directory nested level.
- If the directories take the same priority , we pick the shortest one.
- Otherwise we pick the directory with least nested level, instead of the the one with higher priority but has more nested level greater than 1.
Directory nested level sorting
We choose the directory with least nested level when the counterparts's level is too deep. "too deep" means level diff > 1
For example:
/Users/name/workspace/6/paytm/mr # (priority 0, directory nested level 6)
/Users/3/MiniRecharge # (priority 2, directory nested level 3)
When we cdi mr
, though the first path paytm/mr
is matched mr
by "full match" with highest priority but its nested level is too deep (6).
But the second path /Users/3/MiniRecharge
matched by "abbr" though with much lower priority but has lower nested level 3.
6 - 3 = 3 > 1
. So cdi mr
will cd
into the second path /Users/3/MiniRecharge
.
In V2 or V3 it will cd
into /Users/name/workspace/6/paytm/mr
because V3 always pick the one with highest priority.
In V1 it will cd
into /Users/3/MiniRecharge
because V1 always pick the shortest path.
V5 โ
่ฟ็ๅฝ็๏ผ็ฎๅๅณ็พ
Match against without priority
- full match
- prefix
- abbr
- basename word contains
Then pick the least nested level directory.
FAQ
Install manually
-
Download the cdi cmd https://raw.githubusercontent.com/legend80s/cdi-go/master/cdi-v5.
-
Make it executable:
chmod +x ~/path/to/downloaded/cdi && xattr -c ~/path/to/downloaded/cdi
-
Copy the script in what-does-the-installsh-do to your
~/.zshrc
. -
Make it alive:
source ~/.zshrc
-
Give it a try:
cdi whatever-directory-you-want-to-jump-into codi whatever-directory-you-want-to-open-in-vscode
install.sh
do
What does the It will add the shell functions to your .zshrc
because you cannot change shell directory in golang process.
## --- cdi begin ---
cdipath="~/path/to/downloaded/cdi"
cdi() {
target=$($cdipath -fallback "$@")
echo $target
cd $target
}
# Show debug info
cdi-echo() {
target=$($cdipath "$@")
echo target
}
# Intelligent `code` command `codi`
codi() {
target=$($cdipath "$@")
echo $target
if [[ $target == *"no such dirname"* ]]; then
# DO NOTHING WHEN NO DIRECTORY FOUND
else
code $(cdi-echo $1)
fi
}
# Show cache
alias cdi-stat="$cdipath stat"
# Clear cache
alias cdi-stat-clear="$cdipath stat --clear && echo -n 'Clear cache success. ' && cdi-stat"
## --- cdi end ---
TODO
- Faster directory search, use concurrent search logic like
fd