在 Windows 终端内使用 git-posh 或/和 oh-my-posh 以及命令提示符(cmd,而不是 Powershell)

发布于 2025-01-16 00:46:49 字数 717 浏览 3 评论 0 原文

最近切换到新 Windows 终端 ,经过几个小时的互联网搜索后,我找不到任何有用的东西,我想要的就是在新的 Windows 终端中设置 cmd 以显示 git 分支,就像 powershell 可以实现的那样。

例如这样 在此处输入图像描述

我对 cmd 非常满意,尤其是它能够使用其他 Linux 命令,并且不想仅仅因为 git 分支的良好显示而切换到 powershell。 this 是一个源代码,其中对 powershell 的所有内容都进行了很好的解释,我想要的就是对 CMD 执行相同的操作。

提前致谢

Recently switched to new windows terminal, and after hours of searching on internet I was not able to find anything helpful, all what I want is to set up cmd inside new windows terminal to show git branches just like it's achievable for powershell.

eg like this enter image description here

I have been very comfortable with cmd especially with its ability to use additional linux commands and don't wanna switch to powershell only because of nice displays of git branches. this is a source where everything is nicely explained for powershell, all I want is to do the same for CMD.

thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情泪▽动烟 2025-01-23 00:46:50

为了使用Oh My Posh进行 shell 提示自定义<强>来自cmd.exe,旧版Windows shell(引用自docs,选项卡 cmd,添加强调):

对于自定义提示,Windows CMD 不提供开箱即用的支持。然而有一种方法可以使用Clink来做到这一点,它在同时增强您的 cmd 体验。按照安装说明进行操作并确保选择自动启动。

将 Oh My Posh 与 Clink 集成很简单:在 Clink 脚本目录中创建一个名为 oh-my-posh.lua 的新文件(在 cmd 中运行 clink info 以找到该文件的位置)。

oh-my-posh.lua load(io.popen('oh-my-posh init cmd'):read("*a"))()

添加后,重新启动 cmd 以使更改生效。

正如您后来发现的那样,GitHub 上的此问题提供了有关原因的背景信息本机 cmd.exe 支持是不可能的(尽管 Oh My Posh 通常与 shell 无关),以及为什么需要第三方软件才能使其工作。


至于您更喜欢 cmd.exe 的评论:

我对 cmd 非常满意

从一个已经习惯的 shell 迁移到一个新的 shell 无疑是​​一个痛苦的过渡,但在这种情况下非常值得考虑:

虽然并非没有怪癖,但 PowerShell 在几乎所有方面都远远优于 cmd.exe,使您能够执行 cmd.exe 中无法执行的操作

它能够使用额外的 Linux 命令

从 Windows 端调用的 Linux (WSL) 命令都是通过可执行文件(特别是 wsl.exebash.exe),您同样可以从 PowerShell 中调用它。

In order to use Oh My Posh for shell-prompt customization from cmd.exe, the legacy Windows shell (citing from the docs, tab cmd, emphasis added):

There's no out of the box support for Windows CMD when it comes to custom prompts. There is however a way to do it using Clink, which at the same time supercharges your cmd experience. Follow the installation instructions and make sure you select autostart.

Integrating Oh My Posh with Clink is easy: create a new file called oh-my-posh.lua in your Clink scripts directory (run clink info inside cmd to find that file's location).

oh-my-posh.lua load(io.popen('oh-my-posh init cmd'):read("*a"))()

Once added, restart cmd for the changes to take effect.

As you later discovered, this issue on GitHub has background information on why native cmd.exe support isn't possible (even though Oh My Posh is generally shell-agnostic) and why third-party software is needed to make it work.


As for your comments re preferring cmd.exe:

I have been very comfortable with cmd

Migrating from the shell one is used to a new one is undoubtedly a painful transition, but well worth considering in this case:

While not without its quirks, PowerShell is vastly superior in just about every respect to cmd.exe, and enables you to do things you simply cannot do in cmd.exe

its ability to use additional linux commands

Linux (WSL) commands called from the Windows side are all mediated via executables (notably wsl.exe and bash.exe), which you can equally call from PowerShell.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文