Reputation: 8580
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
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
Reputation: 181080
Depending on your selection you might be able by simply
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:
Find:
just a space
Replace: with nothing
Make your selection
Enable the Find in Selection
option - the "hamburger-like icon" to the right
Upvotes: 2
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.
CTRL + F
, or Apple key + F
on mac. This will highlight all matching characters, spaces.Replace All
button to replace them all with nothing.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/
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.^(\s)*$\n
to the find input box.Replace All
button to replace them all.Upvotes: 4
Reputation: 17854
You can do it fairly easily without an extension.
Now copy the update string to where you want it, and close the document.
Upvotes: 10
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