2

I have the file1.txt with this content

Once upon a time, there was a girl named Persephone.
She had black hair.
She loved her mother more than anything.
She liked to sit outside in the sunshine with her cat, Daisy.
She dreamed of being a painter when she grew up.

And the file2.txt with this content

Once upon a time, there was a girl named Persephone.
She had red hair.
She loved chocolate chip cookies more than anything.
She liked to sit outside in the sunshine with her cat, Daisy.
She would look up into the clouds and dream of being a world-famous baker.

So, I generated a patch file with diff -u file1.txt file2.txt > file_unified.patch, which resulted on

--- file1   2021-03-16 23:26:09.702013804 +0000
+++ file2   2021-03-16 23:26:09.702361860 +0000
@@ -1,5 +1,5 @@
 Once upon a time, there was a girl named Persephone.
-She had black hair.
-She loved her mother more than anything.
+She had red hair.
+She loved chocolate chip cookies more than anything.
 She liked to sit outside in the sunshine with her cat, Daisy.
-She dreamed of being a painter when she grew up.
\ No newline at end of file
+She would look up into the clouds and dream of being a world-famous baker.
\ No newline at end of file

I know I can open two different files by using vim -Od file1 file2, but by chance, is there any way of opening a single patch file as the file_unified.patch and get the vim to open it as if it was two different files (as I would get if I do vim -Od file1 file2)?

2

You could do something like vimdiff file1.txt <( patch -s -o - file1.txt file_unified.patch ) (assuming your shell is bash and your diff is GNU). The patch command applies the patch to the original file and sends the output to stdout, and the <( ... ) redirection captures that output and provides a filename that can be used as the second argument to vimdiff.

You can also open the original file in vim and then use the :diffpatch command to apply the patch and open the result in a split, with diff mode enabled.

0

is there any way of opening a single patch file as the file_unified.patch and get the vim to open it as if it was two different files (as I would get if I do vim -Od file1 file2)?

  1. file_unified.patch is open.
  2. Create vertical split by pressing Ctrl-Wv
  3. Move cursor to --- file1 and press gf Now file1 should appear in the left split.
  4. Enter command: :diffthis
  5. Press Ctrl-Ww to go to the right split.
  6. Move cursor to +++ file2 and press gf Now file2 should appear in the right split.
  7. Enter command: :diffthis
  8. ???????
  9. PROFIT

P.S. If you have :set splitright then do (2) and (5) in order reverse.

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.