Change default code page of Windows console to UTF-8

131

80

Currently I'm running Windows 7 x64 and usually I want all console tools to work with UTF-8 rather than with default code page 850.

Running chcp 65001 in the command prompt prior to use of any tools helps but is there any way to set is as default code page?

Update:

Changing HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Nls\CodePage\OEMCP value to 65001 appear to make the system unable to boot in my case.

Proposed change of HKEY_LOCAL_MACHINE\Software\Microsoft\Command Processor\Autorun to @chcp 65001>nul served just well for my purpose. (thanks to Ole_Brun)

Regent

Posted 2011-04-12T10:42:07.893

Reputation: 1 586

Note the purposed solution could break Windows' find.exe (which would cause problems with Android SDK build): http://superuser.com/questions/176737/why-find-exe-not-work-in-windows-7

– J Rao – 2015-01-18T05:24:55.103

Hm, when I use chcp 65001 my console windows crash when I do dir, but it helps to simply start cmd.exe with the /u flag (nb: it does use unicode by it is not reflected in chcp.com output) – eckes – 2015-01-27T18:58:14.173

Using the UTF-8 code-page also breaks the more command (it gives the misleading error message Not enough memory.) Opening the command-prompt with the /U switch does not help. – Synetech – 2016-03-07T22:00:26.510

10The Windows console is riddled with bugs when the encoding is set to an unsupported multi-byte code page like 65001. Any software using the output counts of the Win32 WriteFile/ReadFile APIs will get the wrong results and consequently stuff build on that like the MSVCRT's implementation of the stdlib will produce mangled/repeated output and hang on input when confronted with non-ASCII. Until MS get around to fixing it — and it has been decades with no sign of that happening — globally changing console code page to 65001 is an extraordinarily bad idea. – bobince – 2016-10-14T08:51:51.240

2Any use of the A versions of Windows functions is broken. All code needs to be ported to use the W versions. – Demi – 2017-04-07T14:42:44.210

1HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Nls\CodePage\OEMCP is used as fallback for non-unicode (non-utf) environments, obviously you try to force it to a 'unicode', as you see it is controversal by definition. Windows hangs probably because some system stuff on boot depends on non-unicode charset to work. – venimus – 2017-08-23T15:00:24.830

Answers

102

To change the codepage for the console only, do the following:

  1. Start -> Run -> regedit
  2. Go to [HKEY_LOCAL_MACHINE\Software\Microsoft\Command Processor\Autorun]
  3. Change the value to chcp 65001

Nils Magne Lunde

Posted 2011-04-12T10:42:07.893

Reputation: 2 154

1@Regent In the question, the registry value you mentioned that worked for you is @chcp 65001>nul, while in this accepted answer, it is chcp 65001. What are the differences between the two? – galacticninja – 2015-03-16T08:41:09.580

8@galacticninja simply putting chcp 65001 will cause every opened command prompt to print 'Active code page: 65001' whilst @chcp 65001>nul will prevent any output. – Regent – 2015-03-26T15:51:13.683

7Autorun is not present for me under Windows 8.1. – kleinfreund – 2015-05-10T09:09:36.340

Setting my console to UTF-8 made my console ding a lot when copy/pasting accented text into it, then discard the accented characters. Couldn't type them in either. – John Dvorak – 2016-07-06T07:31:23.120

https://imgur.com/RvZSlHi seem I don't have Autorun key to change, could I create a key with autorun name here? which type of it? – Luke – 2018-08-23T08:39:08.547

2@kleinfreund @Luke: if Autorun is not present, you can add a New String Value with the contents of @chcp 65001>nul – Dacto – 2018-10-23T20:45:59.257

@NilsMagneLunde So, what is the point of this answer: https://stackoverflow.com/questions/388490 that says: "chcp 65001 is very dangerous."

– Dr.jacky – 2018-12-06T08:06:15.723

1@Dr.jacky Not sure exactly what they mean with dangerous. I've just answered the question of the OP. Whether you should do it or not is a different question. I believe there might be some issues with Python, so you may want to check that out before setting codepage to 65001 at least. – Nils Magne Lunde – 2018-12-18T06:59:17.060

And if you are NOT starting CMD, but just a random console utility, Autorun will not run… – AnrDaemon – 2019-09-06T09:10:50.243

41

Personally, I don't like changing the registry. This can cause a lot of problems. I created a batch file:

@ECHO OFF
REM change CHCP to UTF-8
CHCP 65001
CLS

I saved at C:\Windows\System32 as switch.bat.

I created a link for cmd.exe on the Desktop.

In the properties of the cmd shortcut, changed the destination to: C:\Windows\System32\cmd.exe /k switch

Voilá, when I need to type in UTF-8, I use this link.

juca

Posted 2011-04-12T10:42:07.893

Reputation: 419

7Note that it will print Active code page: 65001 to stdout. So if you are doing something like CHCP 65001 && mycommand.exe then you'll get the codepage printed out at the start. You need to CHCP 65001 >nul && mycommand.exe – frumbert – 2015-06-12T05:33:22.343

35

Reg file:

Windows Registry Editor Version 5.00
[HKEY_CURRENT_USER\Console\%SystemRoot%_system32_cmd.exe]
"CodePage"=dword:fde9
  1. Value must be in hex
  2. Top line must be included exactly as is
  3. HKEY_CURRENT_USER cannot be abbreviated
  4. dword cannot be omitted

Command Prompt:

REG ADD HKCU\Console\%SystemRoot^%_system32_cmd.exe /v CodePage /t REG_DWORD /d 65001
  1. Value can be in dec or hex
  2. %SystemRoot% must be escaped
  3. REG_DWORD cannot be omitted

PowerShell:

New-Item -ErrorAction Ignore HKCU:\Console\%SystemRoot%_system32_cmd.exe
Set-ItemProperty HKCU:\Console\%SystemRoot%_system32_cmd.exe CodePage 65001
  1. Value can be in dec or hex
  2. -Type DWord is assumed with PowerShell 3+
  3. Can use ni -> New-Item
  4. Can use sp -> Set-ItemProperty
  5. Can use -ea 0 -> -ErrorAction Ignore

Cygwin:

regtool add '\HKEY_CURRENT_USER\Console\%SystemRoot%_system32_cmd.exe'
regtool set '\HKEY_CURRENT_USER\Console\%SystemRoot%_system32_cmd.exe\CodePage' 65001
  1. Value can be in dec or hex
  2. Can use / -> \
  3. Can use HKCU -> HKEY_CURRENT_USER
  4. Can use user -> HKEY_CURRENT_USER

Steven Penny

Posted 2011-04-12T10:42:07.893

Reputation: 7 294

Didn't work in Windows 10.

What does work is to create an autorun that sets the codepage via the chcp command.E.g., – George – 2019-01-27T07:50:12.177

What I meant to say before the edit rudely timed out was, issue chcp 65001 in an autorun command file. See https://stackoverflow.com/a/17405182/315083

– George – 2019-01-27T08:02:10.483

The thing is, it only works for CMD, but not for console by itself. – AnrDaemon – 2019-09-06T09:10:06.097

@George is 99% right - however, it does sort of work in Windows 10, but only if you start cmd.exe by Start -> Run -> cmd.exe. It doesn't work if cmd.exe is started through a shortcut. Replacing the path in the registry key name by the path of the shortcut (.lnk file) doesn't help. – Luc VdV – 2019-11-12T07:46:13.497

10

In the 1809 build of Windows 10 I've managed to permanently solve this by going to the system's Language settings, selecting Administrative language settings, clicking Change system locale... and checking the Beta: Use Unicode UTF-8 for worldwide language support box and then restarting my pc.

This way it applies to all applications, even those ones that I don't start from a command prompt!
(Which was neccessary for me, since I was trying to edit Agda code from Atom.)

Isti115

Posted 2011-04-12T10:42:07.893

Reputation: 306

1Thanks, it solved my issue! – Jabba – 2020-02-22T10:09:03.493

6

This can be done by creating a PowerShell profile and adding the command "chcp 65001 >$null" to it:

PS> Set-ExecutionPolicy RemoteSigned
PS> New-Item -Path $Profile -ItemType file -Force
PS> notepad $Profile

This doesn't require editing the registry and, unlike editing a shortcut, will work if PowerShell is started in a specific folder using the Windows Explorer context menu.

Freon Sandoz

Posted 2011-04-12T10:42:07.893

Reputation: 99

1@PimpJuiceIT, no.. see the first line of this answer. – Dacto – 2018-12-14T17:21:06.220

5

The command to change the codepage is chcp <codepage>. Example: chcp 1252. You should type it in a Powershell window. To avoid the hassle of typing it everytime (if you always have to change the codepage), you may append it to the program's command line. To do so, follow these steps:

  1. Right-click the Powershell icon on Start menu and choose "More" > "Open file Location".
  2. Right-click the Powershell shortcut and select "Properties".
  3. Add the following to the end of the "Target" command line: -NoExit -Command "chcp 1252"

Be happy. Don't fuss with Windows Registry unless you have no other option.

JColares

Posted 2011-04-12T10:42:07.893

Reputation: 59

This one worked perfectly for me. -NoExit -Command "chcp 1252 > null" also omits the message about the selected code page in the beginning. – CodeMonkey – 2018-08-03T08:53:07.127