10.4: Edit binary (and normal) plist files with vim
Aug 10, '05 09:34:00AM
Contributed by: moritzh
If you are like me and hate to use Apple's Property List Editor program with its limited capabilites to edit plist files (no searching, always starting with a view with all contents hidden etc.) and prefer to use vim instead for text editing, this one is for you. Using this vim script I created, you can use vim to automatically handle the plist files stored in binary format (new in 10.4). Place the script at the bottom in your .vimrc file. Note that this hint is obviously inspired by some other hints on the same topic but when using emacs.
What does it do? It reads a plist file, checks and memorizes if it is binary, converts it to plain text if necessary and turns on syntax highlinghting for XML. When you save the file again, it is converted to binary again for writing if it was binary in the beginning. If the file was not binary or if the conversion to binary fails (which can happen if you have syntax errors in your file), the file is stored in text format. If an originally-binary plist is saved as plain text due to syntax errors and you fix the errors and save the file again, it is written in binary format. If you open a binary file and just view it without making any changes, you can still exit from vim using the normal :q (no need for :q!) and the file is left untouched (still in binary format). All this should work even when working with several buffers (files), when reading a file into an existing buffer, or when only writing parts of a buffer to file.
Some things to note:
Any suggestions for improvements are welcome, this is the first time I do anything other than simple changes to my .vimrc file.
- The file content is examined in vim and the result from the external conversion using plutil from the Apple Developer Tools is inserted in vim. Using this approach, there is no need for an external shell script which converts a plist file and then hands it off to vim.
- Looking at the conversion commands suggests vim is using pipes, but it actually uses temporary files for the external filtering. Not as elegant, but at least you don't need to create/delete the temp files yourself.
- As has been pointed out in the discussions on those hints: There is no need to save an originally-binary plist in binary format after editing, and some plain text plist files (for startup deamons) must not be saved as binary plist files. But since many people seemed to want to save in binary format anyway, I included the functionality with the needed checks.
- An alternative approach for converting back to binary would be to let plutil act externally on the file after it has been saved in text format (autocmd BufWritePost,FileWritePost *.plist !plutil -convert binary1). The problem with this approach is that vim sees that the timestamp of the file has changed and warns you about it, which is annoying. Well, it does so most of the time. Sometimes, when the conversion happens in the same second as the writing, the time stamp is not changed, so there wil be no warning. At least I think that's the reason; the help files in vim are a bit contradictory about what mechanism is really used to detect if a file changed externally or not.) I tried several ways of disabling this warning, but the behavior was never really satisfying. If you have suggestions, post a comment. (Please do try your method yourself; there are many things which you realize don't work as intended only after using them for a while. Well, that was the case for me at least.)
- It seems to me that the autocommands get executed in any case (e.g. for BufWritePost), even if your file was really modified externally and you answer no to vim's question if you want to save anyway. So in some really rare cases, this might have some side effects.
Mac OS X Hints