DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
View Events Video Library
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Integrating PostgreSQL Databases with ANF: Join this workshop to learn how to create a PostgreSQL server using Instaclustr’s managed service

Mobile Database Essentials: Assess data needs, storage requirements, and more when leveraging databases for cloud and edge applications.

Monitoring and Observability for LLMs: Datadog and Google Cloud discuss how to achieve optimal AI model performance.

Automated Testing: The latest on architecture, TDD, and the benefits of AI and low-code tools.

Trending

  • Send Your Logs to Loki
  • API Design
  • The Systemic Process of Debugging
  • Deploy a Session Recording Solution Using Ansible and Audit Your Bastion Host
  1. DZone
  2. Coding
  3. Tools
  4. Vimdiff and Vim to Compare Files

Vimdiff and Vim to Compare Files

Lorna Mitchell user avatar by
Lorna Mitchell
·
Apr. 30, 15 · Interview
Like (0)
Save
Tweet
Share
6.82K Views

Join the DZone community and get the full member experience.

Join For Free

At the moment I'm working on a tricky project where two similar projects diverged. Very similar things happened to them both, but not quite the same things - and now we're merging the codebases to give us as much common code as possible. All this simply serves to set the scene of exactly what I was doing spending a whole day with large code diffs - I had to look up a few things so I thought I'd capture them while I can remember.

VimDiff

I couldn't have managed this project without a great difftool and for me that means vimdiff. It's very easy to see the changes side-by-side and to push and pull them from one pane to the other - also I'm a vim user, so the navigation doesn't require any thinking from me because this is a tool I use all the time.


My vimdiff cheatsheet only contains three items:


  • ]c Go to next block of diff
  • dp Push this version of the current block into the other pane
  • do Use the block from the other pane in this pane


Today I also had to add a couple of lines to my .vimrc to get more mileage out of my diff. At some point in time, one project's coding standards had been improved or beautified or something ... but the other project didn't have those changes which were mostly whitespace and line wrapping (actually I think the other project may have had a different set of coding standards fixes applied but I like to look forward rather than dwell on how we got into any given mess).


Using diff -w gave me more or less the right output, but vimdiff was basically panicking and showing me all lines as changed. To get around this, I asked stackoverflow what to do and found this question and answers, which led me to add the following lines to my .vimrc:

set diffopt+=iwhite
set diffexpr=""

With these lines in place, vimdiff did exactly what I needed until I had changed (deleted!) so much of one of the files that it couldn't see the similarities any more. At which point, I switched to just using vim ...

Vim To Compare Files

Vimdiff only really works if the files are quite similar, I was refactoring and ended up with one file that was quite small, but still contained sections that I needed to compare to the same sections in another file. To do this, I opened both files in vertical split panes in vim, and made them scroll together. The exact steps look something like this:


  1. Open the first file in vim
  2. Type :vsplit to get two panes side by side (tip: maximise the window on your widescreen monitor before you run this command)
  3. Jump to the second pane (Ctrl+w followed by arrow key) and then open the other file :e filename
  4. Turn on scrollbind for this pane with :set scrollbind (you can turn it off later with :set noscrollbind)
  5. Move back to the first pane again (with Ctrl+w and an arrow in the direction you want to go, if you don't use splits a lot then this can be confusing) and turn on scrollbind there too
  6. To align two things (methods in my case but it could be anything), try [line number]zt to get thing you want to the top of the screen, and do the same in the other pane also. You will need to turn off scrollbinding to point to the right place, then turn it on again.


You don't have the diff features but you can very easily get the code to line up side-by-side and then skip back and forth copying/pasting/deleting/whatever as you wish. I hadn't used the scrollbind before so I found it very handy - between all these tools and a healthy set of vim macros, I got through my day!



Vim (text editor)

Published at DZone with permission of Lorna Mitchell, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.


Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: