No description
Find a file
Jared Falkenhagen b15fb9ae2e
Some checks failed
Run Tests / test (push) Failing after 8m32s
Fix Action execution
2025-11-17 22:10:10 +01:00
.forgejo/workflows Fix Action execution 2025-11-17 22:10:10 +01:00
tests Add more tests, improve help, remove aliases, add README 2025-11-17 20:42:19 +01:00
.gitignore Oh my Zsh Plugin for automatic switching paths added 2025-11-17 20:29:25 +01:00
README.md Update README: remove contributing/license/author sections 2025-11-17 20:46:28 +01:00
smartcd.plugin.zsh Add more tests, improve help, remove aliases, add README 2025-11-17 20:42:19 +01:00

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

  1. 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
  1. Add smartcd to your plugins list in ~/.zshrc:
plugins=(... smartcd)
  1. 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 user from /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

  1. On shell startup: SmartCD checks if you're coming from an excluded directory (OLDPWD)
  2. If excluded: It navigates to either:
    • The last directory (if SMARTCD_USE_LAST_DIR=true)
    • The target directory (default)
  3. Directory tracking: Every time you change directories, SmartCD saves the current path
  4. Storage: The last directory is stored either:
    • In /tmp/smartcd_${USER}_session.dir (session-only)
    • In ~/.smartcd_lastdir (persistent)

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

  1. Verify the plugin is loaded:

    smartcd-status
    
  2. Check your ~/.zshrc includes smartcd in the plugins array

  3. 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_DIR or SMARTCD_TARGET_DIR is 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