Navigating and Editing in vimdiff
Now that you've seen how vimdiff
displays file differences, let's learn how to navigate between differences and make edits.
Open the files again with vimdiff
:
vimdiff file1.txt file2.txt
Navigating Between Differences
In vimdiff
, you can use the following commands to move between differences:
]c
- Jump to the next difference
[c
- Jump to the previous difference
Try navigating to the difference in our files by typing ]c
in normal mode (press Escape first if you're not in normal mode).
Copying Text Between Files
One of the powerful features of vimdiff
is the ability to copy text from one file to another. You can do this with the following commands:
do
(diff obtain) - Get the change from the other file to the current file
dp
(diff put) - Put the change from the current file to the other file
Try positioning your cursor on the difference in the left file and type do
to get the text from the right file. Then, try positioning your cursor on the right file and type dp
to put the text from the right file to the left.
Making Direct Edits
You can also edit files directly in vimdiff
just like you would in regular Vim:
- Press
i
to enter insert mode
- Make your changes
- Press Escape to return to normal mode
- Type
:w
to save changes
Exiting vimdiff
When you're done exploring vimdiff
, exit without saving changes:
:qa!
If you want to save changes before exiting, use:
:wq
for each file, or use:
:wqa
to save and exit all files at once.
vimdiff
is a powerful tool that combines the capabilities of Vim with file comparison features, making it excellent for code reviews, troubleshooting, and merging changes.