如何更改 Windows 中的命令行提示符?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12028372/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 09:49:30  来源:igfitidea点击:

How do I change the command-line prompt in Windows?

windowscmdprompt

提问by Marc B

How do I change the command-line prompt into a console?

如何将命令行提示符更改为控制台?

I've been looking in the console functions API, but I could not find anything for it.

我一直在寻找控制台函数 API,但我找不到任何东西。

回答by Marc B

There's the PROMPT environment variable:

有 PROMPT 环境变量:

set PROMPT=$P$G

$P$Gis the default value, giving you the usual C:\>type output. There are more format variables available here.

$P$G是默认值,为您提供通常的C:\>类型输出。这里有更多可用的格式变量。

回答by David Ferenczy Rogo?an

Another possibility is to set the PROMPTenvironment variable (which in fact is what the PROMPTcommanddoes).

另一种可能性是设置PROMPT环境变量(这实际上是PROMPT命令所做的)。

The advantage of this method is that you can easily set it system-wide and you don't need any scripts, edit the Windows Registry, etc. It will work for any console window no matter how you open it.

这种方法的优点是您可以轻松地在系统范围内对其进行设置,并且不需要任何脚本、编辑 Windows 注册表等。无论您如何打开它,它都适用于任何控制台窗口。

You can do it using two methods, GUI and command-line.

您可以使用两种方法来完成,GUI 和命令行。

1. GUI method

1.图形界面方式

Simply press Win+ Pause/Break(open System properties), click Advanced system settings, Environment variablesand create a new user or system variable named PROMPTwith the value set to whatever you want your prompt to look like. A system variable will set it for all users.

只需按Win+ Pause/Break(打开System properties),单击Advanced system settingsEnvironment variables并创建一个新的用户或系统变量PROMPT,其值设置为您希望提示的外观。系统变量将为所有用户设置它。

You can see it with pictures in this article.

你可以在这篇文章中看到它的图片。

2. Command-line method

2. 命令行方式

Another way to set the PROMPTenvironment variable permanently is to use the SETXcommand:

PROMPT永久设置环境变量的另一种方法是使用以下SETX命令

setx PROMPT <your-prompt-format>

If you want to set it for all users, just add the /Mswitch:

如果你想为所有用户设置它,只需添加/M开关:

setx PROMPT /M <your-prompt-format>

3. Registry method

3.注册方式

In fact, both previous methods just create a string value named PROMPTin the registry. For the current user, it's under the key HKEY_CURRENT_USER\Environment, and the system-wide one for all users under the key HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment.

事实上,之前的两种方法都只是创建了一个PROMPT在注册表中命名的字符串值。对于当前用户,它在 key 下HKEY_CURRENT_USER\Environment,而对于 key 下的所有用户,它是系统范围的HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment



Check this pageor other answers for details about the prompt format.

有关提示格式的详细信息,请查看此页面或其他答案。

Note:it's possible that you will have to reboot your system(or possibly just sign out and in) for the changes to take effect. At least, you have to close and restart the application (console), so it loads the new or changed environment variable. If you can't do it for whatever reason, you can use the following method:

注意:您可能必须重新启动系统(或者可能只是注销并登录)才能使更改生效。至少,您必须关闭并重新启动应用程序(控制台),以便它加载新的或更改的环境变量。如果由于某种原因不能这样做,则可以使用以下方法:

4. Command-line method (temporary)

4.命令行方法(临时)

If you execute the PROMPTcommand, it will set the PROMPTenvironment variable in your local context, so it will take an effect immediately, but until the console is closed only. It's not stored permanently.

如果您执行该PROMPT命令,它将PROMPT在您的本地上下文中设置环境变量,因此它将立即生效,但直到控制台关闭为止。它不会永久存储。

prompt <your-prompt-format>

回答by DaveW

Using HELP:

使用帮助

C:\Windows-15:21:07.12> help PROMPT

Changes the cmd.exe command prompt.

PROMPT [text]

  text    Specifies a new command prompt.

Prompt can be made up of normal characters and the following special codes:

  $A   & (Ampersand)
  $B   | (pipe)
  $C   ( (Left parenthesis)
  $D   Current date
  $E   Escape code (ASCII code 27)
  $F   ) (Right parenthesis)
  $G   > (greater-than sign)
  $H   Backspace (erases previous character)
  $L   < (less-than sign)
  $N   Current drive
  $P   Current drive and path
  $Q   = (equal sign)
  $S     (space)
  $T   Current time
  $V   Windows version number
  $_   Carriage return and linefeed
  $$   $ (dollar sign)

If Command Extensions are enabled, the PROMPT command supports
the following additional formatting characters:

  $+   zero or more plus sign (+) characters depending upon the
       depth of the PUSHD directory stack, one character for each
       level pushed.

  $M   Displays the remote name associated with the current drive
       letter or the empty string if the current drive is not a network
       drive.

Reference for "Command Extensions"(e.g. "Command Extensions are enabled by default.").

“命令扩展”的参考(例如“默认启用命令扩展。”)。

回答by sdfor

The command is:

命令是:

prompt yourPrompt

The full list of options is at Microsoft Windows XP - Prompt

完整的选项列表位于Microsoft Windows XP - Prompt

回答by CigarDoug

I found this article when searching for how to save the PROMPTcommand to always run when launching a command prompt. It works for any version of Windows.

我在搜索如何保存PROMPT命令以在启动命令提示符时始终运行时发现了这篇文章。它适用于任何版本的 Windows。

  • Click the Start menu → Run.
  • From the Rundialog, type "regedit" without quotes and click OK.
  • From the Registry Editor, select HKEY_CURRENT_USER\Software\Microsoft\Command Processor.
  • If the AutoRunvalue is not present, right-click and click NewExpandable String Value. Name the value "AutoRun" without quotes.
  • Double-click the AutoRunvalue.
  • Under data, add "PROMPT" and the value you want to set for the prompt. In my example, "PROMPT $p$_$g".
  • Click OK.
  • Repeat the previous steps for HKEY_LOCAL_MACHINE\Software\Microsoft\Command Processor?to apply the change to ALL users.
  • Close the Registry Editor.
  • Close the command prompt if open, and open it again. The prompt you entered is now employed.
  • 单击开始菜单 →运行
  • 在“运行”对话框中,键入不带引号的“regedit”,然后单击OK
  • 从注册表编辑器中,选择HKEY_CURRENT_USER\Software\Microsoft\Command Processor
  • 如果AutoRun值不存在,请右键单击并单击新建可扩展字符串值。将值命名为“AutoRun”,不带引号。
  • 双击AutoRun值。
  • 在数据下,添加“PROMPT”和要为提示设置的值。在我的示例中,“提示 $p$_$g”。
  • 单击确定。
  • HKEY_LOCAL_MACHINE\Software\Microsoft\Command Processor?重复前面的步骤以将更改应用于所有用户。
  • 关闭注册表编辑器。
  • 如果打开,请关闭命令提示符,然后再次打开它。现在使用您输入的提示。

回答by Engineer

I use a batch file I call DOSbox.bat to set any environment strings I need, and I have a shortcut to it on my desktop. The command in the "Target:" box is C:\Windows\System32\cmd.exe /k DOSbox.bat. I have a Shortcut Key (Ctrl+Alt_D) to invoke it and the "Start in" set to my preferred directory. The shortcut also allows one to set the font, color, and location of the command window.

我使用一个名为 DOSbox.bat 的批处理文件来设置我需要的任何环境字符串,并且我的桌面上有一个快捷方式。“目标:”框中的命令是 C:\Windows\System32\cmd.exe /k DOSbox.bat。我有一个快捷键 (Ctrl+Alt_D) 来调用它,并将“开始于”设置为我的首选目录。该快捷方式还允许设置命令窗口的字体、颜色和位置。

The contents of the batch file are currently:

批处理文件的内容目前是:

@echo off
set dircmd=/ogne
prompt [$p]$_$g

The prompt shows the directory in brackets, and the ">" on a line by itself. This is useful for very long paths.

提示在括号中显示目录,在一行中显示“>”。这对于很长的路径很有用。

回答by wiotrek

If you want to save changes, used

如果要保存更改,请使用

 setx prompt < format >

If you want to use it only one time

如果您只想使用它一次

prompt < format >

回答by Ultra Gamer

The link to the custom prompt style:

自定义提示样式的链接:

http://notes.io/65hi

http://notes.io/65hi

Just do it as shown in the instruction.

只需按照说明进行操作即可。

What is in link:

什么是链接:

Remember!

记住!

When you will open cmd, replace all ^[s (which are after [%some number%m) with key Ctrl+ key [ (don't forget to delete ^[first and then replace by entering Ctrl+ [!).

当您打开 cmd 时,将所有^[s(在 [%some number%m 之后)替换为 key Ctrl+ key [(不要忘记先删除^[然后输入Ctrl+替换[!)。

Copy the code below and paste in the cmd prompt (if successful: exit cmd and start again):

复制下面的代码并粘贴到 cmd 提示符中(如果成功:退出 cmd 并重新启动):

setx prompt ^[[91m┌──[%username%^[[93m@^[[96mhostname^[[91m]-[^[[92m$P^^[[91m]$_└──$G$S^[[93m#$S`