Score:2

How to edit UTF-8 files with vim

mo flag

How can I edit UTF-8 files with vim?

There is a problem with german special characters like 'Ä', 'ü'. There are some in the comments. vim shows Ã~D instead of 'Ä' and ü instead of 'ü' then. (I hope there is no encoding problem on your browser :)

I have no "~/.vimrc" file.

In the "~/.viminfo" is

...
# Value of 'encoding' when this file was written
*encoding=latin1
...

If I enter locale, it gives LANG=C.UTF-8. It was LANG=C but I changed with export LANG=C.UTF-8. But nothing changed.

I'm using MobaXterm 11.0 to get access from a Win10 computer. Maybe there is the problem?

(Linux v. "Ubuntu 18.04.4 LTS")


Update:

  • MobaXterm was already (and still is) set to UTF-8
  • I have changed with [CTRL] + right mouse key (then there is a menu) the shell to UTF-8.
  • In addition I set export LC_TYPE=de_DE.UTF-8 ("de_DE"!)

Now it works a bit better :-|

  • more and cat works
  • less and vi not

With set enc:utf8 in vi, it works there. (So vi is a little part of the solution)

And about less I should maybe read the manpages...?

muru avatar
us flag
What's the problem you have with UTF-8 files?
Andy A. avatar
mo flag
There are german comment in the files. So there shoud be an 'Ä' but vi shows something like 'A~D' (whith an ~ on the head of the 'A').
muru avatar
us flag
Does it show correctly if you `cat` the file? What is your `LC_CTYPE`?
Andy A. avatar
mo flag
No, its the same. If I pipe the result to `less` there is "<C3><84>" instead of "Ä" (bash is not in UTF-8)
muru avatar
us flag
So it's not Vim but pretty everything that's broken. What value do you have for `LC_CTYPE`?
Andy A. avatar
mo flag
This is still "C" like everything else but `LANG` and `LANGUAGE`. `LC_CTYPE="C"`
muru avatar
us flag
Try with a UTF-8 locale for `LC_CTYPE` (`C.UTF-8` should work).
Score:2
cn flag

If the encoding of the file is messed up you can let Vim now by entering these commands:

: set enc=utf8
: set fenc=utf8

The first command controls how the content is displayed, the second how it is written to disk.

However, the problem with wrong encoding could also be in a SSH-client like putty for instance, if you try to edit over SSH. If so have a look at Putty setting to get right encoding.

Score:0
cn flag

Put this in your .vimrc:

function! Latin()     
  silent! exe "e! ++enc=latin1"    
endfunction

function! Utf8()
  silent! exe "e! ++enc=utf-8" 
endfunction

Call this way typing Esc and ':'

:call Utf8()

or

:call Latin()
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.