Many engineers around the world use Vim as their code editor. Some developers choose Vim as their default code editor as they find its shortcuts and commands more productive, while others are forced to use it as they work on machines with no GUI. In both cases, this article will be helpful to fix a common issue faced by many of them.
When working on code in Vim that is sensitive to indentation – such as a Python file, for example – some developers face a problem. They find it challenging to differentiate the tabs from the spaces.
In this tutorial, we’ll explore some of the ways in which we can tackle this problem and make it easier to see all the tabs.
2. Displaying Tabs as Characters
The first and most convenient solution to this problem is to set the tabs to be displayed as certain characters in our code. We can achieve this by using Vim commands that we’ll explore in the next sections. The effect of these commands is going to stay as long as the Vim session remains open.
2.1. Show All Whitespace Characters
To show all whitespaces as characters we’ll use:
Let’s try it on a simple Python for loop to see how it works.
for x in range(3): print(x)
Shown above is how the code looks before we run the command.
Now, let’s run the command to see how it changes:
for x in range(3):$ ^Iprint(x)$
As we can see above, there are some changes to the code.
Most significantly, Vim displays the tab before the print as “^I”. The dollar sign at the end of the line represents a line ending.
This solution is nice for showing the tabs. However, it messes with the indentations and affects readability.
In the next section, let’s see how we can improve readability.
2.2. Customize How We See Whitespace Characters
In this section, we’ll make our file more readable by preserving the original file indentation. We can do this by running an additional Vim command on our file:
This command tells Vim that we want to display a tab as “>” followed by as many “-” as required to fill the original tab space. So, for example, if the tab size is configured as four spaces, then the tab will be displayed as “>—“.
This allows us to keep the original file formatting and makes it more readable.
Let’s try it on our example:
for x in range(3): print(x)
Now, let’s run:
Our code will now look like this:
for x in range(3): >---print(x)
We can see that the code is now a lot more readable.
We can choose any other characters to replace the tab instead of “>-“. The first one will be used once and the second will be used as much as required to keep the original file indentations.
3. Highlighting Tabs
Another way to show tabs, without displaying them as other characters, is to highlight them. We’ll explore how we can do this in the next sections.
3.1. Temporary Highlighting of Tabs
The first way to highlight tabs is to use the find tool in Vim. To do this, we can use:
This will highlight all the tabs in the file so they are clearly visible. However, the downside of this approach is that if we want to search for another word, the highlighting of the tabs will go away. So, we can call this temporary highlighting of tabs (until we need to search for something else).
3.2. Permanent Highlighting of Tabs
There is another way to highlight tabs that is more persistent than using find.
We can do this by telling Vim to treat tabs as errors, which causes Vim to highlight all tabs in red. This will not really cause an error and will allow us to search for other words using the find tool while still having the tabs highlighted.
To do this, we can use:
match Error /\t/
This command will tell Vim to treat any tab as an error and will, therefore, highlight it in red.
3.3. A Bonus Trick
Another trick that we can use to highlight tabs in Vim is to use syntax highlighting. There’s a programming language called whitespace that is completely based on whitespace characters. So, we can trick Vim into thinking that our code is written in this language, and this will make it highlight all whitespace characters, including tabs.
To try this, we can run:
After we run this command, we’ll find that all spaces, tabs, and linefeeds are now highlighted.
Note that this trick is not recommended as this will cause Vim to treat the code in the file not as Python code, but as whitespace code. This will lead to Python syntax highlighting no longer appearing, which may decrease file readability. So, it’s better to choose one of the above solutions instead.
In this article, we’ve seen many ways in which we can make tabs in a file more visible to the user. Some of them highlight tabs while others replace the tabs with other characters. In the end, all of them have their pros and cons, and it’s up to us to choose the one that best fits our needs.