有没有一种方法可以以某种十六进制模式编辑二进制文件?例如,如果我有xxdhexdump -C所示的一些二进制数据,例如: />
如果要在特定位置更改值,
这种视图将有助于找到正确的位置,例如当更改位置在某个已知字符串附近时。

#1 楼

最简单的方法是使用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进行验证。现在已更改。
如果将LANGLC_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.


最后一点,您可以使用%B:set rulerformat=0x%B)在标尺中显示光标
下的字符的十六进制值。

更高级的功能:xxd


您可以使用xxd(1)工具将文件转换为更具可读性的格式,并且
(这很重要位),解析已编辑的“可读格式”并将其作为二进制数据写回。 xxdvim的一部分,因此,如果您安装了vim,则
还应该安装xxd

要使用它: >或者,如果您已经打开文件,则可以使用:

$ xxd /bin/ls | vi -


现在进行更改,您需要在文件的左侧进行更改display
(十六进制数字),对右侧的更改(可打印的表示形式)
在写入时将被忽略。
:%!xxd


这会将文件保存到xxd -r

或将二进制文件加载到当前缓冲区中:

:%!xxd -r > new-ls


来自new-ls

:%!xxd -r


然后只需使用xxd(1)来编写它。 (请注意:出于上述相同的原因,您要在写入文件之前设置:w
选项)。
   -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.


如果您使用的是gVim,也可以从菜单中使用,该菜单位于“工具➙
转换为十六进制”和“工具➙向后转换”下。 >
vim提示Wiki上有一个页面,其中包含更多的信息和一些帮助脚本。就个人而言,我认为如果经常编辑二进制文件,最好使用真正的十六进制编辑器。 Vim可以做到这一点,但是显然不是为它而设计的,如果您
不用binary进行写,Vim可能会破坏您的二进制文件!

评论


答案很不错,但应该以“孩子们不要在家尝试这个”开头。

– msw
16-10-22在23:24

如果我需要删除一些字节怎么办?例如在二进制文件中间。

–安东K
17年11月17日在13:59

我不知道Vim在做什么,但是尽管我什么都没做,但它仍将95KB的文本添加到200KB的二进制文件中。即使使用:set binary noeol fenc = utf-8。实际上,它在打开文件后立即说[noeol] [converted]。为什么vim需要将缓冲区增大150%?我如何防止它破坏这样的文件?

– Braden Best
18年4月1日在19:35

唯一有效的方法是::r!xxd (或$ xxd | vim-)读取,以及:w!xxd -r> 写入,但这并不理想。

– Braden Best
18年4月1日在19:46



极好的答案。请注意,bless的网址无效。我在github.com/bwrsandman/Bless的github上找到了它。

– Sonofagun
19年1月10日,在1:25

#2 楼

要以十六进制视图查看二进制文件的内容,
打开文件,打开二进制模式,然后通过xxd命令过滤缓冲区:

:set binary
:%!xxd


您可以在左侧区域进行更改(编辑十六进制数字),
准备好后,过滤xxd -r,最后保存文件:

:%!xxd -r
:w


如果打开后和关闭前的过滤步骤听起来很繁琐,
并且您经常对扩展名为.bin的文件执行此操作,则可以将其添加到vimrc中以使其自动执行:
" for hex editing
augroup Binary
  au!
  au BufReadPre  *.bin let &bin=1
  au BufReadPost *.bin if &bin | %!xxd
  au BufReadPost *.bin set ft=xxd | endif
  au BufWritePre *.bin if &bin | %!xxd -r
  au BufWritePre *.bin endif
  au BufWritePost *.bin if &bin | %!xxd
  au BufWritePost *.bin set nomod | endif
augroup END


评论


如果我遵循这些说明(打开二进制文件,:%!xxd,:%!xxd -r,:w,没有进行任何更改!),那么所写的二进制文件与原始文件不一样...是这种情况为您(我用/ bin / ls测试)。我需要在保存之前使用:set binary(另请参见说明原因的答案)...也许这是我的vimrc中的内容?但是无论如何,为了安全起见,我将始终使用set binary ...

–马丁·图尔诺伊(Martin Tournoij)
2015年2月4日在21:31



如果您不希望使.vimrc混乱,则可以改为将augroup脚本添加到〜/ .vim / plugin / binary.vim中。

–thom_nic
17年9月6日16:00

如果您使用的是国外安装,则从5.5(1999年9月)开始,该augroup Binary列表位于:vi hex-editing或:help using-xxd中。

– bb010g
19年8月15日在23:34

#3 楼

使用“ bvi”编辑器。 http://bvi.sourceforge.net/(在每个Linux存储库中。)

$ apt-cache show bvi
[snip]
Description-en: binary file editor
 The bvi is a display-oriented editor for binary files, based on the vi
 text editor. If you are familiar with vi, just start the editor and begin to
 edit! If you never heard about vi, maybe bvi is not the best choice for you.


评论


更高级的替代方法是bviplus,它具有vim控件。

–安东K
17年11月17日在14:27

Bviplus主页和屏幕截图。

–尤利安·奥诺夫雷(Iulian Onofrei)
18年5月8日在20:23



#4 楼

TL; DR Answer

用Vim以二进制模式打开文件:

vim -b <file_to_edit>


在Vim中,进入十六进制编辑模式,如下所示: br />
:%!xxd -p


要保存:

:%!xxd -p -r
:w


第一行将缓冲区从十六进制模式转换回去,第二行行会像平常一样保存文件。

请注意-p选项。这样可以避免所有多余的可打印和地址模糊,而仅显示十六进制。如果需要额外的上下文,只需省略-p。它。

评论


这实际上并没有添加其他答案中没有的任何内容。

–草药
17年3月29日在4:17

真正的TL; DR在使用-xxd的:h中,并且从v7.0001开始存在,并且可能更长。如果人们搜索文档,则该站点的活跃度将降低。

–汤米A
17 Mar 30 '17 at 13:15

#5 楼

这看起来像一个方便的小vim插件,它使用临时文件自动完成此工作,该文件会自动为您来回写。采用。如果有人想要的话,我已经在这里包含了相关的代码。它也基于xxd工具。我确定上面链接的GitHub版本可以更好地工作,但是我自己还没有真正使用过它,所以我想我也应该发布这个我肯定可以使用的版本。

源其他版本是vim Wikia,特别是此页面。

代码如下:

"-------------------------------------------------------------------------------  
" Hexmode  
"-------------------------------------------------------------------------------  
" Creates an automatic hex viewing mode for vim by converting between hex dump  
" and binary formats. Makes editing binary files a breeze.  
"-------------------------------------------------------------------------------  
" Source: vim.wikia.com/wiki/Improved_Hex_editing  
" Author: Fritzophrenic, Tim Baker  
" Version: 7.1  
"-------------------------------------------------------------------------------  
" Configurable Options {{{1  
"-------------------------------------------------------------------------------  

" Automatically recognized extensions  
let s:hexmode_extensions = "*.bin,*.exe,*.hex"  

"-------------------------------------------------------------------------------
" Commands and Mappings {{{1
"-------------------------------------------------------------------------------

" ex command for toggling hex mode - define mapping if desired
command! -bar Hexmode call ToggleHex()
command! -nargs=0 Hexconfig edit $VIM\vimfiles\plugin\hexmode.vim | exe "normal 11G" | exe "normal zo"

nnoremap <C-H> :Hexmode<CR>
inoremap <C-H> <Esc>:Hexmode<CR>
vnoremap <C-H> :<C-U>Hexmode<CR>

"-------------------------------------------------------------------------------    
" Autocommands {{{1  
"-------------------------------------------------------------------------------  

if exists("loaded_hexmode")  
    finish  
endif  
let loaded_hexmode = 1  

" Automatically enter hex mode and handle file writes properly  
if has("autocmd")  
  " vim -b : edit binary using xxd-format  
  augroup Binary  
    au!  

    " set binary option for all binary files before reading them  
    exe "au! BufReadPre " . s:hexmode_extensions . " setlocal binary"

    " if on a fresh read the buffer variable is already set, it's wrong
    au BufReadPost *
          \ if exists('b:editHex') && b:editHex |
          \   let b:editHex = 0 |
          \ endif

    " convert to hex on startup for binary files automatically
    au BufReadPost *
          \ if &binary | Hexmode | endif

    " When the text is freed, the next time the buffer is made active it will
    " re-read the text and thus not match the correct mode, we will need to
    " convert it again if the buffer is again loaded.
    au BufUnload *
          \ if getbufvar(expand("<afile>"), 'editHex') == 1 |
          \   call setbufvar(expand("<afile>"), 'editHex', 0) |
          \ endif

    " before writing a file when editing in hex mode, convert back to non-hex
    au BufWritePre *
          \ if exists("b:editHex") && b:editHex && &binary |
          \  let oldro=&ro | let &ro=0 |
          \  let oldma=&ma | let &ma=1 |
          \  silent exe "%!xxd -r" |
          \  let &ma=oldma | let &ro=oldro |
          \  unlet oldma | unlet oldro |
          \ endif

    " after writing a binary file, if we're in hex mode, restore hex mode
    au BufWritePost *
          \ if exists("b:editHex") && b:editHex && &binary |
          \  let oldro=&ro | let &ro=0 |
          \  let oldma=&ma | let &ma=1 |
          \  silent exe "%!xxd" |
          \  exe "set nomod" |
          \  let &ma=oldma | let &ro=oldro |
          \  unlet oldma | unlet oldro |
          \ endif
  augroup END  
endif  

"-------------------------------------------------------------------------------
" Functions {{{1
"-------------------------------------------------------------------------------

" helper function to toggle hex mode
function! ToggleHex()
  " hex mode should be considered a read-only operation
  " save values for modified and read-only for restoration later,
  " and clear the read-only flag for now
  let l:modified=&mod
  let l:oldreadonly=&readonly
  let &readonly=0
  let l:oldmodifiable=&modifiable
  let &modifiable=1
  if !exists("b:editHex") || !b:editHex
    " save old options
    let b:oldft=&ft
    let b:oldbin=&bin
    " set new options
    setlocal binary " make sure it overrides any textwidth, etc.
    let &ft="xxd"
    " set status
    let b:editHex=1
    " switch to hex editor
    set sh=C:/cygwin/bin/bash
    %!xxd
  else
    " restore old options
    let &ft=b:oldft
    if !b:oldbin
      setlocal nobinary
    endif
    " set status
    let b:editHex=0
    " return to normal editing
    %!xxd -r
  endif
  " restore values for modified and read only state
  let &mod=l:modified
  let &readonly=l:oldreadonly
  let &modifiable=l:oldmodifiable
endfunction

" vim: ft=vim:fdc=2:fdm=marker