1

When comparing a buffer to it's on disk file, I regularly do (omitting the diffthis):

:botright vnew
:r ++edit #

According to https://vimhelp.org/insert.txt.html#%3Aread the option ++edit should set the options of fileformat and fileencoding for the buffer to what regularly :editing file would give, unless I interpret the documentation wrong. Problem is, that none of the options of fileformat, fileencoding , ... get set for the :vnew buffer.

What am I doing wrong?

Edit (what I am expecting): Assume I am editing a file minimal.yaml. What I get with :e minimal.yaml is syntax highlighting for that file (could be json, whatever file format).

If another process changes that file on disk and I revisit that file in a vnew | r ++edit # buffer, that new buffer has no fileformat set, while I expect it to be of type yaml.

1
  • What fileformat, fileencoding are you getting and which were you expecting? Can you suggest a minimal reproducible example to illustrate the issue you're having?
    – filbranden
    Mar 2 '20 at 9:14
2

You're confusing fileformat (dos or unix) and fileencoding (utf-8, iso-8859-1) with filetype (yaml, c, python, tex, etc.)

With ++edit, the :read command will set the former two (so you can read a Latin-1 file with CRLF line endings), but it won't trigger filetype detection.

It's hard to do so, since a lot of filetype detection depends on the file name and file extension and Vim will look at the buffer file name to get that information.

Assuming the purpose of using :read is to prevent modifications to the original file, consider just opening the file with :edit or :view (so all the filetype logic is triggered) and then setting options to prevent that buffer from ever saving back to the original file again.

See, for example, :help special-buffers, which tells you how to configure a scratch buffer:

scratch: Contains text that can be discarded at any time. It is kept when closing the window, it must be deleted explicitly. Settings:

    :setlocal buftype=nofile
    :setlocal bufhidden=hide
    :setlocal noswapfile

The buffer name can be used to identify the buffer, if you give it a meaningful name.

Another approach to consider (possibly to combine with some of the scratch buffer support) is to set the buffer filename using the :file command (see :help :file_f), which you can use to tell Vim whenever the buffer is to be saved, you want it to try to write it to the file you specify instead.

Using some of these settings together can help you get the desired effect on the buffer you're creating.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.