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
What you can do is switch your editor find mode to regex, and put "
" (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).
[^s]
" in the find input, and then "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]
".You can use the extension Regex Text Generator
Add this to your
settings.json
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.
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
):"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.