DEV Community

Le Vuong
Le Vuong

Posted on • Updated on

VSCode Tips

Run shell script with editor content as input.

Looking to harness the capabilities of two powerhouse tools? Now you can execute shell scripts using the content directly from your VSCode editor.

Ref: stackoverflow

  • Create a keyboard shortcut (Ctrl-Shift-P > Open keyboard shortcut (JSON) or openGlobalKeybindingsFile)
  • In the keybindings.json file, add a new keyboard shortcut like below:
    {
        "key": "alt+t",         // whatever you want for a keybinding
        "command": "workbench.action.terminal.sendSequence",
        "args": {
          "text": "echo '${selectedText}' | sort - \u000D"
        }
    }   
Enter fullscreen mode Exit fullscreen mode

This keybinding will echo the selected text in the editor and pipe it to the sort command, sorting the text alphabetically. The \u000D represents the Enter character to execute the command.

Feel free to customize the keybinding to suit your needs by replacing the echo '${selectedText}' | sort - with any shell command you prefer.

PS: It will even better if we can use a plugin that can accept commands as input. But I can't find one for now How about passing input to the bound key? Updated: I have created a simple VSCode extension for this. See more details in the comment below.

Top comments (1)

Collapse
 
patfinder profile image
Le Vuong

I just made a VSCode extension to allow user to execute shell commands with selected editor content as input. This will allow you to use the shell commands even more conveniently (no custom keybinding definition needed, commands can be edited).
This repo is still very basic (though useful), so you can add more functionalities if you want.