i want to get the offset of
- the current cursor position
- the current selection range
in vim, beginning from the start of the file. I do this in python, so hints how to do it with vim’s python scripting would be very helpful.
I have used vim.current.. before for doing scripting, but it uses lines and columns rather than a general offset.
Would i have to calculate the amount of all preceding line lengths + the current row, or is there a simpler method ?
If your vim is compiled with the
+byte_offsetoption, then in a Python script after the usualimport vim, you can use, e.g.:to get the byte offset from start of file of the cursor position, and similarly for other marks. More generally, if you have a line/column pair this (assuming
+byte_offsetis how your vim was compiled with) is the way to get a byte offset (there’s also abyte2linefunction to go the other way).While the vim module does make a lot of functionality available directly to Python scripts in vim, I’ve found that
vim.evalandvim.commandare often the handiest (and sometimes the only;-) way to get in just as deep as needed;-). Oh, and I always try to have a vim compiled with +justabouteverything whenever I can;-).