Prefix your user scripts for easy access

Date

This one’s for any Mac, Linux or Windows + WSL users out there.

On my system, I have a bin directory in home folder (~/bin) in my PATH that I put any scripts I have written (user scripts), added to .zshrc via the following line:

export PATH="$HOME/bin:$PATH"

Inside this folder are various small scripts that I may need to access at any given time. These are the kinds of scripts that are tailored to your environment, workflow, etc. A few examples of scripts I have in my bin:

  • A script to switch to a specific project directory, source environment variables from a .env file, set the project’s nvm version and launch a dev server (usually one of these per project)
    • This reduces the friction to start working on an existing project. I can just run the script instead of doing all of the above manually.
  • A simple wrapper script for ripgrep to help search for things in my system
  • A script to launch Chrome (browser) with a specific remote debugging port and user data directory

The problem is, sometimes these scripts aren’t accessed often enough that I have them in my memory, or whatever name I come up with may conflict with another installed utility. There’s a quick tip I want to share that will prevent any naming conflicts, and an easy way to list all of your scripts at any time.

The first step is to prefix your scripts with a special (allowed) character such as , (comma). For example:

  • ,marketplace-ui (a react project)
  • ,chrome-dev-debug
  • ,search

The (comma) prefix is helpful for a few reasons:

  1. I automatically know that any command starting with a comma is a user script from my bin directory.
  2. Prevents name conflicts with other programs installed on the system.
  3. If I need a quick list of my user scripts, I can just type ,<tab> and a list of all commands in my PATH starting with a comma (which are only these scripts) are immediately listed.

To launch one of these scripts, you can do either of the following (regardless of your current working directory):

  • Type the script name and press enter (eg. ,react-project)
  • If the script needs to modify environment variables in the current session, you can source the script (eg. source ,react-project)

NOTE: This only works with scripts that are executable. To make a script executable, add the permission with: chmod +x <script-path>