最も簡単な方法は、binaryオプションを使用することです。から:help binary:
This option should be set before editing a binary file.  You can also
use the -b Vim argument.  When this option is switched on a few
options will be changed (also when it already was on):
        'textwidth'  will be set to 0
        'wrapmargin' will be set to 0
        'modeline'   will be off
        'expandtab'  will be off
Also, 'fileformat' and 'fileformats' options will not be used, the
file is read and written like 'fileformat' was "unix" (a single <NL>
separates lines).
The 'fileencoding' and 'fileencodings' options will not be used, the
file is read without conversion.
[..]
When writing a file the <EOL> for the last line is only written if
there was one in the original file (normally Vim appends an <EOL> to
the last line if there is none; this would make the file longer).  See
the 'endofline' option.
これを行わず、環境がマルチバイトエンコーディング(ほとんどの人が使用するUTF-8など)を使用している場合、Vimはテキストをそのままエンコードしようとし、通常はファイルが破損します。
これを確認するには、ファイルを開き、単にを使用し:wます。現在は変更されています。
あなたが設定した場合LANGとLC_ALLするC(ASCII)、Vimは何も変換しないと、ファイルは同じままVimは任意のマルチバイトエンコーディングを行う必要がないので(それはまだかかわらず、改行が追加されます)。
私は個人的にバイナリを無効 set wrapにすることを好みますが、他の人はそれを有効にすることを好むかもしれません。YMMV。別の便利なことはです:set display=uhex。から:help 'display':
uhex            Show unprintable characters hexadecimal as <xx>
                instead of using ^C and ~C.
最後のヒントとして、ルーラーのカーソルの下にある文字の16進値を%B(:set rulerformat=0x%B)で表示できます。
より高度な: xxd
このxxd(1)ツールを使用してファイルをより読みやすい形式に変換し、(これは重要なビットです)編集した「読みやすい形式」を解析し、バイナリデータとして書き戻すことができます。xxdはの一部でvimあるため、vimインストール済みの場合はにもインストールする必要がありますxxd。
使用するには:
$ xxd /bin/ls | vi -
または、既にファイルを開いている場合は、次を使用できます。
:%!xxd
ここで変更を行います。ディスプレイの左側(16進数)でそれを行う必要があり、右側(印刷可能な表現)への変更は書き込み時に無視されます。
保存するには、次を使用しますxxd -r。
:%!xxd -r > new-ls
これにより、ファイルがに保存されますnew-ls。
または、現在のバッファにバイナリをロードするには:
:%!xxd -r
からxxd(1):
   -r | -revert
          reverse operation: convert (or patch) hexdump into  binary.   If
          not  writing  to stdout, xxd writes into its output file without
          truncating it. Use the combination -r -p to read plain hexadeci‐
          mal dumps without line number information and without a particu‐
          lar column layout. Additional  Whitespace  and  line-breaks  are
          allowed anywhere.
そして、それを使っ:wて書くだけです。(注意:binary
上記の概要と同じ理由で、ファイルに書き込む前にオプションを設定する必要があります)。
これを少し簡単にする補完キーバインド:
" Hex read
nmap <Leader>hr :%!xxd<CR> :set filetype=xxd<CR>
" Hex write
nmap <Leader>hw :%!xxd -r<CR> :set binary<CR> :set filetype=<CR>
gVimを使用している場合は、[ツール] H [HEXに変換]および[ツール]➙[元に戻す]でメニューからも使用できます。
Vimのヒントのwikiはより多くの情報といくつかのヘルパースクリプトを持つページがあります。個人的には、バイナリファイルを頻繁に編集している場合は、実際の16進エディターを使用した方が良いと思います。Vimはやる
べきことをすることができますが、明らかにそのために設計されたものではあり:set binaryません。