Terminal: Could new Windows Terminal customize prompt using emoji?

Created on 15 May 2019  路  11Comments  路  Source: microsoft/terminal

I want to know how to customize prompt using emoji in Windows terminal, since Linux/Mac shell support it years ago. I guess Windows terminal can support it now or later. But I tried, still not konw how to do it. Could @shanselman or any one else give some guide for this? 馃榿

image

Related Links:
How to Customize your Terminal Prompt (for Mac)

A better PROMPT for CMD.EXE or Cool Prompt Environment Variables and a nice transparent multi-prompt - Scott Hanselman

Issue-Question

Most helpful comment

@yanglr There are bugs in unicode display right now that are already known, and if you do this, the prompt won't look correct in the old powershell window.

However, these are the steps to take if you want to experiment:

  1. Launch Powershell or Windows Terminal Powershell tab
  2. execute "notepad $profile"
  3. Copy/paste this line: function prompt { "PS $pwd>" }
  4. Insert emojis where you want
  5. File -> Save As -> Use Default File Name, but you must set encoding to UTF-16 LE because that's what Powershell expects for its profile file (UTF-8 encoding will not work)
  6. Close notepad after saving
  7. Close Powershell / Terminal Windows
  8. Open Windows Terminal Powershell tab again
  9. Enjoy your emoji prompt

image

Remember, your emoji prompt won't work in the old powershell window, it will display weird character boxes or something else there. Also, note that the display of some characters may not work as expected.

All 11 comments

This new Windows Terminal will include emoji support

image

@mdtauk
Thanks, but pity that you misunderstand my question. I ask about customize prompt in Windows Terminal, not about input emoji into Windows Terminal (you mentioned). They are completely two things.

What I need is like this:
image

Yes, although there are some bugs being worked out:
image

Yes, although there are some bugs being worked out:
image

@binarycrusader Oh, cool. How to do it? Need code change manually? Or just achive it with some configurations.

It depends on what your shell is.

For cmd.exe, you can set your prompt with the PROMPT command for the current instance, or setx PROMPT to set it for future cmd.exe instances. You might run into other issues though, because cmd.exe doesn't support utf-8 by default, so you'll probably need a chcp 65001 somewhere before that to get it to work right. cmd.exe also doesn't support empji on the input line quite yet, so you'll probably also need to use a batch script to actually set it.

As far as powershell goes, I don't actually know how to set the powershell prompt. Someone else might be able to help you there.

@zadjii-msft The PowerShell prompt is just a function named prompt that returns a string.

This is better than an environment variable, IMHO, since you can make it more dynamic this way.

P.S.: I use PowerLine for PowerShell as my prompt of choice nowadays.

@ExE-Boss I am also using PowerLine for聽PowerShell, I am looking forward to the accurate reply of @binarycrusader. Thanks~

@yanglr There are bugs in unicode display right now that are already known, and if you do this, the prompt won't look correct in the old powershell window.

However, these are the steps to take if you want to experiment:

  1. Launch Powershell or Windows Terminal Powershell tab
  2. execute "notepad $profile"
  3. Copy/paste this line: function prompt { "PS $pwd>" }
  4. Insert emojis where you want
  5. File -> Save As -> Use Default File Name, but you must set encoding to UTF-16 LE because that's what Powershell expects for its profile file (UTF-8 encoding will not work)
  6. Close notepad after saving
  7. Close Powershell / Terminal Windows
  8. Open Windows Terminal Powershell tab again
  9. Enjoy your emoji prompt

image

Remember, your emoji prompt won't work in the old powershell window, it will display weird character boxes or something else there. Also, note that the display of some characters may not work as expected.

@binarycrusader
OK, cool. I will try, thanks so much~ 馃憦

For cmd.exe, you can set your prompt with the PROMPT command for the current instance, or setx PROMPT to set it for future cmd.exe instances. You might run into other issues though, because cmd.exe doesn't support utf-8 by default, so you'll probably need a chcp 65001 somewhere before that to get it to work right. cmd.exe also doesn't support empji on the input line quite yet, so you'll probably also need to use a batch script to actually set it.

CMD uses the console's wide-character API. The command line is read via ReadConsoleW, and the prompt is written via WriteConsoleW. Also, the PROMPT environment variable is Unicode, so UTF-16LE surrogate pairs in its value get passed along to the console via WriteConsoleW.

CMD uses the console output codepage (i.e. GetConsoleOutputCP) when decoding byte strings, such as when reading a batch script (decoded line by line) or when reading from the pipe in a for /f loop. chcp.com sets both the input codepage and output codepage. We can use chcp.com 65001 to work with UTF-8 string literals in a batch script. Just take care to not save the file with a BOM since CMD doesn't ignore it. Also, at the start it's good to save the current input codepage from chcp.com, so it can be restored at the end.

It's especially important to not leave the input codepage set to 65001, since this limits legacy applications to 7-bit ASCII if they read from the console via ReadFile or ReadConsoleA. The console's bytes API still doesn't support reading the input buffer as UTF-8 since it makes the simple-minded assumption of 1 (ANSI) byte per character, whereas UTF-8 is 1-4 bytes per character. WideCharToMultiByte is called per character and fails if any character requires more than 1 byte, in which case it's read as a NUL byte (e.g. "SP膧M" is read as "SP\x00M" instead of UTF-8 "SP\xc4\x80M"). If we restore the previous codepage (probably OEM), at least it supports about 128 non-ASCII locale characters.

For cmd.exe, you can set your prompt with the PROMPT command for the current instance, or setx PROMPT to set it for future cmd.exe instances. You might run into other issues though, because cmd.exe doesn't support utf-8 by default, so you'll probably need a chcp 65001 somewhere before that to get it to work right. cmd.exe also doesn't support empji on the input line quite yet, so you'll probably also need to use a batch script to actually set it.

CMD uses the console's wide-character API. The command line is read via ReadConsoleW, and the prompt is written via WriteConsoleW. Also, the PROMPT environment variable is Unicode, so UTF-16LE surrogate pairs in its value get passed along to the console via WriteConsoleW.

CMD uses the console output codepage (i.e. GetConsoleOutputCP) when decoding byte strings, such as when reading a batch script (decoded line by line) or when reading from the pipe in a for /f loop. chcp.com sets both the input codepage and output codepage. We can use chcp.com 65001 to work with UTF-8 string literals in a batch script. Just take care to not save the file with a BOM since CMD doesn't ignore it. Also, at the start it's good to save the current input codepage from chcp.com, so it can be restored at the end.

It's especially important to not leave the input codepage set to 65001, since this limits legacy applications to 7-bit ASCII if they read from the console via ReadFile or ReadConsoleA. The console's bytes API still doesn't support reading the input buffer as UTF-8 since it makes the simple-minded assumption of 1 (ANSI) byte per character, whereas UTF-8 is 1-4 bytes per character. WideCharToMultiByte is called per character and fails if any character requires more than 1 byte, in which case it's read as a NUL byte (e.g. "SP膧M" is read as "SP\x00M" instead of UTF-8 "SP\xc4\x80M"). If we restore the previous codepage (probably OEM), at least it supports about 128 non-ASCII locale characters.

Your answer is quite detailed, I will also try, thanks a lot~ 馃憦

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mdtauk picture mdtauk  路  3Comments

Wid-Mimosa picture Wid-Mimosa  路  3Comments

TayYuanGeng picture TayYuanGeng  路  3Comments

ghvanderweg picture ghvanderweg  路  3Comments

ghost picture ghost  路  3Comments