I don’t think there is any built in functionality for doing something like that, unfortunately.What you want is a plugin that iterates over the second file and removes all lines that match in the first file. I did a bit of poking around but I didn’t find any existing plugin that seems to fill that particular hole (though I may well be wrong).
Now, with that said, depending on what you’re trying to do, you might be able to use sublime’s ability to find and remove duplicate lines in your file to do this. There are some caveats though:
- It will only match things exactly, so things like differences in text case or in white space (even extra white space at the end of a line) will make two lines not match each other, which may be undesirable if you don’t have the freedom to manipulate the source data to match better (e.g. by making it all lower case)
- If the first file contains any lines that are duplicates of each other, all of the duplicates will be removed leaving only the first one behind, even if they don’t appear in the second file.
As such, this is very much a technique for certain specific situations. Assuming you know that the above items (particularly #2) aren’t of any concern, you can try something like this:
First, you want to create a new empty file and copy and paste the contents of file #2 into it. Next, add a line that you know will not appear in either of the two files that you are working with (e.g. “===================”). Lastly, copy and paste the contents of file #1 at the bottom.
For your example above, your temporary buffer would then look like this:
2
===================
1
2
3
Now use Edit > Permute Lines > Unique
from the menu (you can also get it via the command palette). This tells sublime to search for and delete all lines that are duplicates of another line. In particular, the first time a line is found, it’s kept and all of the other copies of it are removed.
At this point, your temporary buffer would look like this:
2
===================
1
3
What’s happened is that sublime checks from the top down and removes all duplicate lines that it finds, leaving the first instance of each line behind. By putting the list of lines we want removed on the top, we ensure that it’s the one that remains if any duplicates are found; thus anything that appears in the second file is removed from the first file. The separator is just there to visually show you where your results start (and it has to be unique for obvious reasons).
As mentioned above, this has the unfortunate side effect of removing any duplicate lines in the first file even if they don’t appear in the second file, which may or may not be an issue. Otherwise I would suggest the plugin route, although that would require some coding.