Streamline Your Development Workflow with Custom Commands and Aliases

During development, we often find ourselves repeating the same commands and tasks over and over again. Not only is this repetitive and time-consuming, but it can also lead to errors and frustration. Fortunately, there is a solution: custom commands and aliases. By creating these shortcuts, we can streamline our workflow, save time, and boost our productivity. Here's some tips and tricks I'm using frequently.

Change Directory to Recent Folder

If you're like many developers, you probably have a set of frequently used directories that you navigate to often. To make this process more efficient, you can create a custom command or alias to quickly change directory to your desired location.

Before we get started, it's important to determine which shell you are using. To check, open up your terminal and enter the following command:

echo $SHELL

This will display the name of your current shell, which is typically either Bash or Zsh.

Now, let's say that you frequently work with projects in the /Users/admin/Documents/GitHub directory, and you want to quickly change directory to the folder with the latest modification time. To do this, you can create an alias that utilizes the cd command along with the om qualifier to sort directories by modification time and select the most recent one.

If you're using Zsh, you can create the alias by adding the following line to your ~/.zshrc file:

alias cdgit='cd /Users/admin/Documents/GitHub/*(/om[1])'

If you're using Bash, you can add the following line to your ~/.bashrc file instead:

alias cdgit='cd /Users/admin/Documents/GitHub/$(ls -t /Users/admin/Documents/GitHub | head -n1)'

With this alias in place, you can now change directory to your latest modified project folder by simply entering the command cdgit in your terminal. If you want to change the folder, you can use the touch command to update the folder's modified timestamp and ensure it becomes the latest modified folder. This saves you the hassle of navigating through the directory tree manually, helping you to work more efficiently and effectively.

Generate a Random Password

Creating secure passwords is essential, especially when you need to generate a random key for an API or other sensitive information. Fortunately, you can easily generate a random password using the OpenSSL command-line tool.

To generate a random password, simply enter the following command in your terminal:

openssl rand -base64 20

This will generate a random 20-character password using OpenSSL's built-in random number generator.

To make this process even easier, you can create an alias for the command. For example, you can add the following line to your ~/.bashrc or ~/.zshrc file:

alias genpass='openssl rand -base64 20'

With this alias in place, you can now generate a random password by simply entering the command genpass in your terminal. This can save you time and effort when you need to create secure passwords on a regular basis.

Create a "work" Command

Have you ever found yourself repeatedly typing the same commands over and over again while working on your computer? Maybe you need to connect to a remote server, run a backup, or perform some other routine task that involves multiple steps. If so, you can save time and effort by creating a custom command script that automates these tasks for you.

One way to do this is by creating a "work" command in your terminal. This command can be used to trigger a script that performs a specific set of tasks based on the arguments passed to it. To create a "work" command, you can start by creating a zsh or bash script file and naming it "work".

Inside the "work" file, you can use a switch case to define different commands and their corresponding actions. For example, you might define a command "connect-test" that connects you to a test server via SSH. Here's an example of how the switch case might look:

#!/bin/zsh

case ${1} in
        completion)
                view ~/Documents/Scripts/bin/completion.zsh
                ;;
        edit)
                view ~/Documents/Scripts/bin/work
                ;;
        connect-test)
                ssh -o "ServerAliveInterval 60" root@192.168.100 -L3306:127.0.0.1:3306
                ;;
        ## more commands here
        *)
                echo "`basename ${0}`:usage: [connect-test|edit|completion]"
                exit 1
                ;;
esac

In this example, the "connect-test" command uses the ssh command to connect to a remote server with a specific IP address and port forwarding. You can define additional commands in the same way, using the appropriate syntax for each command.

To make your "work" command more user-friendly, you can also create a completion script that suggests possible commands when you start typing the "work" command. For example, you might create a completion script that suggests "connect-test" when you type "work c" and press tab. Here's an example of how the completion script might look:

# completion.zsh

_my_args=(
        "completion"
        "edit"
        "connect-test"
)

_work() {
        compadd "$_my_args[@]"
}

To use the "work" command, simply type "work" followed by the name of the command you want to execute. For example, if you want to connect to your test server, you would type "work connect-test" in your terminal. If you want to add more commands to your "work" command, you can simply edit the "work" file and add additional cases to the switch statement. You can also use the "edit" command to quickly open the "work" file in your default text editor.

In summary, creating a "work" command is a great way to automate routine tasks and save time while working on your computer. With a little bit of scripting and customization, you can create a command that works for your specific needs and preferences.