Whether you like to use tabs or spaces while writing code is pretty much a personal preference. But both of ‘em have their goods and bads. Some people simply like the idea of using spaces alone and not mix them up with tabs, given how one could fine-tune indentations and sub-indentations in his or her code. However, constantly requiring to press the spacebar in order to achieve the same indentation you’d get when pressing the tab key only once is surely annoying. This is one good reason why many speedster programmers prefer using tabs instead.
Whichever side you take in this battle of the best type of indentation, there are times when you end up using both. In fact there are people who use them both equally, and trust me, that is not a very good practice. In order to avoid indentation issues with one’s code, Vim has a specially dedicated command called retab, which allows you to convert spaces into tabs in your file, and vice versa. In this article, we’ll discuss both of these and a few other things you yourself should know about the vim outlines Retab command.
Convert Tabs to Spaces using Retab
This one is more commonly used over the spaces-to-tabs one. Why, you may ask? Well probably because it gives you a lot more control over what levels of indentation you want to apply to your code, by only altering the required parts and keeping the rest of it all as is, without the hassle of smashing off your spacebar.
Using expandtab command
Now coming to the actual process, there are two different ways you could achieve this. One of these ways is to specify a tabstop of 1 – more on that later if you don’t know what it is. While the second option is to first use the expandtab command and then use the retab command. You can also directly include a setting in your .vimrc file which will automatically convert your tab presses into spaces, without needing to use retab. But for the most part, if you don’t want tabs to be converted in every file you type in, it’s best you don’t do so.
Let me create a demo file to show you the commands in action.
Well, well, well. Should be sufficient enough for now. Now there are two commands I need to use. The first one being:
And the second one being:
Wala! The tabs are now automatically converted into spaces. Couldn’t make sense out of it yet? What’s essentially happening is that when you use the expandtab command, the tabs are supposed to be automatically converted to spaces. But this setting does not apply on already typed in tabs. Which is why, right after it, we use the retab command to bring it to action.
However, there’s an even better way to do this, in case you’re in a hurry. Simply type out the following command inside your target file to carry out both tasks at once:
This will set the expandtab command and also execute retab on your current file at the same time, which should save you a few keystrokes.
Well this one is a bit hard to explain, if you don’t already know what the command does. But basically what we will be doing is that we set the value of tabs to be only 1 whitespace wide, which ends up being a space anyway. If you’ve noticed well, pressing the Tab key will usually be equivalent to 2-4 or even 8 spaces. On the other hand, using a value of 1 will make the tabs appear as spaces. However, be noted the tabs will not actually be converted to spaces, but rather, the tabs themselves will have a whitespace value of 1 – the same as spaces. The command should look something like this:
If you haven’t understood the trick here, apologies. But if it helps you out and gets the jobs done, you can be rest assured that the command is safe, legit, and functions exactly as the previous one. This one is probably more neat, and getting used to it will also be a good bet, which you’ll know why right in the next section.
Convert Spaces to Tabs using Retab
Using the noexpandtab command
If you haven’t read the previous section before coming straight out into this one, I suggest you read it first, even if you may not want to convert tabs to spaces.
Anyways, if you had followed the previous section well, you should be knowing that we first use the “set exapandtab” before using retab. This time around, the only different will be to change “set expandtab” into:
And the retab command into:
Yeah. You caught it right. It just inverses the function of the retab command. This of course, is still quite tedious, so you can do it all in one go by using:
But it still is quite long and tedious. Which is exactly why, once again, our tabstop method comes into play!
Using a tabstop value
Just like we previously saw, you could define a custom tabstop value after the retab command. We initially used 1 in order to make the tabs function as spaces, but this time around, all we’ll do is change that 1 into something like 2, 4 or 8, which are real tab values. Why not give it a ‘lil try?
This command will essentially search for instances where there are 8 tabs in a row (which have a whitespace value of 1, from the previous retab 1 command), and convert them into tabs with higher widths.
But before you use the tabstop method whether it is from 1 to more or from more to 1, I’d recommend you to not mix it with the expandtab method, as they could end up having a bit of conflict, and you may not get the results as expected.
What you need to realize is that “retab 8” will only work on tabs with a whitespace value of 1, and not real spaces. So if you try this on a file that already has real spaces, it won’t work. You may only use this command if you’re already used to the retab 1 workflow. Or else, it’s simply better to use the expandtab method.
Using Retab in a given range
If you don’t want the entire file to be affected by the retab command, you can actually specify a range for it to be executed on. This is best done by using Vim’s Visual Mode, and then you can use any of the retab command by typing “:” after you have created the selection:
When applying retab on a range, it’s best done with the expandtab method, and could cause weird glitches for the tabstop one. At this point, you might be wondering if it’s even worth using the tabstop method, but you’d only realize its power (as long as you don’t need to use it on ranges), once you start using it in all your files!
That is pretty much it about the Vim Retab command! If you need more control over how the stuff is converted, you could probably find scripts online that enhance the functionality of retab, such as converting single spaces into tabs, and what else not. But for the most part, this is pretty much all you’d need.