Earlier I wrote a post where you can customize VScode with 3rd party extensions such as MultiCommand, and CommandRunner .
Recently I opened the magic box of vscode tasks. I will walk though some super helpful workflow I used to do daily in emacs and hoped they exists in vscode.
Custom Tasks
For project level custom tasks, open .vscode/tasks.json
. These tasks are only available in current project.
For user custom tasks, go to command palette (F1
), type user tasks
. These tasks are available across projects.
Quick Switch to Git Main branch
This is a common workflow i do daily, so i can’t stand to use mouse to click. Goto tasks.json . The following task defins a simple task that quickly switch to main
{
"version": "2.0.0",
"tasks": [
{
"label": "To Main",
"command": "git checkout main",
"type": "shell",
"problemMatcher": []
}
]
}
Quick creation of git branch with custom prefix.
I like to prefix my git branches with whou/1016/
, that is my username and the month and date. This way i can quickly organize my branch and never run out of one time branch names such as quickfix
bugfix
.
Go to your tasks.json
, i choose the user level one.
{
"version": "2.0.0",
"tasks": [
{
"label": "Create Branch",
"command": "git checkout -b whou/$(date +'%m%d')/${input:branch-base}",
"type": "shell",
"problemMatcher": []
}
],
"inputs": [
{
"id": "branch-base",
"description": "branch name to create",
"type": "promptString"
}
]
}
here i define custom task backed by a shell command:
(date +%m%d)
gives me the current month and dateinput:branch-base
is defined in theinputs
section where i prompt user for input.
Invoking this by hitting F1
and type run tasks
, then pick the Create Branch, which will prompt you for a branch base, prefix it with username and month/date, then create it in a terminal.
Quick open or create pull request for current branch
Once you are done coding, you need to create PR. VScode GIT extension requires multiple clicks: open git view, open branches, find the right branch, click the create pull request icon.
Thankfully you can go to https://github.com/<owner>/<repo>/pull/<branch-name>
and lands in a pull request view or create pull request view.
The following task opens the URL in browser:
{
"version": "2.0.0",
"tasks": [
{
"label": "Currnet PR",
"command": "open https://$(git config --get remote.origin.url | sed -E 's|^git@(.*):(.*)/(.*)\\.git$|\\1/\\2/\\3|')/pull/$(git rev-parse --abbrev-ref HEAD)",
"type": "shell",
"problemMatcher": []
}
]
}
Conclusion:
Since the tasks can be powered by shell command, the potential is unlimited. You can even write python scripts and invoke it with python...
.
Custom tasks fills the gap of quick customization of my IDE, like what i used to do a lot in my emacs.