tnrich
tnrich

Reputation: 8580

Delete whitespace in Visual Studio Code in arbitrary selection (aka join words) (not trim trailing whitespace)

It seems like this should be a simple question, but I can't figure out how to delete all the whitespace within an arbitrary selection in Visual Studio Code (another way of saying this is to join all characters within a selection).

Note: I'm not asking how to trim trailing whitespace although this function could be used to manually to that.

It seems like there should be a built in way to do this, but if there isn't can someone point me to an extension that will do this?

I wasn't able to find one yet.

Upvotes: 6

Views: 38554

Answers (5)

godGivesMeLanguages
godGivesMeLanguages

Reputation: 63

downloaded this beautiful extension in VS Code https://marketplace.visualstudio.com/items?itemName=bhughes339.replacerules

added to settings.json:

"replacerules.rules": {
        "joinWords0": {
            "find": "\\s",
            "replace": "",
        }
    },    
    "replacerules.rulesets": {
        "ruleset0": {
            "rules": [
                "joinWords0"
            ]
        }
    }

added to 'keybindings.json':

{
        "key": "ctrl+shift+alt+n",
        "command": "replacerules.runRule",
        "when": "editorTextFocus",
        "args": {
            "ruleName": "joinWords0"
        }
    }

now I'm able to with 1. selecting text I want to join and 2. pressing a simple "ctrl+shift+alt+n" delete whitespace in Visual Studio Code in arbitrary selection (aka join words)!!

Upvotes: 0

Mark
Mark

Reputation: 181080

Depending on your selection you might be able by simply

  1. Select the first space
  2. Ctrl+D to select the next space, and repeat for all your spaces
  3. Delete

Here is a technique that could be of more general use. Make a keybinding like so:

  {
    "key": "shift+alt+y",            // or whatever keybinding you want
    "command":  "editor.action.insertSnippet",
    "args": {
      "snippet": "${TM_SELECTED_TEXT/\\s//g}"  // replace spaces with nothing
    },
    "when": "editorTextFocus && editorHasSelection"
  },

Then select your text and trigger this command. [Or make it a snippet and trigger it through the command palette Insert Snippet command.]

This could be modified to use for many things very easily - in part because often the regex will be quite simple as it will operate within already selected text.


You can also just do a Find/Replace easily:

  1. Find: just a space

  2. Replace: with nothing

  3. Make your selection

  4. Enable the Find in Selection option - the "hamburger-like icon" to the right

Upvotes: 2

Bjorn.B
Bjorn.B

Reputation: 1705

Just adding this here for 2022. I use one of these two methods. The first I don't have to remember the Regular Expression formula:

Method 1:
Sometimes this method will strip all spaces in your code (which isn't intended). If that occurs, try the next method. I often try this method first.

  • Select an empty space, and push CTRL + F, or Apple key + F on mac. This will highlight all matching characters, spaces.
  • Push the Replace All button to replace them all with nothing.
  • Hit CTRL + SHIFT + P' or Apple key + Shift + P', and select Format Document.

Method 2:
Found here: https://www.trainingdragon.co.uk/blog/how-to-remove-empty-lines-in-visual-studio-code/

  • Push CTRL + F, or Apple key + F on mac. Select the Use Regular Expression button, or push Apple key + option + R to toggle it on/off.
  • Add ^(\s)*$\n to the find input box.
  • Push the Replace All button to replace them all.

Upvotes: 4

Tom
Tom

Reputation: 17854

You can do it fairly easily without an extension.

  • Copy the selection
  • Ctrl + N (opens new unnamed blank document)
  • Paste
  • Ctrl + H (replace dialog), space, Ctrl + Alt + Enter (replace-all)

Now copy the update string to where you want it, and close the document.

Upvotes: 10

tnrich
tnrich

Reputation: 8580

Here's an extension (remove-whitespace-aka-join-words) I built to do just this:

https://marketplace.visualstudio.com/items?itemName=tnrich.remove-whitespace-aka-join-words

Upvotes: 8

Related Questions