ArturoDent / command-alias

A vscode extension that can create multiple aliases for any command.
MIT License
3 stars 3 forks source link

Command Aliases

Create your own aliases for commands in vscode. A command can have multiple aliases if you want. The aliases are the command titles/labels that show up in the Command Palette. Any built-in titles are not removed. Some built-in commands don't have any title; this extension can quickly create an simple title for any command.

With v0.5.0 you can create aliases for workbench.action.terminal.sendSequence commands, each with different arguments. See below for an example.


Extension Settings

Commands

Upon selecting any number of commands in the QuickPick, you will be asked for an alias for each one. Manually editing the settings you can add multiple aliases to each command. Using this more automated process you can, at present, add only one alias per command.

If you do not supply an alias for a command a default value will be created: <defaultAlias>.

"command aliases": {
  "explorer.newFile": "touch",
  "explorer.newFolder": "mkdir",
  "history.showPrevious": "<defaultAlias>",
  "undo": "<defaultAlias>"
}

Identical aliases for different commands do work.


You can create multiple aliases for any command either manually (as shown below in the Settings section below) or through the createAliases command process. For each command you choose in the QuickPick an input box will open asking for your alias(es) for that command.

If you want to enter multiple aliases for that command just enter a comma-separated list of those aliases, like:

myAlias1, myAlias2, someOtherAlias

Leading and trailing whitespace will be removed from each alias entered.

If you try to create an alias that is just an empty string, it will converted to a <defaultAlias> and will appear as that in your settings. Also, empty strings will be removed from the input box if you try to enter something like Alias1,,,Alias2. That will be converted to A1ias1,Alias2.

It is necessary to create these default alias entries because vscode requires all commands to have these labels/titles - there would be nothing to display in the Command Palette otherwise.

If you manually create an entry without an alias you will see this error message from vscode on trying to reload:

   no title error message



General demo of the command-alias.createAliases process:


   createAliases command demo




Settings

This extension contributes the following settings:

  1. "command aliases": {}
  2. "commandAlias.category": "someString"

command aliases : are a group of commands and their titles or aliases. You choose a command and add an alias to it. Example in settings.json (user settings):

"command aliases": {

  "explorer.newFile": "touch",
  "explorer.newFolder": ["mkdir", "new directory"],     // multiple aliases : use an array  

  "launches.showAllLaunchConfigs": "QP configs",        // an extension command  

  "workbench.action.reloadWindow": "restart"
}

The commands are the same as those you could copy from the Keyboard Shortcuts list. Use Copy Command ID from each command's context menu to get the actual command.

Commands are then generated from these settings either on load of the extension or when you make any change to its settings. This extension's package.json is updated to contribute these commands and activationEvents.


commandAlias.category: the 'category' is text that precedes your command aliases in the Command Palette, like the word Debug in   Debug: Clear Console.

The default category is Alias so that your commands may appear as Alias: mkdir or Alias: touch for example. Youy can change that preceding word to an empty string or to another word in the UI Settings or in your own settings.json manually.

Examples of different categories as shown in the Command Palette:

   Custom category demo



   empty string category demo



You can re-use aliases for different commands - in that case vscode will show both aliases and the commands they are associated with in the Command Palette so you could pick the one you want. I suppose you could group commands in this way. [You could also group commands with the category setting.]

If you had this in your settings:

  "command aliases": {
    "addRootFolder": "mkdir",
    "explorer.newFolder": "mkdir"
  }

you would see in this in your Command Palette upon typing mkdir:


   duplicate aliases in the commnad palette demo


The gif above uses a commandAlias.category set to the empty string so no category word is shown preceding the command.



Using the command workbench.action.terminal.sendSequence:

If you frequently use this command to send text to the terminal, you can set up aliases so they wil show up in the Command Palette. [\u000d (same as \r) is unicode for a return so the terminal command runs immediately, it is up to you whether you want that.]

"command aliases": {

  "explorer.newFile": "touch",
  "explorer.newFolder": ["mkdir", "new directory"],
  "git.checkout": "Git: Switch to...",

  "workbench.action.terminal.sendSequence": [
    { "Run Last Terminal Command": { "text": "\u001b[A\u000d" } },
    { "Set Terminal to Current File Folder": { "text": "cd '${fileDirname}'\r" } }
  ]
},

   command palette sendSequence commands demo


Although you can use command titles like "Run Last Terminal Command" in the settings, vscode does not allow commands with spaces. So that command would necessarily be automatically changed to "Run_Last_Terminal_Command" behind the scenes. The version without spaces should be used in any keybindings or macros. Like:

{
  "key": "alt+t",
  "command": "Set_Terminal_to_Current_File_Folder"
},
{
  "key": "alt+x",
  "command": "Run_Last_Terminal_Command"
}

You can get this altered form of the commands by right-clicking the command in the Keyboard Shortcuts and selecting Copy Command ID or simply change all spaces to underscores.





You can eliminate any aliases from vscode by deleting or commenting-out the settings - then reloading as prompted.



Requirements

Demo using createAliases with a dirty - unsaved - settings.json file:


   reload message



If you choose not to save your settings.json the changes will not be made.


Or, of course, any changes will take affect the next time vscode is started.


reload message



Known Issues

Release Notes

TODO

[ X ] - Explore the ability to use command titles as command names in keybindings.
[  ] - Explore the ability to use multiple args for certain commands.
[  ] - Explore the ability to specify any number of categories and assign to different commands.