Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to execute command across multiple files in VS Code?

I would like to optimize my typescript code by running some commands (e.g. resolve.organizeImports that comes with TS Hero plugin) in all of my typescript (.ts) files.

Doing this file by file can be quite tiresome and time consuming. Is there an easy way of doing this sort of 'bulk' execute?

like image 289
Enn Avatar asked Dec 05 '17 11:12

Enn


People also ask

How do I run all files in VS Code?

Launch VS Code and press the “Ctrl” and “P” keys at the same time to search for a file to open in the current project. Type in the file name. To open the new file in a temporary tab, click on it once. To open the new file in a separate window that you can choose to close manually, double-click it.

How do I select multiple files in VS Code?

You can select multiple files in the File Explorer and OPEN EDITORS view to run actions (Delete, Drag and Drop, Open to the Side) on multiple items. Use the Ctrl/Cmd key with click to select individual files and Shift + click to select a range.


1 Answers

I have written the extension Command on All Files. It is a modification of the extension Format All Files in Workspace by Alex Ross.

You can configure multiple commands that you want to run on all files and configure to which files it should apply. You can override the includeFileExtensions and excludeFolders setting per command.

If you use multi-command by ryuta46 you can create a sequence of commands that you want to apply to each file. (Why recreate what already is implemented) For an example see the extension page.

For the organizeImports from the TS Hero plugin you can have this configuration

settings.json

  "commandOnAllFiles.commands": {
    "TS Hero: Organize Imports": {
      "command": "typescriptHero.imports.organize",
      "includeFileExtensions": [".ts"]
    }
  }

keybindings.json

  {
    "key": "ctrl+i o", // or any other key combo
    "command": "commandOnAllFiles.applyOnWorkspace",
    "args": ["TS Hero: Organize Imports"]
  }

For version 0.1.0 of the extension only the method of the keybinding is implemented to supply the argument to the commandOnAllFiles.applyOnWorkspace command. In the next release it can be done from the Command Palette.

like image 137
rioV8 Avatar answered Oct 27 '22 06:10

rioV8