A way to change the code indentation.

A Question:

What is the * fastest * and easiest option if you copy a sample of code that uses two space indentations and you want to convert it to four space indentations?

Matt Staffer., Twitter

I wrote about doing this in the sublime text a few years ago. It’s not very different in VS code, I don’t think so.

But here’s another way: Use the code pane.

Step 1: Copy and paste the code onto the pane.

Say you’ve got code elsewhere, just copy and paste it:

Step 2: Adjust the code indentation settings.

If you already have a CodePan account, you’ve probably already set it up, so the default is how you like it. But if it’s not yet, adjust it in the Pen Settings area:

Showing the pan settings module that opens in the code pane via a demo.  The settings show code indentation options for spaces and tabs and indentation width.
The code was 2 spaces as copy / paste, and now we’re changing it to 4 spaces as setting.

Step 3: Format the code.

You can do it manually here:

If you save the pen and turn on the “Format on Seam” option, you will not have to use this option at all, as it will be done automatically.

It will immediately reach the new priority of this 4 space:

CodePan uses the prairie under the hat to do this. So you can do this wherever you have access to the working version of Prettier, but on the code pane it can be easier because there is no such thing as editing config or you can adjust it as you wish. Pretier also has its own playground, which is as simple as a code pane, unless you have already set your preferences on the code pane, save one step.

Leave a Reply

Your email address will not be published.