skip to Main Content

I’m doing a lot of text editing right now that involves replacing spans of text with spaces (same number of spaces as number of replaced characters). It would be easier and more efficient if I could somehow highlight/select the text to replace, and then replace it all with blank spaces instead of first deleting the text and then manually refilling the spaces, tabs or newlines, especially during moments where I want to be precise.

For example:

This is my example sentence.

I decide I want to select ‘my example’ and delete it, like this:

This is            sentence.

Instead of having it go like this:

This is sentence.

Is there an easier way to do this?

3

Answers


  1. What you can do is switch your editor find mode to regex, and put "[^s]" in the find input, and then " " (space) in the replace input. Then select the text you want to replace with spaces and either click the "Find in Selection" button or use the keyboard shortcut (Ex. alt+l). Then click "Replace All" or use the keyboard shortcut (Ex. ctrl+alt+enter).

    This will replace each instance of any non-whitespace character in the selection with a space character.

    If you don’t want to preserve the types of whitespace characters (Ex. keeping tab characters as tab characters), just put "." in the find input field instead of "[^s]".

    Login or Signup to reply.
  2. You can use the extension Regex Text Generator

    Add this to your settings.json

      "regexTextGen.predefined": {
        "Replace with spaces": {
          "originalTextRegex": "(?g)(.)",
          "generatorRegex": " {S}"
        }
      }
    
    • Select the text you want to replace, multi cursor is supported.
    • Execute command: Generate text based on Regular Expression (regex)
    • Select the predefined: Replace with spaces
    • Press Enter 2 times.

    You are able to preview the replacement and Escape if needed when you are allowed to edit the generator expression.

    You can create a key binding for this if you need to do it a lot. See the extension page for an example.

    Login or Signup to reply.
  3. This is also easy with this extension, Find and Transform (disclaimer: I wrote it). Here is a keybinding that would work (in your keybindings.json):

    {
    "key": "alt+q",         // whatever keybinding you want
       "command": "findInCurrentFile",
       "args": {
        "replace": [ "$${ return `$1`.replace(/./g, ' '); }$$" ],
        "isRegex": true,
        "restrictFind": "selections",
        "postCommands": "cancelSelection"     // if you had multiple selections and wanted to clear them
      }
    }
    

    "restrictFind": "selections" will only work within selections, as many as you want. If you had repetitive text in the document, you could omit this argument and it will find the selected text wherever it occurs in the document and replace it.

    The $1 referred to is the selected text.

    replace selected text with spaces

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search