Visual Studio Code Terminal Profile Usage Instructions

Beatme CodeBY-NC-ND
16:45 read·2179 words· published

The “terminal profile” described in this article is not the file that contains all the settings for Visual Studio Code’s Terminal.

Visual Studio Code Terminal Profiles

Visual Studio Code terminal profiles are not stored in a separate file, but in the setting item terminal.integrated.profiles.<platform> in the Visual Studio Code settings file settings.json, where platform is the operating system, which can be linux, windows or osx. This allows developers to customize Visual Studio Code Terminal differently for different operating systems Linux/Windows/macOS.

The Visual Studio Code setting item terminal.integrated.profiles.<platform> is an object containing the Visual Studio Code terminal profile for the corresponding operating system. The name of the object’s properties is the name of the terminal profile, and the value (also an object) of the object’s properties is the contents of the terminal profile.

settings.json
{
	// Terminal profiles on Windows Systems
	"terminal.integrated.profiles.windows": {
		// Terminal profile A Shell
		"A Shell": {
			"path": "<Path information for the shell program>",
		},
	},
}

Visual Studio Code Built-in Terminal Profiles

By default, the Visual Studio Code workspace or the user settings file settings.json may not contain the setting item terminal.integrated.profiles.<platform>, but this does not affect the normal use of Visual Studio Code Terminal, because Visual Studio Code automatically recognizes the command-line applications (shell programs) available on the current system and has some built-in terminal profiles.

New Visual Studio Code Terminal Profiles

When launching a new command-line application (shell program), if Visual Studio Code does not list the command-line application you expect, you can add a new property (setting item) to the setting item terminal.integrated.profiles.<platform> in the Visual Studio Code settings file settings.json to represent the new terminal profile, and point the terminal profile to the target command-line application.

Visual Studio Code terminal profiles are represented in the settings file settings.json also as objects, which have the following two important properties (setting items).

path property

path can be the path or alias of the command-line application corresponding to the Visual Studio Code terminal profile. You need to make sure that Visual Studio Code or the operating system is able to locate the target command-line application based on the value of path and certain environment variables (such as PATH); otherwise, Visual Studio Code may not list that terminal profile in the user interface.

The path property can also be an array of paths or aliases to the command-line application that will be used to find the target command-line application in order of precedence.

source property

If path is not specified, then you can set the source property, which is only valid for the setting item terminal.integrated.profiles.windows and has only two valid values PowerShell and Git Bash, which indicate that using the Visual Studio Code built-in terminal profile PowerShell or Git Bash.

Although you are using the Visual Studio Code built-in terminal profile, you can overwrite certain properties, for example, by adding a valid path profile to specify the target command-line application.

The following terminal profile, New PS, will take Visual Studio Code’s built-in terminal profile, PowerShell, and overwrite its path property to cmd.

settings.json
{
	// Terminal profiles on Windows Systems
	"terminal.integrated.profiles.windows": {
		// Terminal profile New PS
		"New PS": {
			// This will use the built-in terminal profile PowerShell
			"source": "PowerShell",
			// Overwrite the path property and start the Windows Command Prompt instead
			"path": "cmd",
		},
	},
}

Add Startup Arguments to Visual Studio Code Terminal

You can add startup arguments to command-line applications (shell programs) launched by Visual Studio Code Terminal through the property (setting item) args of the Visual Studio Code terminal profile, which is an array where each startup argument (name) or argument value corresponds to an element of the array.

Putting the name and value of a startup argument of Visual Studio Code Terminal into the same string is not supported

For the args property of the Visual Studio Code terminal profile, do not put the name of the startup argument and the value of the startup argument in the same string, e.g., ["-OutputFormat XML"], which may cause Visual Studio Code Terminal to fail to launch the command-line application properly, the correct practice is to separate the argument name from the argument value, such as ["-OutputFormat","XML"].

Although the args property of the Visual Studio Code terminal profile is expected to be an array, it is feasible (under certain conditions) to set it to a single string, e.g., "-OutputFormat XML" may have the same effect as ["-OutputFormat","XML"].

The following Visual Studio Code terminal profile, PS-parameters, adds the startup argument -OutputFormat to the command-line application PowerShell, with a value of XML.

settings.json
{
	// Terminal profiles on Windows Systems
	"terminal.integrated.profiles.windows": {
		// Terminal profile PS-parameters
		"PS-parameters": {
			// This will use the built-in terminal profile PowerShell
			"source": "PowerShell",
			// Add startup arguments for PowerShell
			"args": [
				// Startup argument -OutputFormat with the value XML
				"-OutputFormat", "XML",
			],
		},
	},
}

Manage Environment Variables in Visual Studio Code Terminal

The property (setting item) env of the Visual Studio Code terminal profile is an object that can be used to manage environment variables in the command-line application (shell program) launched by Visual Studio Code Terminal. The name of the property of this object is the name of the environment variable and the value of the property is the value of the environment variable. The environment variables contained in the env property will be added to the command-line application or overwrite environment variables of the same name in the command-line application. If you want to remove an environment variable that is already in the command-line application, set the value of its corresponding property in env to null.

You may need to restart Visual Studio Code for the env property of the modified terminal profile to take effect

If you modify the env property of the Visual Studio Code terminal profile, for example, by adding a new environment variable for the command-line application or overwriting an existing environment variable, these changes will not be effective for an already open command-line application, and a restart of Visual Studio Code may be required for the changes to take effect.

Command-line applications may not fully support the env property of the Visual Studio Code terminal profile

It should be noted that some command-line applications may not fully support managing environment variables through the env property of the Visual Studio Code terminal profile, for example, some command lines may not support adding environment variables through env.

The following Visual Studio Code terminal profile, CMD-variables, changes the environment variable OS and adds the environment variable OS_OTHER (which may require a restart of Visual Studio Code), and after launching the Command Prompt corresponding to this terminal profile, we check the environment variables OS and OS_OTHER with the echo command.

settings.json
{
	// Terminal profiles on Windows Systems
	"terminal.integrated.profiles.windows": {
		// Terminal profile CMD-variables (may require restarting Visual Studio Code)
		"CMD-variables": {
			// Start the Windows Command Prompt
			"path": "cmd",
			"env": {
				// Change the existing environment variable OS
				"OS": "Hello world!",
				// Add a new environment variable OS_OTHER
				"OS_OTHER": "Hello other worlds!",
			}
		},
	},
}
# Command Prompt
echo %OS%
Hello world!
echo %OS_OTHER%
Hello other worlds!

How to manage environment variables in all command-line applications of Visual Studio Code?

With the Visual Studio Code setting item terminal.integrated.env.<platform> (platform is one of linux, windows, or osx), you can add, remove, or change the environment variables for all command-line applications in a given operating system. The usage is similar to the env property of the Visual Studio Code terminal profile, except that when the terminal.integrated.env.<platform> and env properties contain the same environment variable, the env property takes precedence.

Although we removed the environment variable OS in the Visual Studio Code setting item terminal.integrated.env.windows, the terminal profile CMD-override re-added it, and after launching the Command Prompt corresponding to the terminal profile, you can view the environment variables OS and OS_OTHER with the echo command.

settings.json
{
	// Manage environment variables on the command line in Windows
	"terminal.integrated.env.windows": {
		// Delete the existing environment variable OS
		"OS": null,
		// Add a new environment variable OS_OTHER
		"OS_OTHER": "Other worlds!",
	},
	// Terminal profiles on Windows Systems
	"terminal.integrated.profiles.windows": {
		// Terminal profile CMD-override (may require restarting Visual Studio Code)
		"CMD-override": {
			// Start the Windows Command Prompt
			"path": "cmd",
			"env": {
				// Re-add the deleted environment variable OS
				"OS": "Hello world!",
			}
		},
	},
}
# Command Prompt
echo %OS%
Hello world!
echo %OS_OTHER%
Other worlds!

Set the Name, Icon, Color of Visual Studio Code Terminal

The properties (setting items) icon and color of the Visual Studio Code terminal profile allow you to set the icon and the icon color of the command-line application (shell program) launched by Visual Studio Code Terminal. The property overrideName allows you to set whether the title of the command-line application launched by Visual Studio Code Terminal is changed to the name of the terminal profile, and if it is set to true (the default is false), the title is the name of the terminal profile.

settings.json
{
	// Terminal profiles on Windows Systems
	"terminal.integrated.profiles.windows": {
		// Terminal profile PS-customized
		"PS-customized": {
			// This will use the built-in terminal profile PowerShell
			"source": "PowerShell",
			// Replace the title of the command-line application with the name of the terminal profile PS-customized
			"overrideName": true,
			// Set the icon of the terminal
			"icon": "calendar",
			// Set the icon color of the terminal
			"color": "terminal.ansiGreen",
		},
	},
}

Terminal,Appearance and Styles

You can change only the name, icon and color of a specific command-line application launched by Visual Studio Code Terminal, as described in the section Change the Name, Icon, Color of Visual Studio Code Terminal, or you can specify the default name, description, icon and icon color of a command-line application through the Visual Studio Code setting item, as described in the section Customize the Default Name, Description, Icon, Color of Visual Studio Code Terminal Tabs.

Set the Default Visual Studio Code Terminal Profile

With the Visual Studio Code setting item terminal.integrated.defaultProfile.<platform> (platform is one of linux, windows or osx), you can specify a default terminal profile for Visual Studio Code in a particular operating system, usually this means specifying the command-line application (or different startup arguments) that Visual Studio Code Terminal launches by default, and the value of this setting item can be the name of any valid Visual Studio Code terminal profile.

If the Visual Studio Code setting item terminal.integrated.defaultProfile.<platform> is ignored, or set to null, then Visual Studio Code will attempt to use the command-line application PowerShell on Windows systems, and the command-line application (shell program) specified by the environment variable SHELL (case-sensitive) on Linux/macOS systems.

settings.json
{
	// Select PS-customized as the default terminal profile
	"terminal.integrated.defaultProfile.windows": "PS-customized",
}

Alternatively, you can set the default Visual Studio Code terminal profile by typing and executing the command Terminal: Select Default Profile in Visual Studio Code’s Command Panel or by clicking on the arrow button next to the plus button in the upper right corner of Visual Studio Code’s Terminal Panel and then selecting the menu item Select Default Profile, which will modify the Visual Studio Code user settings file.

Since the workspace settings file overrides the setting items in the user settings file, the above may not have the desired effect if you have specified the Visual Studio Code default terminal profile in the workspace settings file.

TERMINALPROBLEMS

Set Visual Studio Code Terminal Profiles for Automation Features

Generally, automation features of Visual Studio Code (such as tasks and debugging) use the default terminal profile and launch the corresponding command-line application (shell program). You can specify the terminal profile used by automation features through the Visual Studio Code setting item terminal.integrated.automationProfile.<platform> (platform is one of windows, linux or osx).

The Visual Studio Code setting item terminal.integrated.automationProfile.<platform> is not a string representing the name of the terminal profile, but an object representing the contents of the terminal profile, which may contain the properties path, args, env, overrideName, icon, and color, but does not include the property source, it should be noted that some properties may not have the desired effect, such as icon and color, which may not be able to change the icon and icon color of a command-line application.

settings.json
{
	// Set a terminal profile for automation features
	"terminal.integrated.automationProfile.windows": {
		// Start the Windows Command Prompt
		"path": "cmd",
		// Set the icon color of the terminal (may have no effect)
		"color": "terminal.ansiRed",
	},
}

Remove Visual Studio Code Terminal Profiles

If you want to remove a Visual Studio Code terminal profile, then you can set the value of its corresponding property in the setting item terminal.integrated.profiles.<platform> to null.

In the following example, we remove the terminal profile Command Prompt.

settings.json
{
	// Terminal profiles on Windows Systems
	"terminal.integrated.profiles.windows": {
		// Remove the terminal profile Command Prompt
		"Command Prompt": null,
	},
}

Source Code

settings.json·codebeatme/vscode·GitHub