javascripttypescriptvisual-studio-codevscode-extensionsvscode-api

Execute command by creating terminal vscode extension api


I want to execute command automatically after creating terminal. I have created terminal with the following code but I dont know how can I execute command automatically.

const terminal = vscode.window.createTerminal("Open Terminal");
terminal.show();

Solution

  • There is a sendText() method on a Terminal instance. The "text" is for your shell command:

    sendText(text: string, addNewLine?: boolean): void
    

    So

    const terminal = vscode.window.createTerminal("Open Terminal");
    terminal.show();
    terminal.sendText('cd ..', true);   // your command as a string here
    

    See vscode api: Terminal.