tags:

views:

297

answers:

2

I have a file with some accents, and VIM displays them as "~V" characters. The "od -bc" command tells me the characters are charcode 226. I want to substitute them using VIM. But I can't get it to match the characters. How can I achieve that?

Optional question: how can I have VIM tell me which charset is used to interpret the current file?

+1  A: 

You can use the following formats, from vim's manual on patterns and regular expressions:

ordinary atom
magic   nomagic matches
\%d \%d match specified decimal character (eg \%d123
\%x \%x match specified hex character (eg \%x2a)
\%o \%o match specified octal character (eg \%o040)
\%u \%u match specified multibyte character (eg \%u20ac)
\%U \%U match specified large multibyte character (eg \%U12345678)

So you should be able to do something like this to replace char 226 with a space globally in the file:

:%s/\%d226/ /g

As for the latter, if you do:

:set encoding

You'll see output like:

encoding=latin1
Chris J
Sadly, VIM outputs "E71: Invalid character after \%" when I try that.
Alsciende
I get E486: Pattern not found (which I expect). However if I omit the 'd' (i.e., I just do s/\%226/), I do get E71. Exactly what command are you trying?
Chris J
I'm typing :%s/\%d226/é/g
Alsciende
What's the encoding of the file? I'm wondering if it's unicode, so needs a Unicode specifier with the \%u pattern...
Chris J
A: 

One very simple way to deal with such "weird" characters is:

  1. select the offending character(s) visually (v)
  2. yank it to buffer
  3. replace it with: :%s/<ctrl-r>"/something-else/g

where <ctrl-r> is pressing ctrl and letter r - together with " it will copy buffer to command line - effectively putting your offending characters inside of s/// operation.

depesz