Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

10.4: Edit binary (and normal) plist files with vim UNIX
Tiger only hintIf 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:
  • 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.
Any suggestions for improvements are welcome, this is the first time I do anything other than simple changes to my .vimrc file.
    •    
  • Currently 3.67 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (3 votes cast)
 
[30,115 views]  

10.4: Edit binary (and normal) plist files with vim | 10 comments | Create New Account
Click here to return to the '10.4: Edit binary (and normal) plist files with vim' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
10.4: Edit binary (and normal) plist files with vim
Authored by: wgscott on Aug 10, '05 11:33:29AM

This is fantastic. I just stuck it at the bottom of my .vimrc file and it works great. Thanks VERY VERY much for making this public.



[ Reply to This | # ]
Thanks!
Authored by: forman on Aug 10, '05 12:21:57PM
It's like you're reading my mind. The script works as promised and it's much appreciated.

Michael.

[ Reply to This | # ]
typo in alternative method
Authored by: moritzh on Aug 10, '05 01:34:18PM

I just see that the code for the alternative method I mention has been mangled (I guess because it contains < and >). So replace:

autocmd BufWritePost,FileWritePost *.plist !plutil -convert binary1
with
autocmd BufWritePost,FileWritePost *.plist !plutil -convert binary1 <afile>
(that's literal). Just in case somebody want to try that out.

P.S.: @ Rob: There is something annoying goind with the comment editing:
If I enter something as <code>&lt;</code> to get the less than sign < in yellow code style and press preview, the result is shown OK.
BUT: In the editing window, this less sign just entered is transformed from a &lt; to <. So if you edit some more and the submit, things like a less than sign are not properly encoded as HTML entities anymore.
So basically every time you press preview, you need to re-encode the HTML entities so they show up OK when finally submitting. I experienced this while composing this comment and this must have been the cause for the omission.

Note: This applies only to the inline <code>, not to the special

</code> (which starts a new paragraph so is not always what you want). This behavior might be standard-compliant, but it sure is annoying and dangerous when editing lenghty posts. So anyone else, be warned.
</p>
<p>P.P.S.: You can imagine getting something like <code><code>&lt;</code></code> inthis comment isn't so easy... :)</code>


[ Reply to This | # ]
typo in alternative method
Authored by: moritzh on Aug 10, '05 01:42:30PM
Argh, now I hit the submit instead of the preview button by accident...
Anywat, you get the point I think... The last part should've been:

Note: This applies only to the inline <code>, not to the special [code] (which starts a new paragraph so is not always what you want). This behavior might be standard-compliant, but it sure is annoying and dangerous when editing lenghty posts. So anyone else, be warned.

P.P.S.: You can imagine getting something like <code>&lt;</code> in this comment isn't so easy... :)

[ Reply to This | # ]

Another Salvo in the vi/emacs War!
Authored by: EatingPie on Aug 10, '05 02:07:49PM

Crap! You beat the Emacs guys' attempts at getting this right! (Emacs ones don't memorize the original binary/text format.)

Okay. You win this one... But watch your back!

:Pie

---
-Pie


[ Reply to This | # ]

10.4: Edit binary (and normal) plist files with vim
Authored by: gxw on Aug 10, '05 09:06:21PM

Cool hint!
I don't seem to have a .vimrc file on my system (running Tiger).
Will this still work if I add the script to the .viminfo file (located in home folder)?

PS
.viminfo gets a current timestamp when I run vi.



[ Reply to This | # ]
10.4: Edit binary (and normal) plist files with vim
Authored by: geohar on Aug 11, '05 02:36:48AM
I think ~/.viminfo stores recent files and line positions, that kind of thing. There is no ~/.vimrc file unless you create one, but just go ahead and create that file. The same file will be read whether you use fink's vim or the apple provided one.

[ Reply to This | # ]
10.4: Edit binary (and normal) plist files with vim
Authored by: moritzh on Aug 11, '05 09:08:09AM

geohar is right. Don't add it to ~/.viminfo as this only memorizes in which line you were for each file, what recent searched you have perfomed etc.

~/.vimrc (vim run commands) is the file vim scans each time you open vim to edit a file. There is none by default, but you can create one. Either just start with an empty file or you can copy the file /usr/share/vim/vim62/vimrc_example.vim and append the script from the hint at the bottom. That way, you also get some nice features that are not enabled by default.



[ Reply to This | # ]
I don't have /dev/stdin and /dev/stdout
Authored by: ajwitte on Aug 18, '05 09:31:54PM

I'm running 10.4.2 (fresh install) and I don't have the devices /dev/stdin and /dev/stdout. I'd mknod them, but I'm not sure how, and I don't think that would persist across a reboot. Is there any way to get the system to provide those devices, or is there an alternative?



[ Reply to This | # ]
10.4: Edit binary (and normal) plist files with vim
Authored by: bananaoomarang on Jun 19, '12 09:15:17AM

For anyone who's getting the plutil help message instead of a file, just replace the lines:


'[,']!plutil -convert xml1 /dev/stdin -o /dev/stdout

and


silent '[,']!plutil -convert binary1 /dev/stdin -o /dev/stdout


with:


'[,']!plutil -convert xml1 -i /dev/stdin -o /dev/stdout

and

silent '[,']!plutil -convert binary1 -i /dev/stdin -o /dev/stdout



[ Reply to This | # ]