I am scripting a diff program between two longs paths and want one file to be read-only and the other to be read-write. e.g. I want to run something like srcdiff test.c and my script will expand that to vimdiff test.c /long/path/to/other/test.c I would like /long/path/to/other/test.c to be read-only and test.c to be read-write. I can accomplish this by doing :set ro however I would have to do this every time I open a file. I tried vimdiff test.c -R /long/path/to/other/test.c but that opened both files as read-only. Is it possible to accomplish this?

I am using version VIM - Vi IMproved 6.2 (2003 Jun 1, compiled Sep 16 2003 11:00:10) on EL3 so updates are not exactly possible.


You can use startup commands to do this. From :h -c

+{command}                      *-+c* *-c*
-c {command}    {command} will be executed after the first file has been
        read (and after autocommands and modelines for that file have
        been processed).  "command" is interpreted as an Ex command.
        If the "command" contains spaces, it must be enclosed in
        double quotes (this depends on the shell that is used).
        Example: >

So you could do something like

vimdiff file1 file2 -c "wincmd l" -c "se ro"

What this does is essentially running :wincmd l (equivalent to <C-w>l, i.e. move one split to the right) to move to your second file, and then running :se ro on this secondary file.

| improve this answer | |
  • Awesome! Exactly what I was looking for. I added an extra wincmd l after the se ro so that it puts them back in the right order. Thank you! – Zachary Brady Nov 2 '16 at 17:09

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.