VScode Custom Commands without writing extensions

Wenhoujx
3 min readDec 12, 2022

--

As a productivity addict, you love customize your IDE, but you don’t always have time to write an vscode extension.

Here is a walkthrough how to customize vscode with the simple example to remove blank lines without writing extensions.

Install someone else’s extension.

vscode has a blessed huge community, often someone already published an extension.

I’d rather not use extensions with narrow usage.

Extensions for custom commands.

A few extensions that allows you to write your custom commands:

  1. Commands by the same author of remove empty line extension
  2. multicommand , I use this extension based on its popularity.

Use multicommand to remove blank lines

First install multicommand plugin. Go to user settings.json, by hitting F1(open commands)

In settings.json add the following config:

// ... config above
"multiCommand.commands": [
{ // run this first
"command": "multiCommand.remove_empty_lines",
"sequence": [
{
"command": "editor.actions.findWithArgs",
"args": {
// double-escaped, the final \n to delete the newline.
"searchString": "^(\\s*)$\n",
"replaceString": "",
"isRegex": true,
}
},
{ // run this second
// replace all matches
"command": "editor.action.replaceAll"
},
{ // run this third
// close search widget
"command": "closeFindWidget"
}
],
"when": "editorTextFocus"
},
]

Explanations:

  • editor.actions.findWithArgs I found this after some google search, here is the API. One nice thing about Emacs is you can check out docs of any function with describe-function. Please let lmk if you find a better way with vscode.

Once you have the config in settings.json and make sure it's valid JSON. You can hit F1 to open all commands, and type multicommand,Hit enter, then type remove_empty_lines (likely it's only command) to remove all blank lines!

extras:

  1. Bind a multicommand to a key so that I can get to the list of all my custom commands directly. You can do so in F1 -> Preference: open keyboard shortcuts.

Use Command Runner and shell to remove blank lines

If you are more comfortable with shell than vscode API, Command runner extension is the right tool for you. It allows you define custom commands with shell scripts.

Install the extension and put these into your settings.json:

// configs above
// name of the terminal
"command-runner.terminal.name": "runCommand",
"command-runner.terminal.autoClear": true,
"command-runner.terminal.autoFocus": true,
"command-runner.commands": {
// name of your custom command : shell script.
"remove empty lines": "sed -i '' '/^[[:space:]]*$/d' \"${file}\""
},

invoke f1 -> Run Command , then hit enter , pick your command.

This opens up a runCommand terminal and run the sed command to remove empty lines.

⚠️ the sed command shown here works on OSX, it may behaves differently in different OS

One downside of this is escaping the quotes in a string command might be tricky, this works the best with simple straightforward commands, e.g. git ….

--

--

Wenhoujx

I write about interesting tech stuff i encounter in my startup life.