Detect non-Unicode Xterms
From Vim Tips Wiki
created 2006 · complexity basic · author Adrian Perez · version 6.0
I like to do my editing work in Unicode (say UTF-8) and Vim is quite good at this: setting encoding=utf8 does the job. But there are terminals which do not work natively in UTF-8. A good example is the Linux text console: you may set it to Unicode mode, but with a Spanish keymap you won't be able to enter accented characters without reverting to non-Unicode mode. Another example is Xterm, either old Xterms or ones which are not started un UTF-8 mode. My vimrc contains the following snippet which selects a sane value for termencoding in those terminals:
if has("multi_byte") set encoding=utf-8 if $TERM == "linux" || $TERM_PROGRAM == "GLterm" set termencoding=latin1 endif if $TERM == "xterm" || $TERM == "xterm-color" let propv = system("xprop -id $WINDOWID -f WM_LOCALE_NAME 8s ' $0' -notype WM_LOCALE_NAME") if propv !~ "WM_LOCALE_NAME .*UTF.*8" set termencoding=latin1 endif endif endif
Isn't it more simple to do:
at the beginning of the vimrc, which also works quite well?
Doing that you set the terminal encoding to utf-8 as well, because I want Vim to always utf-8 internally. That's why the second line says:
The problem is that setting the "termencoding" to the same value as "encoding" you make Vim believe that the terminal it's running on supports utf-8, and it will output utf-8 sequences to the terminal. If your terminal does not support utf-8 (i.e: aterm, Linux and OpenBSD virtual terminals, xterm without -u8, and so on) you will see weird characters. So I detect wether Xterm is in unicode mode with "xprop" and set "termencoding" to "latin1" for terminals which do not support unicode. Note that this keeps Vim working internally in utf-8, which is a Good Thing, IMHO.
These are some good heuristics, but I think the better place for them is in your shell startup script (e.g. ~/.bashrc or similar). That way you can guarantee that your $LANG environment variable is correct for all your applications, and then you can rely on it in your vimrc.
sample code wont cover case when xprop returns locale with 'utf' part in lower case. propv !~ "WM_LOCALE_NAME .*[Uu][Tt][Ff].*8" covers both