Vimdiff - How to hide all same lines in both files and show only different ones - vim

vimdiff file1 file2
besides differences shows also same lines from both files. Is it possible to hide them? How to do it?

As Vim is a text editor (not a specialized diff viewer), the full contents of both files are kept (so you can do edits and persist them). Vim usually just "hides" multiple identical lines by folding them away; they are still present, just not visible.
You can influence how many identical lines are kept around changes (default: 6 lines above and below) via the context value of the 'diffopt' option. So, to completely fold all identical lines:
:set diffopt+=context:0

It is still showing common lines if common line is going in sequence with different. one after another

The solution suggested by Ingo Karkat worked perfectly in Linux. Thanks for sharing it. However, it's not working on Mac.

Related

Text showing misaligned in Vim vs Cat

I've been using vim for a few weeks, and so far I've downloaded a few plugins including airline, nerdtree, and a colorscheme. When I downloaded some files with wget, I noticed that they weren't properly aligned. Text in the code files are normally supposed to be aligned in columns, and there generally shjouldn't be issues with indentation. As an example, when I try running the command cat filename.extension, I get this:
This is how the file is supposed to look. Everything is aligned neatly into columns without any issues. However, if I try editing the file in Vim, it instead looks like this:
The text for whatever reason is not aligned properly, and so far I am not sure how to resolve it...
The file appears to be using tabulations for indentation and alignment.
The de-facto standard width of a tabulation is 8 characters.
cat respects that "standard" to the letter, without the possibility of changing the tabulation width.
Vim also respects it by default but it allows the user to change the tabulation width and you tell it to use a tabulation width of 4 characters.
The file looks different because the two programs have different tabulation settings.
Therefore, if you want the file to look the same in the two programs, use the same tabulation settings. Since they can't be changed in cat, you will have to revert Vim's to their default values.
You didn't show us your vimrc so we don't know exactly what you did and we can't really tell you how to go about it.

Describe vim replacements

In almost every copy of vim that I have used, the program will give a description of changes after replacing text. (For example, something like "92 substitutions on 20 lines" would be displayed.)
I'm now working with a copy of vim that does not do that by default.
Is there a simple command (or addition I can make to my vimrc file) that will enable this behavior?
It's governed by option report.
You can see you current setting with
set report?
To report even the minimal change
set report=0
I think you experience the effects of the 'report' option. If the (substitution, or any other command's) changes cover more than those (default 2), you'll see the message, else nothing.
So, you can put the following into your ~/.vimrc to always see those messages:
set report=0
Although not exactly your question,
in vim's substitutions, you can use the "n" flag to count the number of
matches and lines (without real substitutions).
Example
:%s/a//gn
55311 matches on 17459 lines

Give vimdiff some hints

I've got two c++ files that I want to diff with vimdiff. One of them has a lot more function definitions at the start, before both have a common function that I'm actually interested in. However, vimdiff seems incapable to ignore all the function defs before the common one (perhaps because of different arguments).
Is there any way I can give a hint to vimdiff that, say, line xxx in file1.cxx is equals to line yyy in file2.cxx?
I'm open for alternative solutions without vimdiff, but they must be on linux and very preferably command line, since I'm ssh-ing and any graphical interface is a bit uncomfortable.
Vim just delegates the actual work of comparing the files to the external diff utility, cp. :help diff-diffexpr. The help page also shows how a different utility can be used. Unfortunately, I'm not aware of any more "intelligent" or configurable diff tool that would help in your situation.
A workaround might be (temporarily) removing the excess functions that you're not interested in, anyway. With the BlockDiff plugin, you don't actually need to modify the files. Just select the interesting lines in both windows and execute :[range]BlockDiff on them. Only those sections will then be diffed in a separate tab page. (The plugin mentions this requires a GUI, but Vim in a terminal supports tab pages just as well.)

Is there a way to adjust vimdiff's line position for a change?

I have fixed some indentation issues in my project and I'm looking at the vimdiff output for the before and after. I notice that vimdiff seems to be very confused as to what the actual changes are, rendering a pretty much useless output in this case:
For example, it seems to think that the very first line is a newly added line:
<div class="text-xs-center p-4">
In reality, all that has changed is the indentation. Vimdiff is not recognizing the changes properly.
In another, similar file, the diff works much better:
I think the difference is that in the second file I did not remove the first line break.
Is there a way to manually fix this sort of thing, so that the diff shows properly? I don't want to change either file, the changes are correct. But I'd like to tell vimdiff that it's comparing the wrong lines to one another.
Is this possible?
The underlying diff tool compares individual lines, regardless of whether "only" indentation changed, or something more fundamental. As in your first case, there's one extra unindented line, so diff recognizes that as unchanged, and this messes up the entire diff.
If you want to ensure that only indentation got changed, you can ignore whitespace changes via
:set diffopt+=iwhite
Then, the diff should show no changes at all (or, in your first example, only the added line 5).
Maybe there are also other diff utilities that more intelligently handle these cases. If you find such tool, you can configure Vim to use it via 'diffexpr'.

Editing multiple files simultaneously with Vim

I need to add several lines all at the same location to multiple files. I was wondering if I could possibly open all files with Vim, and only make the changes in one file for which the changes will be made in all files simultaneously. I really want to avoid opening X number of files, copying this, pasting, then repeating for each file of X files...There's gotta be a better way to do this, hopefully with vim...
Thanks!
Amit
You could record macro and execute it on other files. See http://www.thegeekstuff.com/2009/01/vi-and-vim-macro-tutorial-how-to-record-and-play/ for detailed tutorial.
You can use the windo command to operate in all windows. Combine this with a substitute command and you have this (say you want to add "This is a new line." at line 2 in every file):
:windo 2s/\(.*\)/This is a new line.^M\1
Off course, as others noted, there are much better tools for this job (awk comes to mind).

Resources