Changing Between Spaces and Tabs in Sublime Text
BY:WMK-IT
A question:
If you copy a code sample that uses two-space indentation and you want to convert it to four-space indentation, what’s the *fastest* and easiest option?
wmk-tech, Twitter
Sublime Text is pretty dang good at making it easy to switch between using tabs and spaces to indent your code. More importantly, it makes it easy to adjust the indentation of code that doesn’t match your preference. I thought I’d put this together for reference, as there is a particular sequence of steps for some of the transitions that needs to be followed.
The first step is making sure your User Preferences are how you like them.
I prefer spaces, so my settings are like this:
Adjust to your liking there.
You can override these settings on any given file, from the bottom right.
This is also headquarters for fixing up a document that isn’t how you like it.
Sublime Text is pretty dang good at making it easy to switch between using tabs and spaces to indent your code. More importantly, it makes it easy to adjust the indentation of code that doesn’t match your preference. I thought I’d put this together for reference, as there is a particular sequence of steps for some of the transitions that needs to be followed.
The first step is making sure your User Preferences are how you like them.
I prefer spaces, so my settings are like this:
Adjust to your liking there.
You can override these settings on any given file, from the bottom right.
This is also headquarters for fixing up a document that isn’t how you like it.
Changing from Tabs to Spaces (Same Level of Indentation)
Here’s a document that is in tabs right now. I can tell because I’ve selected the text and there are dashes in the white space not dots. Dashes are tabs, dots are spaces. Also, in the bottom right, I can see it says “Tabs Size: 2”.
You can see that those Tabs are 2 spaces wide. Assuming I want to switch to spaces and I’m happy with 2 spaces per tab, I just select “Convert Indentation To Spaces” from that menu.
And I’m good to go for that case.
Here’s a document that is in tabs right now. I can tell because I’ve selected the text and there are dashes in the white space not dots. Dashes are tabs, dots are spaces. Also, in the bottom right, I can see it says “Tabs Size: 2”.
You can see that those Tabs are 2 spaces wide. Assuming I want to switch to spaces and I’m happy with 2 spaces per tab, I just select “Convert Indentation To Spaces” from that menu.
And I’m good to go for that case.
Changing from Tabs to Spaces (Different Level of Indentation)
Let’s say a straight conversion from tabs to spaces isn’t going to do it for me. Say the tab level is set to 6 spaces per tab, and I want to convert to spaces, but only 2 spaces per indentation. The trick here is to adjust the spacing while in tabs first, then convert.
Let’s say a straight conversion from tabs to spaces isn’t going to do it for me. Say the tab level is set to 6 spaces per tab, and I want to convert to spaces, but only 2 spaces per indentation. The trick here is to adjust the spacing while in tabs first, then convert.
Changing From Spaces to Spaces (Different Level of Indentation)
What if you have a file that is in spaces how you like it, but it’s using 4 spaces instead of 2? Sublime Text can still help here. You use the ability of tabs to be of variable length. So first convert to tabs, adjust the width, then switch back to spaces.
What if you have a file that is in spaces how you like it, but it’s using 4 spaces instead of 2? Sublime Text can still help here. You use the ability of tabs to be of variable length. So first convert to tabs, adjust the width, then switch back to spaces.
Changing From Spaces to Tabs
I think we’re getting the hang of this now. You can do any of this in the reverse direction if you prefer tabs.
I think you can extrapolate other switching scenarios from there.
I don’t think there is any scenario that you can’t adjust to your liking this way. If you’re caught manually adjusting indentation (that is consistent within itself), stop, you can figure out a way to fix it with settings.
If you want to discuss tabs vs. spaces below, go for it, but I can tell you I really don’t care. I mostly care that any particular project is consistent about it and enforces it.
I think we’re getting the hang of this now. You can do any of this in the reverse direction if you prefer tabs.
I think you can extrapolate other switching scenarios from there.
I don’t think there is any scenario that you can’t adjust to your liking this way. If you’re caught manually adjusting indentation (that is consistent within itself), stop, you can figure out a way to fix it with settings.
If you want to discuss tabs vs. spaces below, go for it, but I can tell you I really don’t care. I mostly care that any particular project is consistent about it and enforces it.
.editorconfig
Also worth mentioning: There is a thing called EditorConfig. You put a file in the root of your project called .editorconfig
. If someone opens that project in an editor that supports it, those settings will be honored. There is one for SublimeText.
The file might look something like:
Also worth mentioning: There is a thing called EditorConfig. You put a file in the root of your project called .editorconfig
. If someone opens that project in an editor that supports it, those settings will be honored. There is one for SublimeText.
The file might look something like:
Join Our Telegram Channel for More Insights
WMK-IT
Machinlearning
python
Programming
Javascript
Css
Mobile Application
Web development
Coding
Digital Marketing
Web Development
WMK-IT&TECH
Job
Blog post
WMK Tech Copyright © 2024. All rights reserved