简体   繁体   English

如何在 Windows git-bash 终端上获得彩色输出?

[英]How do I get colorized output on my windows git-bash terminal?

How do I get colorized highlighting on the output?如何在输出上获得彩色突出显示?
eg例如

npm start  
npm test
etc.

npm 启动示例

System:系统:
Windows 10视窗 10
Git version 2.16.1.windows.4 Git 版本 2.16.1.windows.4
Node 5.6.0节点 5.6.0

Uninstall Git Bash for Windows 10 and run the installer again:卸载适用于 Windows 10 的 Git Bash 并再次运行安装程序:

  • Click next until you'll prompt to choose to Configure the terminal emulator to use with Git Bash , here you select Use Windows default console window (instead of the default option which use MinTTY as terminal emulator)单击下一步直到您提示选择配置终端模拟器以与 Git Bash 一起使用,在这里您选择使用 Windows 默认控制台窗口(而不是使用MinTTY作为终端模拟器的默认选项)
  • Click next and finish the installation点击下一步并完成安装

Test if it works correctly:测试它是否正常工作:

  • Open Git Bash (cmd will open up), try to use ls you'll see all folder colored in blue打开 Git Bash(cmd 会打开),尝试使用ls你会看到所有文件夹都变成蓝色
  • Try some ANSI escape code by running echo -e "\\033[44m\\033[37m Test \\033[0m" you'll se the text Test with a blue background as seen in the screenshot below通过运行echo -e "\\033[44m\\033[37m Test \\033[0m"尝试一些ANSI 转义码,您将看到带有蓝色背景的文本Test ,如下面的屏幕截图所示

在此处输入图片说明

Windows 10 Console support 24-bit color and ANSI escape sequences Windows 10 控制台支持24 位颜色ANSI 转义序列

The terminal was a MinTTY, which does not support colors it seems.终端是一个 MinTTY,它似乎不支持颜色。 Fixed it by re-installing and ticking of Windows Terminal.通过重新安装和勾选 Windows 终端来修复它。

For me, it was choosing Windows default console window instead of MinTTY that made color output not working.对我来说,是选择Windows 默认控制台窗口而不是MinTTY导致颜色输出不起作用。

My solution:我的解决方案:

  1. Uninstall git bash ;卸载git bash during installing, choose MinTTY (the default option).在安装过程中,选择MinTTY (默认选项)。
  2. After installation, head over to C:\\Program Files\\Git\\etc , open your git bash安装后,转到C:\\Program Files\\Git\\etc ,打开你的 git bash
  3. vim ~/.bashrc (for some reason using an editor doesn't work; I guess it's a privilege issue) vim ~/.bashrc (由于某种原因使用编辑器不起作用;我想这是一个权限问题)
  4. Under # Add colors to 'ls' change it to alias ls='ls --color=auto'# Add colors to 'ls'将其更改为alias ls='ls --color=auto'

Then restart and it should work.然后重新启动,它应该可以工作。

My version: git version 2.24.1.windows.2我的版本:git 版本 2.24.1.windows.2

Use winpty使用winpty

Windows Git Bash has some documented quirks: Winpty and Git Bash Windows Git Bash 有一些记录在案的怪癖: Winpty 和 Git Bash

$ winpty npm start
winpty: error: cannot start 'npm': Not found in PATH

Again another windows / git bash quirk, it doesn't attempt to resolve executables ending in .cmd so it needs a nudge.再次是另一个 windows / git bash 怪癖,它不会尝试解析以.cmd结尾的可执行文件,因此它需要轻推。

$ winpty npm.cmd start

The same applies for yarn with:这同样适用于yarn

winpty yarn.cmd start

While we are here and you are having trouble getting gyp to locate your Python2 install location to compile native extensions then try this:虽然我们在这里,你有麻烦gyp找到您Python2安装位置编译原生扩展,然后试试这个:

env PYTHON=/c/Python27/ winpty yarn.cmd install

Git Bash overrides your PYTHON environment variable for only the duration of this command. Git Bash 仅在此命令的持续时间内覆盖您的PYTHON环境变量。 You get winpty to run the pseudo-tty session to allow animation rendering and coloring.您可以通过winpty运行伪 tty 会话以允许动画渲染和着色。

Assuming your python 2 install location is C:\\Python27假设你的 python 2 安装位置是 C:\\Python27

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 你如何为Git-Bash设置16种终端颜色? - How do you set the 16 Terminal Colors for Git-Bash? 如何在 Windows 的 git-bash (msys2) 中做一个包? - How to do a package in git-bash (msys2) in windows? 如何在 Windows 中的 git-bash 中执行位于带有空格的文件夹(即“程序文件”)中的命令? - How can I execute commands in git-bash in Windows which are located in a folder with spaces ie “Program Files”? 如何使用名称启动git-bash中的Windows进程 - How to start a windows process in git-bash with a name 如何使用 git-bash 在 Windows 10 上安装 GNU Parallel - How to Install GNU Parallel on Windows 10 using git-bash 如何在 windows git-bash 控制台下运行批处理文件? - How can I run a batch-file under windows git-bash console? 在Windows上的RMarkdown中执行bash(Git-Bash) - Executing bash in RMarkdown on Windows (Git-Bash) 如何使用git-bash向Windows 10目录中的文件添加扩展名 - How to add extensions to files in windows 10 directory with git-bash Git-bash选项卡完成:“致命:非git存储库:'。git'”(Windows) - Git-bash tab completion: “fatal: Not a git repository: '.git'” (Windows) 如何在Windows 10上将Git Bash Here终端从mingw更改为标准cmd或powershell? - How do I change the Git Bash Here terminal from mingw to the standard cmd OR powershell on Windows 10?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM