Comfortable bash with Git Bash and Cmder When I tried to start my life, Python was garbled.
$ python -c "print('Japanese')"
譌 ・ 譛 ャ 隱 ・
chcp.com 65001
So add the following two lines to .bashrc
.
.bashrc
chcp.com 65001
export PAGER=less
That's all for the conclusion, but I'll leave the background to that.
$ python -c "print('Japanese')"
譌 ・ 譛 ャ 隱 ・
I typed as many character encoding confirmation commands as I could think of.
$ echo $LANG
ja_JP.UTF-8
$ echo $PYTHONIOENCODING
utf-8
$ python -c "import sys; print(sys.stdout.encoding)"
utf-8
$ python -c "import sys; print(sys.getdefaultencoding())"
utf-8
$ python -c "import locale; print(locale.getpreferredencoding(False))"
cp932
It almost returns UTF-8.
Only locale.getpreferredencoding ()
responded like cp932, but according to Documentation, this is:
Returns how to encode the text data based on the user's settings. This function is just a guess **, as user settings are expressed differently between different systems and may not be programmatically available on some systems.
It's not so suspicious.
While doing so, I found the following description in the Document of sys.stdout
:
Character encoding is platform dependent. ** On Windows, if the stream is interactive (if the isatty () method returns True), the console code page is **, otherwise the ANSI code page is used. Other platforms use locale encoding (see locale.getpreferredencoding ()).
Code page ...!
I googled because I didn't know the code page. It seems to be set with the chcp
command.
Even from Git Bash, if the path is in / C / windows / system32
, you can call it with the extension name chcp.com
.
$ chcp.com #Code page confirmation
Current code page: 932
$ chcp.com 65001 #Code page settings(UTF-8)
$ python -c "print('Japanese')"
Japanese
It was fine and I got rid of the garbled characters.
I can't see help in Python. It is displayed as Not enough memory.
.
>>> import sys
>>> help(sys.argv)
Not enough memory.
It seems that Windows more.com
does not work well under the environment of chcp 65001
.
When the content you want to display spans several pages, it displays --More (40%) --
or something that prompts you to enter a key. Such a function is called a pager.
On Linux, the less
command usually plays that role.
I want to use less.exe
instead of more.com
which is no longer available!
Python seems to use it if you specify it in the environment variable PAGER
.
I couldn't find any mention of the pager in the documentation. Is it self-evident, or is it a feature other than Python?
$ export PAGER=less
$ python
>>> import sys
>>> help(sys.argv) #From here on down, the less screen is displayed, so the Python interpreter is hidden once.
Help on list object:
class list(object)
| list() -> new empty list
| list(iterable) -> new list initialized from iterable's items
|
| Methods defined here:
|
...
Congratulations help is back.
This time I noticed in the Python help display, but other people using more.com
are likely to run into the same problem.
Even in that case, there may be something that can be solved with ʻexport PAGER = less`.
Try running locale.getpreferredencoding ()
again with the resolution resolved.
$ python -c "import locale; print(locale.getpreferredencoding(False))"
cp932
After all acquisition by getpreferredencoding
is not good enough.
If chcp 65001
is set, the result ofcmd /?
will be in English.
If you do start cmd /?
, Japanese will be displayed.
reference: [Command Prompt / How to switch between English mode and Japanese mode-chcp --Living with Windows](http://win.just4fun.biz/%E3%82%B3%E3%83%9E%E3%83%B3%] E3% 83% 89% E3% 83% 97% E3% 83% AD% E3% 83% B3% E3% 83% 97% E3% 83% 88 /% E8% 8B% B1% E8% AA% 9E% E3 % 83% A2% E3% 83% BC% E3% 83% 89% E3% 83% BB% E6% 97% A5% E6% 9C% AC% E8% AA% 9E% E3% 83% A2% E3% 83 % BC% E3% 83% 89% E3% 81% AE% E5% 88% 87% E3% 82% 8A% E6% 9B% BF% E3% 81% 88% E6% 96% B9% E6% B3% 95 % E3% 83% BBchcp.html)
It seems that the environment variable GIT_PAGER
can also be used in git.
2016/09/27 About chcp 2016/10/09 Added about PAGER
Recommended Posts