Skip to content


How to Remove Empty Lines in Text using Visual Studio

You can remove empty lines in text using Microsoft Visual Studio’s code editor by using the replace operation with regular expressions. Regular expressions are a very powerful tool that can be used to search for almost anything in text.

  1. Invoke the Replace dialog box (click Ctrl-G)
  2. Make sure "Use Regular Expressions" is checked
  3. In Find type in ^$\nHere’s what this text string means:^ (caret) – Matches at the start of the string the regex pattern is applied to. Matches a position rather than a character. Most regex flavors have an option to make the caret match after line breaks (i.e. at the start of a line in a file) as well.

    $ (dollar) – Matches at the end of the string the regex pattern is applied to. Matches a position rather than a character. Most regex flavors have an option to make the dollar match before line breaks (i.e. at the end of a line in a file) as well. Also matches before the very last line break if the string ends with a line break.

    \n – Matches an LF (line feed) character. Can be used in character classes.

  4. Leave Replace field blank.
  5. Click "Replace All".

Voilà! All empty lines will be deleted.

Posted in Programming.

Tagged with , , , , , , , , , , , , , .


2 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

  1. chraebs says

    The “search” button did find all the matches, but the replace by “nothing” (empty value) didn’t work.
    Visual Studio told “980 times replaces” but nothing actually happend. I don’t know what i’m doing wrong. I’m working on an XML and the regex i’m using is: ^\n*$

  2. Jack says

    I cant do it.. Dont understand what is wrong :(



Some HTML is OK

or, reply to this post via trackback.