123

You can use the xxd command to transform a file in Vim to hex representation, doing :%!xxd : enters command-line mode, % matches whole file as a range, ! filters that range through an external command, xxd is that external shell command Giving an output like this, this is split into octet count/line (octets per line may be changed with parameter -c on xxd ...


91

The simplest way is to use the binary option. From :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 ...


19

To view the content of a binary file in a hex view, open the file, switch on binary mode, and filter the buffer through the xxd command: :set binary :%!xxd You can make changes in the left area (edit the hex numbers), and when ready, filter through xxd -r, and finally save the file: :%!xxd -r :w If the filtering step after opening and before closing ...


15

Functionality like this is handled by autocmds. In order to disable autocmds for a specific command, you can use :noautocmd (abbreviated :noau). In this case :noau e foo.xlsx will simply open the raw file rather than triggering the autocmds that try to open the zip file. You can also use this from your shell: $ vim -c 'noau e foo.xlsx' If you want ...


15

:goto 2356 jumps to the 2356th byte in the buffer. Use the %o field in 'statusline' or 'rulerformat' to display the byte number of the character under your cursor. Use %O to display the value in hexadecimal format. Reference: :help :goto :help 'statusline' :help 'rulerformat'


14

Taken from :h hex-editing: If one has a particular extension that one uses for binary files (such as exe, bin, etc), you may find it helpful to automate the process with the following bit of autocmds for your <.vimrc>. Change that "*.bin" to whatever comma-separated list of extension(s) you find yourself wanting to edit: " vim -b : edit binary using ...


10

This seems to the the "zip" plugin, which is shipped with Vim and enabled by default. :help zip has some information about it, among other things: PREVENTING LOADING If for some reason you do not wish to use vim to examine zipped files, you may put the following two variables into your <.vimrc> to prevent the zip plugin from loading: let g:...


9

I ended up using the following solution, which implements the logic from my question. [count]GO to move [count] bytes forward. [count]Go to move [count] bytes backwards. Add this to your .vimrc: function! JumpToByte(byte_nr) " See https://vi.stackexchange.com/a/3911/2720 for the byte counting bug let crt_byte = line2byte(line('.')) + col('.') - 1 ...


9

This search moves 40 chars (not bytes, though) forward: /\_.\{40}/e by searching for exactly 40 chars (\{40}) of any kind, including newline (\_.), and placing the cursor at the end of the search (/e). See: http://vimregex.com/#Non-Greedy, :help search-offset and :help \_ Also, see :h 23.4 for binary editing. Update: Based on this answer, here's a ...


9

If we look at /usr/share/vim/vim80/plugin/gzip.vim we can see how the plugin does this: augroup gzip " Remove all gzip autocommands au! " Enable editing of gzipped files. " The functions are defined in autoload/gzip.vim. " " Set binary mode before reading the file. " Use "gzip -d", gunzip isn't always available. autocmd BufReadPre,...


8

Try this: function! FileOffset() return line2byte(line('.')) + col('.') - 1 endfunction This returns the 1-based offset in file, which is the same as %o in statusline. You can, of course, subtract 1 to get the 0-based offset.


7

Most likely, Vi adds a trailing linefeed. There used to be whole flame wars whether editors should add a trailing linefeed for the content. It is a convention, that differs between Windows (there you would usually talk about line separators) while on Unix like operating systems one usually speaks about line terminators. (The most convincing argument for ...


7

It is distributed with Vim (as xxd.exe), along with diff.exe. So it exists and should work on Windows. You don't need to install anything separately, as it is already bundled with Vim (at least in the versions available from vim.org).


7

One approach is to let your operating system mount the zip file as a directory. Then the problem reduces to applying the transformation to all the files in that directory (and its subdirectories). Most modern Unix systems (including Linux and OS X) support FUSE, which allows user code to implement filesystems. For example, you can use [fuse-zip]: Create an ...


6

The other answer did not work for me when I opened a binary file without line ending. It seems that there is a bug in vim when it comes to counting bytes in a binary file without eol. (edit: yes, this was a bug. I have submitted a patch, which got accepted in 7.4.781). To find the byte offset, while accounting for the bug in old Vim versions, use: let ...


6

Use the "bvi" editor. http://bvi.sourceforge.net/ (It's in every Linux repository.) $ 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 ...


5

There are two answers to your question: General In order to open a file, overriding the default filetype detection, you can just specify a :setf[iletype] command as part of :edit. For example: :edit +setf\ java strange.cpp The default filetype detection does not change an existing filetype, that's how this works. Of course, you can also do this ...


5

Most programs (such as grep, git, mercurial, etc.) consider a file to be binary if it contains a NULL byte. You can check this with Vim like so: fun! IsBinary() return !!search('\%u0000', 'wn') endfun Note that this searches the entire buffer; and won't be very fast for large files. A faster way would be to use the file utility: fun! IsBinary2() ...


4

Starting it 2005 vim added support for editing zip files natively. The build in support uses native zip utilities (zip, gzip, jar, xpi, etc) to manipulate these files. It does not look as if there is a direct way to interact with a compressed stream of data unless you use external tools. vim <zipfile> should open a buffer listing all files in the zip ...


3

TL;DR Answer Open the file with Vim in binary mode: vim -b <file_to_edit> In Vim, get into hex editing mode like so: :%!xxd -p To save: :%!xxd -p -r :w This will convert the buffer back from hex mode and then save the file like normal. Note the -p option. This avoids all the extra printable and address fluff and just shows you the hex. Just ...


2

Edit: notice after posting there's already this answer in the comments. Run vim in binary mode as vim -b asterix. I think the problem has something to do with conversions caused by encoding/fileencoding settings. If your file is already loaded, you can switch to binary mode without leaving vim. Save the file, then do :e ++bin.


2

Vim ships with the zip plugin (:help pi_zip) that lets you automatically browse and edit the contents of the zip file. These browse buffers are read-only. In order to read the actual zip file, you need to use binary mode: $ vim -b archive.zip. If you did use binary mode, the readonly buffer originates from the zip file's file system permissions. Sometimes ...


2

Using external Unix tools is easy: :%!gzip -d :%!gunzip -d


2

Your mentioned automatic uncompression of ZIP files (and other archive formats) is already built into Vim, cp. :help pi_zip. The plugin sets up :autocmds that detect ZIP files based on their file extension (:au zip lists them all), not on their contents. There's no detection built in, because it doesn't make much sense to edit non-text files with a text ...


1

This looks like a handy little vim plugin that does the job using a temporary file which it writes back and forth for you automatically. Some years ago I found a similar plugin which I adapted and improved for my own use. I've included the relevant code for that here, in case anyone wants it. It too is based on the xxd tool. I'm sure the GitHub version I ...


Only top voted, non community-wiki answers of a minimum length are eligible