Remove All Lines Containing String in VSCode
1 min read

Remove All Lines Containing String in VSCode

How do we delete entire lines containing a certain substring in VSCode with the Find function?

Example Scenerio in VSCode

I had a JSON file where I wanted to delete every line containing the line code where every value of code was different.

// Example json
[
  {
    "name": "United Kingdom",
    "dial_code": "+44",
    "code": "GB"
  },
  {
    "name": "United States",
    "dial_code": "+1",
    "code": "US"
  },
  {
    "name": "Uruguay",
    "dial_code": "+598",
    "code": "UY"
  },
  {
    "name": "Uzbekistan",
    "dial_code": "+998",
    "code": "UZ"
  },
  {
    "name": "Vanuatu",
    "dial_code": "+678",
    "code": "VU"
  },
  {
    "name": "Venezuela, Bolivarian Republic of Venezuela",
    "dial_code": "+58",
    "code": "VE"
  },
  {
    "name": "Vietnam",
    "dial_code": "+84",
    "code": "VN"
  }
]
International phone number codes 

Using regex and VSCode Find in File function.

  1. Open VSCode’s Search: Find in Files. (cmd+f)
  2. Click on the regular expression button [.*]
  3. Write the regular expression ^.*[STRING].*$\n, where [STRING] is your substring. It would be ^.*code.*$\n in our example scenario
  4. Replace all with an empty line.