This article is transcoded by SimpRead, original URL zhuanlan.zhihu.com
Overview
- Autojump is a command-line tool for quickly jumping to directories you have previously visited
- It records your visit history and assigns weights to frequently used directories, allowing you to navigate using short commands
- Greatly improves terminal directory navigation efficiency, reducing the need to type full paths
Installation
macOS
brew install autojump
Configuration
Oh-My-Zsh Configuration
Add autojump to the plugins list in your ~/.zshrc file:
plugins=(git autojump)
Or use the following command to add it automatically:
if ! grep -q "plugins=.*autojump" ~/.zshrc; then
sed -i '' '/^plugins=/s/)/ autojump)/' ~/.zshrc
fi
Manual Configuration (Non-Oh-My-Zsh)
Add the following to your ~/.zshrc file:
[ -f /usr/local/etc/profile.d/autojump.sh ] && . /usr/local/etc/profile.d/autojump.sh
Usage Guide
Basic Commands
Jumping to a Directory
Autojump records directories you have entered via cd, and afterwards you can quickly jump to them with the j command:
j directory_name
For example:
# Jump to ~/projects/website from anywhere
j website
Smart Matching
Autojump intelligently matches partial directory names; the full name is not required:
# Match frequently used directories containing "doc"
j doc
Open File Browser
Open the directory in the graphical interface:
jo directory_name
Advanced Usage
View Directory History and Weights
View Autojump’s recorded directory history and their weights:
j -s
Increase Directory Weight
Manually increase the weight of the current directory:
j -i [weight]
Decrease Directory Weight
Manually decrease the weight of the current directory:
j -d [weight]
Clean Up Invalid Paths
Remove non-existent directory history records:
j --purge
Automated Configuration Script
Here is a complete script for automatic installation and configuration:
#!/bin/bash
# Install autojump
if ! command -v autojump &> /dev/null; then
echo "Installing autojump..."
brew install autojump
else
echo "autojump is already installed"
fi
# Configure Oh-My-Zsh plugin
if [ -f ~/.zshrc ]; then
if ! grep -q "plugins=.*autojump" ~/.zshrc; then
echo "Configuring autojump plugin..."
sed -i '' '/^plugins=/s/)/ autojump)/' ~/.zshrc
echo "Configuration complete, please reload your zsh configuration: source ~/.zshrc"
else
echo "autojump plugin is already configured"
fi
else
echo ".zshrc file not found, please configure manually"
fi
Frequently Asked Questions
-
Why isn’t autojump working?
Make sure you have reloaded the configuration:source ~/.zshrc -
How to reset the autojump database?
Delete the data file:rm ~/.local/share/autojump/autojump.txt -
Why did it jump to the wrong directory?
When multiple directories contain the same name parts, autojump jumps to the directory with the highest weight. Usej -sto check and use a more specific name.