I’m using the “Run on Save” extension in VSCode to execute custom commands when I save my files. I have a custom bash function pdx3 defined in my .bashrc file. However, when I save a file, the command fails with an error that pdx3 is not found. Here is my current configuration:
{
"runOnSave.commands": [
{
"match": ".*",
"cmd": "pdx3"
}
]
}
I suspect this is because VSCode is not sourcing my .bashrc file, and hence the pdx3 function is not available in the environment. How can I ensure that my custom bash functions and aliases are recognized and executed by the “Run on Save” extension in VSCode?
What I’ve Tried:
- Ensuring Environment Variables Are Inherited:
Set “terminal.integrated.inheritEnv”: true in settings.json to make sure the integrated terminal inherits environment variables. - Sourcing .bashrc Directly:
Updated the command to source .bashrc before executing pdx3:
{
"runOnSave.commands": [
{
"match": ".*",
"command": "source ~/.bashrc && pdx3",
"runIn": "terminal",
"runningStatusMessage": "Running pdx3...",
"finishStatusMessage": "pdx3 completed."
}
]
}
- Specifying the Shell:
Made sure the shell being used by VSCode is /bin/bash by setting the runOnSave.shell option:
{
"runOnSave.shell": "/bin/bash",
"runOnSave.commands": [
{
"match": ".*",
"command": "source ~/.bashrc && pdx3",
"runIn": "terminal",
"runningStatusMessage": "Running pdx3...",
"finishStatusMessage": "pdx3 completed."
}
]
}
References:
Run on Save – Visual Studio Marketplace
Command not found error in VS Code extension – Coding Beauty
Auto run command from vscode extension on file save – Stack Overflow
VS Code: Keep Your Code Clean by Running Extension Commands on Save – Business Central Deep Dive