Wednesday, April 11, 2012

Re: Reformat code without buffer to apear "unsaved"

Timothy Madden, Wed 2012-04-11 @ 20:44:44+0300:
> Is it possible to reformat code upon opening the file (or after a
> key-press), and show the buffer as "saved" (without writing the
> formatted code back) ?

You can clear the modified flag with `:set nomodified`.

I would use BufReadPost and BufWritePost autocommands to do whatever
reformatting you want to do when reading and writing the file,
respectively. Note that, according to the documentation for the
'modified' option:

This option is not set when a change is made to the buffer as the
result of a BufNewFile, BufRead/BufReadPost, BufWritePost,
FileAppendPost or VimLeave autocommand event.

So you shouldn't need to `:set nomodified` if you're using these
autocommands; the buffer will be flagged as having been modified in the
first place.

No comments: