Git Bash 在 Windows 7 上显示奇怪的字符

13

我在Windows中安装了Gitbash,并定义了Linux命令行(例如,ls用于列出目录),但命令行返回奇怪的字符。

Reginaldo@Dell MINGW64 /c/dev/php/laravel/flamboyant (master)
$ php artisan
←[32mLaravel Framework←[39m version ←[33m5.1.29 (LTS)←[39m

←[33mUsage:←[39m
  command [options] [arguments]

←[33mOptions:←[39m
  ←[32m-h, --help←[39m            Display this help message
  ←[32m-q, --quiet←[39m           Do not output any message
  ←[32m-V, --version←[39m         Display this application version
  ←[32m    --ansi←[39m            Force ANSI output
  ←[32m    --no-ansi←[39m         Disable ANSI output
  ←[32m-n, --no-interaction←[39m  Do not ask any interactive question
  ←[32m    --env[=ENV]←[39m       The environment the command should run under.
  ←[32m-v|vv|vvv, --verbose←[39m  Increase the verbosity of messages: 1 for norm
al output, 2 for more verbose output and 3 for debug

←[33mAvailable commands:←[39m
  ←[32mclear-compiled←[39m      Remove the compiled class file
  ←[32mdown←[39m                Put the application into maintenance mode
  ←[32menv←[39m                 Display the current framework environment
  ←[32mhelp←[39m                Displays help for a command
  ←[32minspire←[39m             Display an inspiring quote
  ←[32mlist←[39m                Lists commands
  ←[32mmigrate←[39m             Run the database migrations
  ←[32moptimize←[39m            Optimize the framework for better performance
  ←[32mserve←[39m               Serve the application on the PHP development ser
ver
  ←[32mtinker←[39m              Interact with your application
  ←[32mup←[39m                  Bring the application out of maintenance mode
 ←[33mapp←[39m
  ←[32mapp:name←[39m            Set the application namespace
 ←[33mauth←[39m
  ←[32mauth:clear-resets←[39m   Flush expired password reset tokens
 ←[33mcache←[39m
  ←[32mcache:clear←[39m         Flush the application cache
  ←[32mcache:table←[39m         Create a migration for the cache database table
 ←[33mconfig←[39m
  ←[32mconfig:cache←[39m        Create a cache file for faster configuration loa
ding
  ←[32mconfig:clear←[39m        Remove the configuration cache file
 ←[33mdb←[39m
  ←[32mdb:seed←[39m             Seed the database with records
 ←[33mevent←[39m
  ←[32mevent:generate←[39m      Generate the missing events and listeners based
on registration
 ←[33mhandler←[39m
  ←[32mhandler:command←[39m     Create a new command handler class
  ←[32mhandler:event←[39m       Create a new event handler class
 ←[33mkey←[39m
  ←[32mkey:generate←[39m        Set the application key
 ←[33mmake←[39m
  ←[32mmake:command←[39m        Create a new command class
  ←[32mmake:console←[39m        Create a new Artisan command
  ←[32mmake:controller←[39m     Create a new resource controller class
  ←[32mmake:event←[39m          Create a new event class
  ←[32mmake:job←[39m            Create a new job class
  ←[32mmake:listener←[39m       Create a new event listener class
  ←[32mmake:middleware←[39m     Create a new middleware class
  ←[32mmake:migration←[39m      Create a new migration file
  ←[32mmake:model←[39m          Create a new Eloquent model class
  ←[32mmake:policy←[39m         Create a new policy class
  ←[32mmake:provider←[39m       Create a new service provider class
  ←[32mmake:request←[39m        Create a new form request class
  ←[32mmake:seeder←[39m         Create a new seeder class
  ←[32mmake:test←[39m           Create a new test class
 ←[33mmigrate←[39m
  ←[32mmigrate:install←[39m     Create the migration repository
  ←[32mmigrate:refresh←[39m     Reset and re-run all migrations
  ←[32mmigrate:reset←[39m       Rollback all database migrations
  ←[32mmigrate:rollback←[39m    Rollback the last database migration
  ←[32mmigrate:status←[39m      Show the status of each migration
 ←[33mqueue←[39m
  ←[32mqueue:failed←[39m        List all of the failed queue jobs
  ←[32mqueue:failed-table←[39m  Create a migration for the failed queue jobs dat
abase table
  ←[32mqueue:flush←[39m         Flush all of the failed queue jobs
  ←[32mqueue:forget←[39m        Delete a failed queue job
  ←[32mqueue:listen←[39m        Listen to a given queue
  ←[32mqueue:restart←[39m       Restart queue worker daemons after their current
 job
  ←[32mqueue:retry←[39m         Retry a failed queue job
  ←[32mqueue:subscribe←[39m     Subscribe a URL to an Iron.io push queue
  ←[32mqueue:table←[39m         Create a migration for the queue jobs database t
able
  ←[32mqueue:work←[39m          Process the next job on a queue
 ←[33mroute←[39m
  ←[32mroute:cache←[39m         Create a route cache file for faster route regis
tration
  ←[32mroute:clear←[39m         Remove the route cache file
  ←[32mroute:list←[39m          List all registered routes
 ←[33mschedule←[39m
  ←[32mschedule:run←[39m        Run the scheduled commands
 ←[33msession←[39m
  ←[32msession:table←[39m       Create a migration for the session database tabl
e
 ←[33mtracker←[39m
  ←[32mtracker:tables←[39m      Create the migrations for Tracker database table
s and columns
 ←[33mvendor←[39m
  ←[32mvendor:publish←[39m      Publish any publishable assets from vendor packa
ges
 ←[33mview←[39m
  ←[32mview:clear←[39m          Clear all compiled view files

Reginaldo@Dell MINGW64 /c/dev/php/laravel/flamboyant (master)

有人能帮我吗?


这些是终端控制代码。 - David Hoelzer
4个回答

11
您需要启用ANSI控制代码处理。详见此处

作者:Jason Karns,关于Shell

我已经在Windows上使用Git三年多了,重新爱上了命令行。当然不是Windows命令提示符,而是优美、无处不在的bash。Git在很大程度上依赖于GNU工具,因此在Windows上它需要cygwin或msys。过去曾受到cygwin的伤害,我更喜欢msys+mingw的极简主义和简单性。除了Git之外,整个Ruby生态系统都存在于shell中。然而,许多工具、gems和实用程序假定shell中具有标准的ANSI颜色支持,这使我感到疲倦。结果发现一个可爱的解决方案,为bash(和cmd)提供ansi颜色支持:ansicon。

下载zip并解压缩。有几种安装方法:

  1. 将其解压到一个永久位置(我使用C:/bin/ansicon)。从相应的目录(x86/x64)中执行ansicon.exe -i,即可完成设置。任何新的shell(包括bash和Windows cmd)都将自动运行ansicon实用程序以显示彩色输出。在运行ansicon.exe-u之前,请务必不要移动可执行文件。这将删除注册表项并防止每个命令shell出现丑陋的错误消息。

  2. 或者,将ansicon可执行文件放置在PATH中,或将其位置添加到PATH中。然后,您可以使用ansicon.exe –p在会话中启动ansicon。

这个实用程序在我的Windows XP上一直表现很好。我一直有在Windows 7上使用它的困扰,但我听说应该得到支持。当Windows 7问题得到解决时,我会发布更新。

更新

找到了根本原因 如果您在Windows x64上使用64位JVM与JRuby,则ansicon将无法工作。问题在于ansicon(64位)能够注入32位进程,但反之则不能。目前,JRuby启动器是一个32位可执行文件。因此,如果您正在运行64位shell(cmd,bash或其他),ansicon将正确地注入该进程。然后,它将成功地注入到32位的JRuby启动器进程中。此时,在所有方面,您都在运行ansicon的32位版本。因此,如果您正在64位JVM上运行JRuby,则32位ansicon无法注入到64位JVM中。有一个针对JRuby的开放功能请求,希望其64位版可以附带一个64位启动器。您应该投票支持此功能。我也听说adox(Jason Hood)正在努力解决这个问题。请继续关注。

当然,目前最简单的解决方法是确保JRuby使用32位JVM。只需更改(或设置)JAVA_HOME环境变量,将其指向32位JVM,您就大功告成了。

更新2:问题已解决

最新的64位二进制文件(ansi6432.zip)已经解决了这个问题。只需下载并将它们覆盖1.51版本即可。

输入图像描述


太棒了。这使得CMD中的颜色可以工作,但不适用于MingGW64控制台。 - Spencer Williams

11
对于MingGW64,我发现进入选项>终端并从(我假设是默认值)切换到解决了这个问题。 我也重新启动了控制台。

输入图像描述


@zhekaus 在更改后重启了终端吗?尝试过这个问题的其他答案了吗?你可以考虑查找类似的问题并检查那些解决方案。 - haakym
请查看以下问题:http://stackoverflow.com/search?q=Git+Bash+%E2%86%90%5B32m - haakym
当然,我试过了!我尝试了stackoverflow上的所有方法,甚至更多!!但都徒劳无功! - zhekaus
@zhekaus 很抱歉听到您还没有找到解决方案。您是否考虑尝试使用其他命令行工具呢?我推荐 http://cmder.net。 - haakym
这对我没有用,但在框中写入“cygwin”就可以了。 - Ben Watson

8

这个答案 帮我解决了在windows系统中从git bash运行gradle时出现的奇怪字符问题。只需在git/etc/bash.bashrc的最后一行添加export TERM=cygwin即可。如果这对你有帮助,请确保给予他们回答的赞 - 我只是在这里转载,因为这是谷歌引导我的地方。


1
这个方法同样适用于使用Gitbash的Windows终端!感谢您的解决方案。 - Tuxedo Joe

2

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接