|
|
||
|---|---|---|
| .forgejo/workflows | ||
| tests | ||
| .gitignore | ||
| README.md | ||
| smartcd.plugin.zsh | ||
SmartCD Plugin for Oh My Zsh
A configuration-driven directory navigation plugin for Oh My Zsh that automatically manages your working directory when logging in from excluded directories (e.g., when using su user from /root).
Features
- Automatic directory navigation: Automatically changes to a target directory when logging in from excluded directories
- Last directory tracking: Optionally remembers and returns to your last working directory
- Persistent storage: Choose between session-only or persistent storage of the last directory
- Configurable exclusions: Define which directories should trigger automatic navigation
- Easy configuration: Simple command-line interface for all settings
Installation
Using Oh My Zsh
- Clone this repository into your Oh My Zsh custom plugins directory:
git clone https://git.flaky.network/jf/smartcd ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/smartcd
- Add
smartcdto your plugins list in~/.zshrc:
plugins=(... smartcd)
- Reload your shell configuration:
source ~/.zshrc
How It Works
SmartCD monitors your current directory and automatically navigates to a configured target directory when you log in from an excluded directory. This is particularly useful when:
- Using
su userfrom/root- you'll automatically be taken to your home directory or last working directory - Switching users from restricted directories
- Starting new shell sessions from excluded paths
The plugin tracks your current directory on every change and can optionally remember it across sessions.
Configuration
SmartCD automatically initializes its configuration on first run. The configuration file is created at ~/.oh-my-zsh/custom/smartcd.config.zsh with default settings. No manual setup is required.
The default configuration:
export SMARTCD_EXCLUDE_DIRS=("/root")
export SMARTCD_TARGET_DIR="$HOME"
export SMARTCD_USE_LAST_DIR=false
export SMARTCD_PERSISTENT_STORAGE=false
Usage
Commands
SmartCD provides several commands for configuration:
Exclude Directories
Replace the entire exclude list:
smartcd_set_exclude_dirs /root /tmp /var
Add current directory to exclude list:
smartcd_add_exclude_dir
Add specific directory to exclude list:
smartcd_add_exclude_dir /some/directory
Target Directory
Set the default target directory:
smartcd_set_target_dir ~/projects
Navigation Behavior
Use last directory on login:
smartcd-use-last
Always use target directory on login:
smartcd-use-default
Storage Options
Enable persistent storage (survives reboots):
smartcd-enable-persistent
Disable persistent storage (session-only):
smartcd-disable-persistent
Information
Show current configuration:
smartcd-status
Show help:
smartcd-help
Examples
Basic Setup
If you frequently use su user from /root, you can configure SmartCD to automatically navigate to your home directory:
# This is already the default, but you can verify:
smartcd-status
# The exclude list should include /root
# Target directory should be your home directory
Remember Last Directory
To always return to your last working directory when logging in from excluded directories:
smartcd-use-last
smartcd-enable-persistent # Optional: persist across reboots
Custom Target Directory
To navigate to a specific project directory instead of home:
smartcd-target ~/projects/myproject
Multiple Excluded Directories
To exclude multiple directories:
smartcd_set_exclude_dirs /root /tmp /var/log
Or add directories incrementally:
smartcd_add_exclude_dir /root
smartcd_add_exclude_dir /tmp
smartcd_add_exclude_dir /var/log
How It Works Internally
- On shell startup: SmartCD checks if you're coming from an excluded directory (
OLDPWD) - If excluded: It navigates to either:
- The last directory (if
SMARTCD_USE_LAST_DIR=true) - The target directory (default)
- The last directory (if
- Directory tracking: Every time you change directories, SmartCD saves the current path
- Storage: The last directory is stored either:
- In
/tmp/smartcd_${USER}_session.dir(session-only) - In
~/.smartcd_lastdir(persistent)
- In
Storage Locations
- Session file:
/tmp/smartcd_${USER}_session.dir - Persistent file:
~/.smartcd_lastdir - Config file:
~/.oh-my-zsh/custom/smartcd.config.zsh
Troubleshooting
Plugin not working
-
Verify the plugin is loaded:
smartcd-status -
Check your
~/.zshrcincludessmartcdin the plugins array -
Ensure the config file exists:
ls -la ~/.oh-my-zsh/custom/smartcd.config.zsh
Not navigating on login
- Ensure your previous directory (
OLDPWD) is in the exclude list - Check that
SMARTCD_USE_LAST_DIRorSMARTCD_TARGET_DIRis configured correctly - Verify the target directory exists and is accessible
Configuration not persisting
- Check file permissions on the config file
- Ensure
~/.oh-my-zsh/custom/directory exists and is writable
Development
Running Tests
zsh tests/smartcd_test.zsh