Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the structure of a vim .swp file?

Tags:

python

vim

An application wants to shell text out to vim and know what edits are being made, in real time.

The .swp file provides this information. Can anyone provide guidance on how to read this binary file, say, with Python?

like image 452
ktenney Avatar asked Oct 27 '25 08:10

ktenney


2 Answers

I did my own investigation into .swp file formats when I wrote my ls.swp tool that views .swp contents (that's a perl script with lots of documentation in it).

My key sources of interest were:

  • A Vim General mailing list thread on vim swap file format
  • Official Vim documentation on swap file recovery

Parsing this required a bit of reverse-engineering, and I was only able to extract certain data. All I wanted to get out of it was some basic information about the status of the edited file. My results look like this:

$ ls.swp .ls.swp.sw*
.ls.swp.swo:adamhotep@tabasco:~/git/misc-scripts/ls.swp (running) 85d ago
.ls.swp.swp:adamhotep@tabasco:~/git/misc-scripts/ls.swp 85d ago

I'm rather certain you're not going to get any real-time data since that'd just be a big pile of disk I/O for vim to maintain.

like image 142
Adam Katz Avatar answered Oct 28 '25 22:10

Adam Katz


I wouldn't rely on the swapfile contents to get real-time updates. Its format is geared towards Vim's uses, and its format isn't documented other than by its implementation. You would have to duplicate large parts of the algorithm, and maintain that whenever the internal format changes (without prior notice).

Alternatively, I would use one of the embedded languages (e.g. Python) to interface with the outside program that wants to get real-time updates. A Python function could periodically send along the entire buffer contents on a socket, for example.

like image 40
Ingo Karkat Avatar answered Oct 28 '25 20:10

Ingo Karkat



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!