Search code examples
visual-studio-coderustcharacter-encodingnon-ascii-characters

How to fix Visual Studio Code distorting characters on Windows?


I have problems with Swedish national characters when using Rust in Visual Studio Code in Windows 11. It can be shown with the following program:

fn main() {
let abc = " ååå
ööö
äää";
println!("<---{}--->", abc);
}

When the program is run from the command line using cargo run, the output is as follows:

<--- ååå
     ööö
     äää--->

Strangely, spaces are added at the beginning of lines 2 and 3. However, when the program is run in Visual Studio Code, the Swedish characters get distorted.

<--- ååå
     ├╢├╢├╢
     äää--->

How can I solve it? I work with text processing and this is a major problem.

EDIT: Since the problem doesn't appear on many systems, I add the technical data: Windows 11 Pro Version 10.0.22621 Build 22621; Visual Studio Code Version: 1.73.1 (user setup) Date: 2022-11-09 Chromium: 102.0.5005.167 Node.js: 16.14.2 Sandboxed: No.


Solution

  • - This answer is Windows specific. -

    INFO: This answer describes you how can change your VSCode settings to force UTF-8 in your console. An alternative to this answer would be to force UTF-8 system-wide, as described here: Using UTF-8 Encoding (CHCP 65001) in Command Prompt / Windows Powershell (Windows 10)


    It seems that sometimes the Windows shell doesn't use the correct UTF-8 code page.

    You can tell VSCode to force a codepage in its shell using the following settings.

    • Open the Settings page (Shortkey: Ctrl+,)
    • Click on the button on the top right whose mouse-over text reads "Open Settings (JSON)"
    • Add the following lines:
       "terminal.integrated.profiles.windows": {
            "PowerShell": {
                "source": "PowerShell",
                "icon": "terminal-powershell",
                "args": [
                    "-NoExit",
                    "/c",
                    "chcp.com 65001"
                ]
            },
            "Command Prompt": {
                "path": [
                    "${env:windir}\\Sysnative\\cmd.exe",
                    "${env:windir}\\System32\\cmd.exe"
                ],
                "args": [
                    "/K",
                    "chcp 65001"
                ],
                "icon": "terminal-cmd"
            },
        },
    

    This will force the UTF-8 code page.

    If it worked, opening a new shell should display Active code page: 65001.

    Source: https://github.com/microsoft/vscode/issues/19837


    Previous, deprecated settings:

    • If your shell is "CMD":
      "terminal.integrated.shellArgs.windows": ["/K", "chcp 65001"],
      
    • If your shell is "Powershell":
      "terminal.integrated.shellArgs.windows": ["-NoExit", "/c", "chcp.com 65001"],