I saw that you could display the current byte offset in the statusline using
%o, but I found no function or command which does the same. Is there a way of getting the current byte offset pragmatically?
function! FileOffset() return line2byte(line('.')) + col('.') - 1 endfunction
This returns the 1-based offset in file, which is the same as
statusline. You can, of course, subtract 1 to get the 0-based offset.
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 offset = line2byte(line('.')) + col('.') - 1 if version < 781 && &l:binary == 1 && &l:eol == 0 " Vim prior 7.4.781 had a bug where the line count is off by 1 or 2. " See https://groups.google.com/forum/#!msg/vim_dev/zX45zm-cnc0/-BWjjh5tlX8J let offset += 1 let offset += line('.') == 1 endif
This bug also affects the
%o specifier in e.g.