Is there an easy way to add spaces to multiple lines so that each line has equal amount of characters?
Motivation, say I want to add ‘*’ at end of each line
/***************************
* This is line 1. *
* Line2. *
* May be line3. *
* For sure this is line 4. *
****************************/
Without using any custom format program, is there a way in VS code to add different number of spaces just before the last ‘*’ like so:
/***************************
* This is line 1. *
* Line2 *
* May be line3 *
* For sure this is line 4. *
****************************/
2
Answers
*
at the lines that are incorrect*
‘s beyond the correct positionEsc
to get out of multi cursor*
‘sCtrl+Delete
Esc
to get out of multi cursorYou can do this fairly simply with an extension I wrote, Find and Transform, because it allows you to use javascript or the vscode extension api in a replacement. So starting with this text:
you would make this keybinding (in your
keybindings.json
):So the replacement is straightforward javascript which gets the selected text, splits it on the newlines and then uses the length of the first line to pad the other lines with spaces and then adds a
*
to the end of each line. It does nothing to the first or last line of the selection.Demo:
You can do multiple selections at a time too.