Match the BOM

79 views
Skip to first unread message

zod

unread,
Nov 19, 2008, 12:34:04 PM11/19/08
to vim_multibyte
How do you match the BOM in a file you are editing in vim?

I've tried the following:

/\xfeff
/\o177377

I know you can just set nobomb to get rid of it, but I'm just
wondering how you would regex it. Does the BOM fall outside of the
unicode range that vim's regex engine uses or do I just have the
syntax wrong?

Thanks,

zod

Tony Mechelynck

unread,
Nov 19, 2008, 10:13:11 PM11/19/08
to vim_mu...@googlegroups.com

The BOM doesn't fall outside the Unicode range, it is not considered
part of the text, so when reading a file in ucs-bom mode, Vim strips the
BOM and sets the local 'bomb' option to remember that it must be added
back when writing the file.

To know if the current file has a BOM at its start, just use

if &bomb

You can't match it with a regex because it is not stored with the file's
data.


Best regards,
Tony.
--
What does it mean if there is no fortune for you?

zod

unread,
Nov 19, 2008, 11:44:30 PM11/19/08
to vim_multibyte
Thanks, Tony. That was driving me crazy.

François Pinard

unread,
Nov 20, 2008, 7:04:13 AM11/20/08
to vim_mu...@googlegroups.com
[Tony Mechelynck]

>On 19/11/08 18:34, zod wrote:

>> Does the BOM fall outside of the unicode range that vim's regex
>> engine uses or do I just have the syntax wrong?

>The BOM doesn't fall outside the Unicode range, it is not considered
>part of the text [...]

Just for the record (and to be pedantic), while the BOM is a Unicode
character, the reversed BOM is not part of Unicode. This is more
a philosophical detail than a technical issue. :-)

--
François Pinard http://pinard.progiciels-bpi.ca

Tony Mechelynck

unread,
Nov 21, 2008, 2:16:27 AM11/21/08
to vim_mu...@googlegroups.com
On 20/11/08 13:04, François Pinard wrote:
> [Tony Mechelynck]
>
>> On 19/11/08 18:34, zod wrote:
>
>>> Does the BOM fall outside of the unicode range that vim's regex
>>> engine uses or do I just have the syntax wrong?
>
>> The BOM doesn't fall outside the Unicode range, it is not considered
>> part of the text [...]
>
> Just for the record (and to be pedantic), while the BOM is a Unicode
> character, the reversed BOM is not part of Unicode. This is more
> a philosophical detail than a technical issue. :-)
>

To be still more pedantic, U+FFFE is part of the Unicode range, where it
is listed as "Not a character", i.e., it is one of the "forbidden"
codepoints which are "in range". (The "original" Unicode range as still
supported by Vim for UTF-8, UTF-32be and UTF-32le, used to be from
U+0000 to U+7FFFFFFF. The Unicode Consortium later invalidated, among
others, (a) all planes above plane 0x10, and (b) the last two codepoints
U+xxFFFE and U+xxFFFF in every plane, which brings the highest "valid"
codepoint down to U+10FFFD at most.)


Best regards,
Tony.
--
His head smashed in, and his heart cut out,
And his liver removed, and his bowels unplugged,
And his nostrils raped, and his bottom burned off,
And his penis split ... and his ...
"Monty Python and the Holy Grail" PYTHON (MONTY)
PICTURES LTD

Reply all
Reply to author
Forward
0 new messages