Search code examples
ideeditorsublimetext2sublimetext

How do I ignore long lines in Sublime Text when searching


I often use the search feature in Sublime Text to search folders - some of these folders contain extremely long lines (1000's of chars long) due to minified code, base64 encoded data or certificates.

Search results often come back looking like the following (which is both annoying visually, as well as giving me accidental matches on shorter strings):

Annoying find result

Is there a way I can omit lines greater than a certain length from my search results?


Solution

  • You can achieve what you are after (to a degree) using negative look ahead and negative look behind syntax.

    Just suppose you didn't want to include any lines over 50 characters long where you are searching word MATCH, the following regex would work (?<!.{46})MATCH(?!.{46})

    Scenarios (first 4 lines produce a match, the following 3 do not)

    xxxxxxxxxxxxxMATCHxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx50
    MATCHxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx50
    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxMATCH
    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxMATCHxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx95
    
    MATCHxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx51
    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxMATCH
    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxMATCHxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx96
    

    In this example the longest line included in the returned result set will be of length 95 characters as there are 45 before and after the word MATCH. so not perfect but close, and for your purpose I'm sure it will do to exclude those super long lines of encoded data.

    You can tune the length of the look behind and look ahead to get the result you are after.