使用 Windows 终端的命令行参数Using command-line arguments for Windows Terminal

可以使用 wt.exe 从命令行打开 Windows 终端的新实例。You can use wt.exe to open a new instance of Windows Terminal from the command line. 还可以改为使用执行别名 wtYou can also use the execution alias wt instead.

备注

如果从 GitHub 上的源代码生成了 Windows 终端,则可以使用 wtd.exewtd 打开该生成。If you built Windows Terminal from the source code on GitHub, you can open that build using wtd.exe or wtd.

拆分窗格的 Windows 终端命令行参数

命令行语法Command line syntax

wt 命令行接受两种类型的值:“选项”和“命令” 。The wt command line accepts two types of values: options and commands. “选项”是一系列标志和其他参数,可以将 wt 命令行的行为作为一个整体来控制。Options are a list of flags and other parameters that can control the behavior of the wt command line as a whole. “命令”提供应该实现的操作或操作列表(以分号分隔)。Commands provide the action, or list of actions separated by semicolons, that should be implemented. 如果未指定命令,则默认情况下会将命令指定为 new-tabIf no command is specified, then the command is assumed to be new-tab by default.

wt [options] [command ; ]

若要显示列出可用命令行参数的帮助消息,请输入:wt -hwt --helpwt -?wt /?To display a help message listing the available command-line arguments, enter: wt -h, wt --help, wt -?, or wt /?.

选项和命令Options and commands

下面是 wt 命令行支持的命令和选项的完整列表。Below is the full list of supported commands and options for the wt command line.

选项Option 说明Description
--help-h-?/?--help, -h, -?, /? 显示帮助消息。Displays the help message.
--maximized-M--maximized, -M 以最大化形式启动终端。Launches the terminal maximized.
--fullscreen-F--fullscreen, -F 以全屏形式启动终端。Launches the terminal as full screen.

重要

--maximized-M--fullscreen-F 仅在 Windows 终端预览中可用。--maximized, -M and --fullscreen, -F are only available in Windows Terminal Preview.

命令Command 参数Parameters 说明Description
new-tab --profile, -p profile-name--startingDirectory, -d starting-directorycommandline--title--profile, -p profile-name, --startingDirectory, -d starting-directory, commandline, --title 创建新选项卡。Creates a new tab.
split-pane -H, --horizontal-V, --vertical--profile, -p profile-name--startingDirectory, -d starting-directorycommandline--title-H, --horizontal, -V, --vertical, --profile, -p profile-name, --startingDirectory, -d starting-directory, commandline, --title 拆分新窗格。Splits a new pane.
focus-tab --target, -t tab-index 聚焦于特定选项卡。Focuses on a specific tab.

重要

--title 仅在 Windows 终端预览中可用。--title is only available in Windows Terminal Preview.

命令行参数示例Command line argument examples

命令可能会略有不同,具体取决于所使用的命令行。Commands may vary slightly depending on which command line you're using.

打开新的配置文件实例Open a new profile instance

若要打开新的终端实例(在此示例中,该命令将打开名为“Ubuntu-18.04”的配置文件),请输入:To open a new terminal instance, in this case the command will open the profile named "Ubuntu-18.04", enter:

wt -p "Ubuntu-18.04"

-p 标志用于指定应打开的 Windows 终端配置文件。The -p flag is used to specify the Windows Terminal profile that should be opened. 将“Ubuntu-18.04”替换为已安装的任何终端配置文件的名称。Substitute "Ubuntu-18.04" with the name of any terminal profile that you have installed. 这将始终打开一个新窗口。This will always open a new window. Windows 终端尚不能在现有实例中打开新选项卡或窗格。Windows Terminal is not yet capable of opening new tabs or panes in an existing instance.

以一个目录为目标Target a directory

若要指定应该用作控制台起始目录的文件夹(在本例中为 d:\目录),请输入:To specify the folder that should be used as the starting directory for the console, in this case the d:\ directory, enter:

wt -d d:\

多个选项卡Multiple tabs

若要打开具有多个选项卡的新终端实例,请输入:To open a new terminal instance with multiple tabs, enter:

wt ; ;

若要打开具有多个选项卡的新终端实例(在本例中为命令提示符配置文件和 PowerShell 配置文件),请输入:To open a new terminal instance with multiple tabs, in this case a Command Prompt profile and a PowerShell profile, enter:

wt -p "Command Prompt" ; new-tab -p "Windows PowerShell"

多个窗格Multiple panes

若要使用一个选项卡打开一个包含三个窗格(分别运行命令提示符配置文件、PowerShell 配置文件以及运行 WSL 命令行的默认配置文件)的新终端实例,请输入:To open a new terminal instance with one tab containing three panes running a Command Prompt profile, a PowerShell profile, and your default profile running a WSL command line, enter:

wt -p "Command Prompt" ; split-pane -p "Windows PowerShell" ; split-pane -H wsl.exe

-H 标志(或 --horizontal)指示你希望水平拆分窗格。The -H flag (or --horizontal) indicates that you would like the panes to be split horizontally. -V 标志(或 --vertical)指示你希望垂直拆分窗格。The -V flag (or --vertical) indicates that you would like the panes split vertically.

选项卡标题(预览Tab title (Preview)

若要打开带有自定义选项卡标题的新终端实例,可使用 --title 参数。To open a new terminal instance with custom tab titles, use the --title argument. 若要在打开两个选项卡时设置每个选项卡的标题,请输入:To set the title of each tab when opening two tabs, enter:

wt --title tabname1 ; new-tab -p "Ubuntu-18.04" --title tabname2

重要

此功能仅在 Windows 终端预览中可用。This feature is only available in Windows Terminal Preview.

选项卡焦点Tab focus

若要打开带有特定焦点选项卡的新终端实例,请使用 -t 标志(或 --target)以及选项卡-索引号。To open a new terminal instance with a specific tab in focus, use the -t flag (or --target), along with the tab-index number. 若要在第一个选项卡中打开默认配置文件,并在第二个选项卡 (-t 1) 中打开焦点“Ubuntu-18.04”配置文件,请输入:To open your default profile in the first tab and the "Ubuntu-18.04" profile focused in the second tab (-t 1), enter:

wt ; new-tab -p "Ubuntu-18.04" ; focus-tab -t 1

PowerShell 中多个命令的示例Examples of multiple commands from PowerShell

Windows 终端使用分号字符 ; 作为分隔符来分隔 wt 命令行中的命令。Windows Terminal uses the semicolon character ; as a delimiter for separating commands in the wt command line. 遗憾的是,PowerShell 也使用 ; 作为命令分隔符。Unfortunately, PowerShell also uses ; as a command separator. 若要解决此问题,可以使用以下技巧从 PowerShell 运行多个 wt 命令。To work around this, you can use the following tricks to run multiple wt commands from PowerShell. 在下面的所有示例中,将创建一个新的终端窗口,其中包含三个窗格:一个运行命令提示符,一个运行 PowerShell,最后一个运行 WSL。In all the following examples, a new terminal window is created with three panes - one running Command Prompt, one with PowerShell, and the last one running WSL.

下面的示例使用 Start-Process 命令来运行 wtThe following examples use the Start-Process command to run wt. 如需了解终端为什么使用 Start-Process,请参阅下面的使用 startFor more information on why the terminal uses Start-Process, see Using start below.

单引号括起来的参数Single quoted parameters

在此示例中,wt 参数以单引号(')括起来。In this example, the wt parameters are wrapped in single quotes ('). 如果不计算任何内容,则此语法非常有用。This syntax is useful if nothing is being calculated.

start wt 'new-tab "cmd" ; split-pane -p "Windows PowerShell" ; split-pane -H wsl.exe'

转义的引号Escaped quotes

将变量中包含的值传递到 wt 命令行时,请使用以下语法:When passing a value contained in a variable to the wt command line, use the following syntax:

$ThirdPane = "wsl.exe"
start wt "new-tab cmd ; split-pane -p `"Windows PowerShell`" ; split-pane -H $ThirdPane"

请注意,使用 `-p 参数中的“Windows PowerShell”的双引号 (") 转义给 split-pane 参数。Note the usage of ` to escape the double-quotes (") around "Windows PowerShell" in the -p parameter to the split-pane parameter.

使用 startUsing start

上述所有示例显式使用 start 启动终端。All the above examples explicitly used start to launch the terminal.

下面的示例不使用 start 运行命令行。The following examples do not use start to run the command line. 但是,可以通过另外两种方法来对命令行转义:Instead, there are two other methods of escaping the command line:

  • 仅对分号进行转义,使 PowerShell 忽略它们,并将它们直接传递到 wtOnly escaping the semicolons so that PowerShell will ignore them and pass them straight to wt.
  • 使用 --%,因此 PowerShell 会将命令行的其余部分视为应用程序的参数。Using --%, so PowerShell will treat the rest of the command line as arguments to the application.
wt new-tab "cmd" `; split-pane -p "Windows PowerShell" `; split-pane -H wsl.exe
wt --% new-tab cmd ; split-pane -p "Windows PowerShell" ; split-pane -H wsl.exe

在这两个示例中,新创建的 Windows 终端窗口将通过正确分析所有提供的命令行参数来创建窗口。In both of these examples, the newly created Windows Terminal window will create the window by correctly parsing all the provided command-line arguments.

但是目前不建议使用这些方法,因为 PowerShell 会等待新创建的终端窗口关闭,然后再将控制权返回给 PowerShell。However, these methods are not recommended currently, as PowerShell will wait for the newly-created terminal window to be closed before returning control to PowerShell. 默认情况下,在返回到提示符之前,PowerShell 将始终等待 Windows 应用商店应用程序(如 Windows 终端)关闭。By default, PowerShell will always wait for Windows Store applications (like Windows Terminal) to close before returning to the prompt. 请注意,这与命令提示符的行为不同,后者会立即返回到提示符。Note that this is different than the behavior of Command Prompt, which will return to the prompt immediately.