Aaron Medina | GitHub | Linkedin
For developers who prefer to have Visual Studio Code focused on a single project at a time, this tool can be incredibly useful. It ensures that only files related to your current work are visible, avoiding distractions from unrelated projects. As you add new projects to the configured workspaces in the configuration file, the tool dynamically includes them as options, streamlining your workflow.
While there are extensions available for Visual Studio Code that provide robust project management features, this script offers a unique approach. By dynamically loading workspace configurations from a configuration file and listing all subdirectories in the configured workspaces, it ensures that only files related to your current work are visible. This minimizes distractions and keeps your focus on the task at hand.
- Dynamically loads workspaces from a configuration file.
- Lists all subdirectories in the configured workspaces, sorted by oldest-newest.
- Provides a simple terminal-based selection interface.
- Opens the selected folder in Visual Studio Code.
- Bash/Zsh: Ensure you have a Bash/Zsh shell environment available.
- Visual Studio Code: Make sure VS Code is installed and available in your system's PATH.
- Configuration File: A configuration file named
vscode_openworkspaces.confmust be placed in the same directory as the script.
The configuration file should be in a JSON-like format. Each key-value pair represents a workspace name and its corresponding directory path.
{
"Workspace Name": "~/path/to/workspace",
"Another Workspace Name": "~/another/path"
}
- Replace
~/path/to/workspace1and~/path/to/workspace2with the absolute or relative paths to your workspace directories. - Use the tilde (
~) to denote the home directory.
- Clone or copy the script to a directory of your choice.
- Ensure the script is executable:
chmod +x vs-openworkspace.sh
- Add workspace/s to
vscode_openworkspaces.conf - Run the script from the terminal:
./vs-openworkspace.sh
- Follow the on-screen prompts to select a folder from the available options.
- The selected folder will be opened in Visual Studio Code.
-
The script will load the workspace configuration file and display the available folders:
Select a folder from Workspace1 (/path/to/workspace1): [1] Project1 [2] Project2 Select a folder from Workspace2 (/path/to/workspace2): [3] ProjectA [4] ProjectB -
Enter the number corresponding to the folder you wish to open. For example, entering
2will openProject2in VS Code. -
The script validates your input and opens the selected folder:
Opening folder: /path/to/workspace1/Project2 from workspace Workspace1
- If the configuration file is missing or malformed, the script will terminate with an error message.
- If no folders are found in the configured workspaces, the script will notify you and exit.
- Invalid input during folder selection will prompt an appropriate error message and exit the script.
- Colors: The script uses ANSI color codes to enhance readability. You can modify the color codes by changing the variables defined in the script (
RED,GREEN,BLUE, etc.). - Workspace Configuration: Update
vscode_openworkspaces.confto add or modify workspaces.
Make this script easier to use:
-
Make the script executable
-
Create a wrapper function in your shell configuration file (~/.bashrc or ~/.zshrc) to call the script easily
-
Use the script anywhere by typiing the command you used in your wrapper.
- VS Code Command Not Found: Ensure
codeis available in your system PATH. Refer to VS Code documentation for instructions. - Directory Permissions: Verify that the script has read permissions for the workspace directories.
This script is distributed under the MIT License. Feel free to use, modify, and distribute it as needed.
