Skip to content

Commit

Permalink
fix(dotfiles): 🐛 fix cd.aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastienrousseau committed Dec 14, 2024
1 parent 677db19 commit 08cb525
Show file tree
Hide file tree
Showing 2 changed files with 185 additions and 90 deletions.
147 changes: 111 additions & 36 deletions lib/aliases/cd/README.md
Original file line number Diff line number Diff line change
@@ -1,65 +1,140 @@
<!-- markdownlint-disable MD033 MD041 MD043 -->

<img
src="https://kura.pro/dotfiles/v2/images/logos/dotfiles.svg"
alt="dotfiles logo"
alt="Dotfiles logo"
width="66"
align="right"
/>

<!-- markdownlint-enable MD033 MD041 -->

# Dotfiles (v0.2.469)

Designed to seamlessly enhance your shell environment 🐚
**Seamlessly enhance your shell environment 🐚**

Check notice on line 12 in lib/aliases/cd/README.md

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

lib/aliases/cd/README.md#L12

Emphasis used instead of a heading

![Dotfiles banner][banner]

## 🅲🅳 🅰🅻🅸🅰🆂🅴🆂
---

## 🚀 Introduction

This repository includes a robust set of shell aliases and scripts designed to streamline your command-line experience. The `cd` aliases script simplifies filesystem navigation with:

Check notice on line 20 in lib/aliases/cd/README.md

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

lib/aliases/cd/README.md#L20

Expected: 80; Actual: 182

- **Dynamic error handling**
- **Automatic directory listing**
- **Customizable paths for frequent directories**

---

## 🛠️ Features

### 🌟 Navigation Shortcuts

| Alias | Description |
|------------------|--------------------------------------|
| `-` | Switch to the previous directory |
| `..`, `...` | Ascend one or two levels in the tree |
| `....`, `.....` | Ascend three or four levels |
| `hom` | Navigate to the home directory (`~`) |

---

### 📂 Custom Directory Access

Quickly access frequently used directories with predefined shortcuts. You can customize these paths to fit your needs:

Check notice on line 43 in lib/aliases/cd/README.md

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

lib/aliases/cd/README.md#L43

Expected: 80; Actual: 118

| Alias | Directory Path | Description |
|-------|--------------------------|------------------------|
| `app` | `${HOME}/Applications` | Applications directory |
| `cod` | `${HOME}/Code` | Code directory |
| `des` | `${HOME}/Desktop` | Desktop directory |
| `doc` | `${HOME}/Documents` | Documents directory |
| `dot` | `${HOME}/.dotfiles` | Dotfiles directory |
| `dow` | `${HOME}/Downloads` | Downloads directory |
| `mus` | `${HOME}/Music` | Music directory |
| `pic` | `${HOME}/Pictures` | Pictures directory |
| `vid` | `${HOME}/Videos` | Videos directory |

---

### 🔧 System Directories

Effortlessly navigate to critical system directories:

| Alias | Directory Path | Description |
|-------|----------------|--------------------------------|
| `etc` | `/etc` | System configuration directory |
| `var` | `/var` | Variable files directory |
| `tmp` | `/tmp` | Temporary files directory |

---

### ⚙️ Enhanced Customization

- **Dynamic Directory Paths**: Customize aliases through environment variables
- **Error Handling**: Provides clear messages for invalid directories
- **Optional Directory Listing**: Automatically lists contents after navigation
- **Tab Completion**: Supports custom tab completion for aliases

---

## 📦 Installation

1. **Clone the repository**:
```bash

Check notice on line 83 in lib/aliases/cd/README.md

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

lib/aliases/cd/README.md#L83

Fenced code blocks should be surrounded by blank lines
git clone https://github.com/sebastienrousseau/dotfiles.git
```

2. **Source the script in your shell configuration**:

```bash
echo 'source /path/to/dotfiles/cd.sh' >> ~/.bashrc
```

3. **Reload your shell**:

```bash
source ~/.bashrc
```

---

## 🧑‍💻 Usage

Here are some examples of how you can use the `cd` aliases:

This set of `cd` command aliases provides an intuitive and efficient way to
navigate your filesystem. Incorporating dynamic error handling, optional
directory listing, and customization for an improved command-line experience.
```bash
# Navigate to the Code directory
cod

### Setup
# Ascend two levels in the directory tree
...

To use these aliases, integrate the script into your `.bashrc` or `.bash_profile`.
# Access the Documents directory
doc

### Navigation Shortcuts
# Navigate to the system configuration directory
etc
```

* `-`: Go to the previous directory.
* `..`, `...`, `....`, `.....`: Ascend one to four levels in the directory tree.
* `~`: Navigate to the home directory.
---

### Custom Directory Access
## 📚 Documentation

Leverage aliases for quick access to frequently visited directories:
For advanced configuration and detailed usage examples, visit the [official documentation](https://dotfiles.io).

* `app`: Applications directory
* `cod`: Code directory
* `des`: Desktop directory
* `doc`: Documents directory
* `dot`: Dotfiles directory
* `dow`: Downloads directory
* `mus`: Music directory
* `pic`: Pictures directory
* `vid`: Videos directory
---

### System Directories
## 🛡️ License

Quickly access system directories:
This project is licensed under the [MIT License](https://opensource.org/licenses/MIT).

* `etc`: System configuration (`/etc`)
* `var`: Variable files (`/var`)
* `tmp`: Temporary files (`/tmp`)
---

### Enhanced Customization
## 👨‍💻 Author

Customize directory paths through environment variables to suit your system's
structure and preferences. Use concise aliases (`app`, `cod`, `des`, etc.) for
efficient navigation.
Created with ♥ by [Sebastien Rousseau](https://sebastienrousseau.com)

For a detailed guide on configuring and using these aliases, plus additional
information on each command, visit our [documentation](https://dotfiles.io).
- Website: [https://sebastienrousseau.com](https://sebastienrousseau.com)
- GitHub: [https://github.com/sebastienrousseau](https://github.com/sebastienrousseau)

[banner]: https://kura.pro/dotfiles/v2/images/titles/title-dotfiles.svg
128 changes: 74 additions & 54 deletions lib/aliases/cd/cd.aliases.sh
Original file line number Diff line number Diff line change
@@ -1,56 +1,43 @@
#!/usr/bin/env bash

################################################################################
# 🅳🅾🆃🅵🅸🅻🅴🆂
# Author: Sebastien Rousseau
# Copyright: 2015-2024. All rights reserved
# Description: Enhanced `cd` command aliases with checks, functions, and customization.
# 🅳🅾🆃🅵🅸🅻🅴🆂 - Change directory aliases
# Made with ♥ by Sebastien Rousseau
# License: MIT
# Script: cd.aliases.sh
# Version: 0.2.469
# Website: https://dotfiles.io

# Usage:
# Customize directory paths via variables and use aliases to navigate.
# Example: `cod` to go to the Code directory, `..` to go up one directory.

# Configuration
DOC_DIR="${HOME}/Documents"
VID_DIR="${HOME}/Videos"
# Add more configurable paths here
# This script provides functions and aliases to quickly change directories.
################################################################################

# Check if directory exists and change to it, listing contents optionally
#-----------------------------------------------------------------------------
# Helper Functions
#-----------------------------------------------------------------------------
# Function to change directory with optional listing
change_directory() {
local path="$1"
local path="$1"
local list_contents="${2:-false}"

if [[ -d "${path}" ]]; then
cd "${path}" || exit # Exit if cd fails
else
echo "Directory '${path}' does not exist."
fi
if [[ -d "${path}" ]]; then
cd "${path}" || { echo "Failed to change to directory: ${path}"; return 1; }
echo "Changed directory to: ${path}"
if [[ "${list_contents}" == "true" ]]; then
ls -lh --group-directories-first
fi
else
echo "Error: Directory '${path}' does not exist."
return 1
fi
}

# Parent Directory Shortcuts
alias -- -='cd -'
alias ..='cd ..'
alias ...='cd ../..'
alias ....='cd ../../..'
alias .....='cd ../../../..'

# Home Directory Shortcut
alias hom='change_directory "${HOME}" true'

# Frequently Used Directories
alias doc='change_directory "$DOC_DIR" true' # Documents
alias vid='change_directory "$VID_DIR" true' # Videos
# Define more aliases like the above for other directories

# System Directories (consider checks for system-specific paths)
alias etc='change_directory "/etc" true'
alias var='change_directory "/var" true'
alias tmp='change_directory "/tmp" true'
# Add tab completion for custom aliases
_cd_alias_completion() {
local cur=${COMP_WORDS[COMP_CWORD]}
local dirs=("app" "cod" "des" "doc" "dot" "dow" "mus" "pic" "vid" "etc" "var" "tmp")
COMPREPLY=($(compgen -W "${dirs[*]}" -- "$cur"))
}
complete -F _cd_alias_completion app cod des doc dot dow mus pic vid etc var tmp

# Frequently Used Directories with Improved Error Handling and Customization
#-----------------------------------------------------------------------------
# Frequently Used Directory Variables
#-----------------------------------------------------------------------------
HOME_DIR="${HOME}"
APP_DIR="${HOME}/Applications"
CODE_DIR="${HOME}/Code"
DESK_DIR="${HOME}/Desktop"
Expand All @@ -61,13 +48,46 @@ MUSIC_DIR="${HOME}/Music"
PICS_DIR="${HOME}/Pictures"
VIDS_DIR="${HOME}/Videos"

# Define functions for each alias with checks and optional ls
alias app='change_directory "$APP_DIR" true' # Applications
alias cod='change_directory "$CODE_DIR" true' # Code
alias des='change_directory "$DESK_DIR" true' # Desktop
alias doc='change_directory "$DOCS_DIR" true' # Documents
alias dot='change_directory "$DOTF_DIR" true' # Dotfiles
alias dow='change_directory "$DOWN_DIR" true' # Downloads
alias mus='change_directory "$MUSIC_DIR" true' # Music
alias pic='change_directory "$PICS_DIR" true' # Pictures
alias vid='change_directory "$VIDS_DIR" true' # Videos
#-----------------------------------------------------------------------------
# Parent Directory Shortcuts
#-----------------------------------------------------------------------------
alias -- -='cd -' # Go to the previous directory
alias ..='cd ..' # Go up one level
alias ...='cd ../..' # Go up two levels
alias ....='cd ../../..' # Go up three levels
alias .....='cd ../../../..' # Go up four levels

#-----------------------------------------------------------------------------
# Home and Frequently Used Directories
#-----------------------------------------------------------------------------
alias app='change_directory "${APP_DIR}" true' # Applications
alias cod='change_directory "${CODE_DIR}" true' # Code
alias des='change_directory "${DESK_DIR}" true' # Desktop
alias doc='change_directory "${DOCS_DIR}" true' # Documents
alias dot='change_directory "${DOTF_DIR}" true' # Dotfiles
alias dow='change_directory "${DOWN_DIR}" true' # Downloads
alias hom='change_directory "${HOME_DIR}" true' # Home Directory
alias mus='change_directory "${MUSIC_DIR}" true' # Music
alias pic='change_directory "${PICS_DIR}" true' # Pictures
alias vid='change_directory "${VIDS_DIR}" true' # Videos

#-----------------------------------------------------------------------------
# System Directories
#-----------------------------------------------------------------------------
if [[ -d "/etc" ]]; then
alias etc='change_directory "/etc" true' # System configuration directory
fi

if [[ -d "/var" ]]; then
alias var='change_directory "/var" true' # System variable data directory
fi

if [[ -d "/tmp" ]]; then
alias tmp='change_directory "/tmp" true' # Temporary files directory
fi

#-----------------------------------------------------------------------------
# Dynamic Features
#-----------------------------------------------------------------------------
# Export the function for use in subshells
export -f change_directory

0 comments on commit 08cb525

Please sign in to comment.