Open the command console of your Ubuntu 20.04 system with the simple shortcut, i.e., Ctrl+Alt+T as we have known it for so long. To begin the article, we need a file with text in it. So, we will be using the “touch” query to create a new text file named “new.txt”. Within the console, we will be opening this newly created text file in the Vim editor while using the “vim” command using the name of a file as shown below. The file will be opened within the panel mode of your Vim editor. Press the “Enter” key to reach the file’s normal mode.
Now, within the normal mode, we are unable to add any data within our file. It’s necessary to open the “Insert” mode of the Vim editor to add some text and perform some functions. Thus we have simply pressed the “I” button from the keyboard while we are at the Normal mode of Vim. It will quickly convert the Normal mode to the “Insert” mode, and we can add some text data to the file. You can see, we have added 5 same words with a distance of some spaces in the Insert mode total of 43 characters in 1 line.
Show All White Spaces:
Right now, you are unable to see the whitespaces between the text words of this mode. To see all the spaces inserted within this file of data, you need to add the “hls” command on the command area of Vim. So, after adding the text within the Vim text file, navigate towards the normal mode by pressing the Esc key. Press the “:” character to open the command area. Now add the “hls” command stated below to show all the spaces found in the text of this file.
:se hls /\_s
When you add the “hls” command and press the “Enter” button, the output will be something like below. The yellow highlighted space shows the whitespaces between the words and at the end of the line.
Within the insert mode, we have added another line of text within the new.txt in Vim. We have also added space as characters after both the lines. All the spaces between the words of lines and the end of these lines are clearly shown as a yellow color.
If you don’t want to show all the whitespaces of the text on the Vim editor of your system, you need to turn the “hls” command off. For that purpose, go to the normal mode first. Press the “;” character, and you will be in the command mode. Add the “nohls” command in it preceded by “se” and press the “Enter” key from the keyboard. You will see that the yellow highlighted spaces will be gone as below. So, this was all about showing the all-whitespaces within Vim.
:se nohls
Show Only EOL White Spaces:
Rather than displaying all the whitespaces of the whole text file, one can also show the unwanted spaces, i.e., spaces after the end of the line. First of all, you need to change the color we want to see in place of spaces. As we have seen, the yellow color was displayed when we highlighted the spaces in the above example. So, let’s change the color to red this time. For that purpose, I got to the normal mode of the Vim text file using the “Esc” key. Press the “:” character to open the command mode. Now within the command mode, try adding the stated command to change the color of highlighted spaces.
:highlight ExtraWhitespace ctermbg=red guigb=red
After adding the highlight command and pressing Enter, the updates will be applied to the editor. From now on, we will be seeing the red color for whitespaces instead of the yellow color. So, let’s see how we can only get the whitespaces that are extra and held at the end of both the text lines. So, within the normal mode, tap the “:” character to get the command mode. Within the command area, try out the match command to show all the whitespaces after the end of both lines. The command is something as follows:
:match ExtraWhitespace /\s\+$/
After pressing the Enter key from the keyboard, our Vim screen has been highlighted with red color where the whitespaces have been found so far at the end of both lines.
If you want to remove the spaces, you can do so as well. As you can see, we have removed the second text line from this Vim file, and now we are only left with the extra whitespaces held at the end of the first line of text.
Let’s have some more fun and try to change the color once again with the same olf “highlight” command. This time, we need to update the command with the word “blue” replacing the word “red”. Use the highlight command within the command area of Normal mode and press the “Enter” button from your keyboard.
The vim is showing the extra whitespaces as blue highlighted color at the end of each line.
Conclusion:
We have discussed the ways to show the whitespaces found in some text files by using Vim editor. We have discussed the commands to show all the whitespaces and the spaces only at the end of each line. We have used different colors to show spaces and hope you will like them.