Vim Tips Wiki
(cleanup and adding category - still needs comment merge)
(Spelling error)
 
(6 intermediate revisions by 3 users not shown)
Line 1: Line 1:
 
{{review}}
 
{{review}}
  +
{{TipImported
{{Tip
 
 
|id=1128
 
|id=1128
  +
|previous=1127
|title=Detecting non-unicode Xterms
 
  +
|next=1129
|created=February 10, 2006 19:04
+
|created=2006
 
|complexity=basic
 
|complexity=basic
 
|author=Adrian Perez
 
|author=Adrian Perez
 
|version=6.0
 
|version=6.0
 
|rating=15/8
 
|rating=15/8
  +
|category1=Encoding
|text=
 
 
|category2=Terminals
 
}}
 
}}
 
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:
 
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 of entering 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 the a sane value for termencoding in those terminals:
 
   
 
<pre>
 
<pre>
Line 31: Line 32:
 
Isn't it more simple to do:
 
Isn't it more simple to do:
 
<pre>
 
<pre>
let &amp;termencoding=&amp;encoding
+
let &termencoding=&encoding
 
</pre>
 
</pre>
  +
at the beginning of the .vimrc, which also works quite well?
+
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:
 
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:
 
 
<pre>
 
<pre>
 
set encoding=utf-8
 
set encoding=utf-8
Line 45: Line 46:
   
 
----
 
----
  +
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.
   
  +
----
[[Category:Terminals]]
 
  +
sample code wont cover case when xprop returns locale with 'utf' part in lower case.
[[Category:Encoding]]
 
  +
propv !~ "WM_LOCALE_NAME .*[Uu][Tt][Ff].*8"
  +
covers both
  +
  +
----

Latest revision as of 14:43, 22 April 2014

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

Comments[]

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