Jan Weytjens wrote:
I use a Belgian azerty keyboard and use nmap in _vimrc (vim 7.0 on Windows 2000) to map the ² and µ keys to ~ (² is more or less in the same place on the keyboard as the tilde on an american qwerty and one can think of µ as standing for Majuscule).

These mappings don't work in files for which the encoding and fileencoding are set to utf-8 (.xsd files, ...). It does not make a difference what the encoding and fileencoding of _vimrc are (latin-1 or utf-8). Changing the encoding to latin-1 at the ex prompt makes it work (but makes special characters unreadable). Changing the encoding back to utf-8 at the ex prompt makes the mapping stop working. Curiously, when the mapping is done at the ex prompt (with the encoding set to utf-8), then it does work.

Also, some scripts don't work when the encoding is utf-8 (e.g., EnhancedCommentify-2.2 with Alt-X).

Is there a way to make them work in utf-8 encoded files?

Jan



1) Make sure that Vim still interprets your keyboard data as 8-bit, not Unicode, if the OS default is 8-bit. This is done when switching over to UTF-8

   if &termencoding == ""
      let &termencoding = &encoding
   endif
   set encoding=utf-8

Rationale: The 'termencoding' option, which tells Vim how the keyboard sends data, is empty by default. An empty value means "use the value of 'encoding'". That's OK as long as you don't set 'encoding' yourself (the default for 'encoding' is got from the OS at startup time.) But if you change 'encoding', that doesn't make the keyboard driver send its data any differently.

2) If this doesn't work, keep it, but add

   scriptencoding latin1

near the top of your vimrc.

3) If if it _still_ doesn't work, keep both (1) and (2), but try redefining the mappings in addition, as follows:

   function! SpecialMaps()
      " remove mappings if defined
      silent! unmap <Char-178>
      silent! unmap! <Char-178>
      silent! unmap <Char-181>
      silent! unmap! <Char-181>
      " map exponent 2 to tilde
      noremap    <Char-178>   <Char-126>
      noremap!   <Char-178>   <Char-126>
      " map Greek mu to tilde
      noremap    <Char-181>   <Char-126>
      noremap!   <Char-181>   <Char-126>
      " if we need more mappings with bytes >= 128,
      " add them here.
   endfunction
   if has("autocmd") && version >= 700
       augroup vimrcmaps
           au! EncodingChanged * SpecialMaps()
       augroup END
   endif

Rationale: Characters above 127 are represented in memory (and on disk) by more than one byte in UTF-8. From 128 to 255 the character number (as shown in <Char-nnn>) is the same for Latin1 and UTF-8 but the byte representation is different.

The "if" statement takes care of the fact that EncodingChanged is new in version 7. The map statements without bang are for Normal, Visual, etc.; with bang they are for Insert, Command-Line, etc.

4) If it still doesn't work, wrap the map lines in the function above in ":exe" commands with an argument in _single_ quotes, for instance

      exe 'noremap <Char-181> <Char-126>'

This will force evaluation of <> when storing the mapping, not the function.


Best regards,
Tony.

Reply via email to