Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I view EBCDIC formatted files in VIM?

Tags:

vim

What is the best way to view an EBCDIC formatted file in VIM?

like image 412
Aaron Thomas Avatar asked Jul 07 '16 13:07

Aaron Thomas


1 Answers

On a mainframe

First of all, if Vim was compiled and is running on a system which default encoding is EDCDIC (e.g. an IBM manframe with an ANSI C compiler) then Vim will open EBCDIC files in the system's code page by default. Such an instance of Vim will have:

has("ebcdic")

evaluating to 1. And when invoked with --version Vim will print:

+ebcdic

Instances of Vim not compiled in an EBCDIC environment will never evaluate has("ebcdic") to true. This feature of Vim is needed because other features behave differently in a purely EBCDIC environment.


Not on a mainframe

Yet, most systems today do not use EBCDIC code pages. For the situation where a file encoded in an EBCDIC code page needs to be edited in Vim on a more popular system, Vim uses the iconv library. In essence, to be able to open a file encoded in an EBCDIC code page Vim needs to be compiled with iconv support. iconv support can be tested by evaluating:

has("iconv")

Or searching for the

+iconv

string in the output of vim --version.

EBCDIC has several code pages, and Vim will only be capable of using the code pages supported by the iconv library it was compiled against. To check which code pages are available you can use the iconv utility that comes together with the iconv library:

iconv --list

Now, let's assume that we have a file called myfile encoded with the EBCDIC-US code page (also called the EBCDIC-037 code page) and that the iconv installed on the system supports this code page.

Before opening the file in Vim we need to set Vim's encoding to utf-8, in ~/.vimrc we need:

set enc=utf-8

:h fenc advises that the encoding must be set to utf-8 if any file conversion (through iconv) is performed. Otherwise data loss is possible when writing the file back.

Now we open the file in Vim with vim myfile and see mangled characters. That is fine, we now need to perform the conversion using iconv with:

:e ++enc=EBCDIC-US

Vim will now display the file in utf-8 but will save the file in EBCDIC-US, both accomplished using iconv conversions on the fly.


Closing notes

The mapping between IBM's naming of code pages:

EBCDIC-037
EBCDIC-273
EBCDIC-500
...

and iconv's names

EBCDIC-US
EBCDIC-AT-DE-A
EBCDIC-BE
...

Is often non-trivial. Yet, if the display encoding (enc) is set to utf-8 there should be no issue in trying different code pages with

:e ++enc=EBCDIC-US
:e ++enc=EBCDIC-AT-DE-A

until the correct conversion is found.


Extra note: consider using vi.SE if you have more questions related to Vim.

like image 153
grochmal Avatar answered Sep 23 '22 21:09

grochmal