开发者

Printing Unicode characters to the PowerShell prompt

开发者 https://www.devze.com 2023-02-28 17:40 出处:网络
I\'m trying to set up Mercurial to print out the status of the repository I\'m currently in with PowerShell. So far, I have everything working, but I would like to be able to print \'☿\' to the promp

I'm trying to set up Mercurial to print out the status of the repository I'm currently in with PowerShell. So far, I have everything working, but I would like to be able to print '☿' to the prompt when I am in a repository. Unfortunately, it seems that PowerShell has some problems with printing Unicode characters.

In the accepted answer for Is there a Windows command shell that will display Unicode characters? it is suggested that PowerShell v2, which shipped with Windows 7 (which I am using) would be able to print Unicode 开发者_如何学Pythoncharacters, but I can't seem to get it to work. Likewise, the next answer of using chcp 65001 does not work either.

Is this still a deficiency in PowerShell, or am I missing something obvious?


This is not a PowerShell deficiency. It is a deficiency with the Windows console subsystem which PowerShell.exe uses. The console subsystem does not support Unicode but code pages instead which dates back to the DOS days. The PowerShell V2 fix is provided via the PowerShell Integrated Scripting Environment or PowerShell_ISE.exe. This is a graphical app based on WPF which can handle Unicode characters easily.

In theory you could change the code page using chcp or

[Console]::OutputEncoding = [System.Text.Encoding]::GetEncoding(850)

to display different characters but I haven't had much luck with this. You'd also need to find a code page that contains the character you want to display.


Prints semi-fine here. Consolas doesn't have a glyph for that character, so all I see in the console window is a replacement glyph. But PowerShell accepts and prints it just fine, as you can see when copy-pasting it.

Now, if you have set your console window to raster fonts, then the story is a little different, but PowerShell luckily has sane defaults.


You can't using the default PowerShell (i.e. windows) console. However, if you use the PowerShell ISE (which provides its own console), it will work. I am not sure if this will meet your needs though.


Deja Vu Sans Mono works in cmd and has that character, and so it works in PowerShell too.

Printing Unicode characters to the PowerShell prompt

Printing Unicode characters to the PowerShell prompt

BTW, I suggest also installing Droid Sans Mono, and Courier New, to the command prompt. They'll then be available in PowerShell too. There aren't many mono spaced fonts that cmd supports. But those are three, Deja Sans Mono, Droid Sans Mono, and Courier New.


You need to

  1. install a monospace font in your system, that contains your glyph (possibly use Character Map to find a suitable one, etc) and then
  2. set it as a console font, as described here: http://www.watchingthenet.com/how-to-add-and-change-fonts-in-windows-command-prompt.html
0

精彩评论

暂无评论...
验证码 换一张
取 消