By josh / July 30, 2013

Quick text cleanup with Sublime’s new Invert Selection command

Sublime Text 3 introduces the new Invert Selection command, and by combining it with multi-select or regular expressions and a little creativity, you can quickly accomplish tasks like zapping unwanted lines in a data file or stripping HTML tags from a block of text.

Invert Selection is useful in scenarios where you want to delete more text than you want to keep, or where it’s just simpler to select the text you’d like to retain.

As its name implies, the command will deselect any text that was highlighted and simultaneously select the text that was not highlighted.

Let’s look at a few scenarios where Invert Selection might be handy.

Clean up CSV data

Even with Sublime’s multi-select, it’s often a pain to select text for deletion. To extract first names from the following CSV list, you could use multi-select to carefully highlight the last name and comma in each row:

Knope,Leslie
Swanson,Ron
Haverford,Tom
Perkins,Ann
Ludgate,April

But a simpler alternative is to highlight all the first names by double clicking them while holding down Ctrl (Windows and Linux) or Command (OS X). Then click Selection | Invert Selection to highlight the unwanted text for deletion.

Filter unwanted lines

By combining Find and Invert Selection, you can easily filter out lines that don’t match a pattern, such as deleting everyone not named Josh in the following list:

Knope,Leslie
Earl,Josh
Swanson,Ron
Haverford,Tom
Perkins,Ann
Turner,Josh
Ludgate,April

Open the Find panel and enable the Regular expression option, then enter the following regular expression, which matches all lines containing the word Josh:

^.*Josh

Click Find All to highlight the two matching lines. Run Invert Selection, then click Edit | Text | Delete Line to eliminate the non-matching lines.

Strip HTML tags

With another regex, Invert Selection can remove HTML tags from a block of text. Let’s clean up a section of the Sublime release notes:

<span style="font-size: 10pt">Release Date: 8 July 2013</span>
    <ul>
  <li>
    Removed expiry date
  </li>

  <li>
    Backported various fixed from Sublime Text 3
  </li>

  <li>
    Improved minimap click behavior. The old behavior is available via the <tt>minimap_scroll_to_clicked_text</tt> setting
  </li>

  <li>
    Added <tt>copy_with_empty_selection</tt> setting, to control the behavior of the copy and cut commands when no text is selected
  </li>

</ul>

Launch Find and make sure the Regular expression option is enabled. Enter the following regex, which matches text that’s wrapped with HTML tags:

(?&lt;=>)(.+?)(?=&lt;)

Click Find All to select the text between the tags. Then run Invert Selection, highlighting the tags for deletion.

Now that your text is all fresh and clean, sign up for the Sublime Text Tips newsletter to learn what else you can do with it:

http://sublimetexttips.com/newsletter

About the author

josh

1comment
Ted Cowan - October 5, 2016

Josh,

I am new to Sublime Text, and I needed to delete all lines that did not contain a specific string. Your “Josh” example did not work for me using Sublime build 3126 on an iMac running El Capitan.

What did work for me was to use a regular expression that matched the entire line containing my specific string, including the \n. Then the Invert Selection selected everything except the lines I wanted to keep. I then hit the DELETE key, which deleted the unwanted lines. Using EDIT>TEXT>DELETE LINE deleted everything in the file, which is not exactly what I was expecting.

Thanks for the great pointers and a very informative post. Sublime is a truly great product. I am demo-ing it on a Mac.

Reply
Click here to add a comment

Leave a comment: