Automating Line Break Removal
And some white space while we're at it too.
I like to write small utilities to make my life easier (mostly forwork). My job often entails tasks that are repetitive or at least periodically recurring.
One such task is adding a machineKey
tag to the web.config
file of the web applications installations we deploy.
While there are a number of ways to generate a machineKey, I get these machine keys from the Developer Fusion Machine Key Generator site.
The Machine Key generator makes life easy by giving you a key, however the way it is output is rather cumbersome: It outputs the code over 11 lines, which are comprised of empty white-space lines and loads of space characters.
I like to keep a clean and tidy web.config file, and because the machineKey
should hardly be touched, prefer to keep it all on a single line.
After having enough of ctrl-shift-arrow-delete/backspace-ing to tidy this up, I wrote a handy utility to do it for me.
To use it, I feed the code from Developer Fusion into the textbox, click the button and then copy out the cleansed result.
You can see how I built the tool below.
HTML
<textarea id="txtData" cols="70" rows="7"></textarea>
<br />
<input type="button" onclick="FixData()" value="Un-Linebreak" />
JavaScript
function FixData() {
var ctrl = document.querySelector('#txtData');
var txt = ctrl.value;
txt = txt.replace(/[\r\n+]/g, " ");
txt = txt.replace(/\s\s+/g, " ");
ctrl.value = txt;
}
Result
Silvrback doesnt currently allow embedding of the result from Codepen, but you can [see the working result here:
So there you go, an easy way to get rid of line breaks and excess white space, in a single click (excluding the clicks to load it up).
I have the link to the working utility pinned to my Chrome bookmark bar for quick access.
While the tool might not seem like that much of a time saving, it does actually save me a dozen seconds or so per application I work on, (1.5 x websites/week) x (12 seconds x 52 weeks)
= about 15 minutes per year that I have just saved myself- and now you.
Optimising the small things in life, for developers and people.