Wikia

Vim Tips Wiki

Detect non-Unicode Xterms

Talk0
1,613pages on
this wiki
Revision as of 14:43, April 22, 2014 by 207.238.217.68 (Talk)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Tip 1128 Printable Monobook Previous Next

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 in 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

CommentsEdit

Isn't it more simple to do:

let &termencoding=&encoding

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:

set encoding=utf-8

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


Around Wikia's network

Random Wiki