The file has been launched in the Vim editor within the normal mode. We have gone to the insert mode using the “I” button from the keyboard and added some text to it. The default tab can place 3 to 4 spaces within the text upon use.
Let’s see what the value of 1 tab in Vim within Ubuntu 20.04 by default is. So, after the first word, we placed the cursor on the first space and pressed the tab. It has inserted the 4 spaces after the first word, as you can see from the image attached below.
Let’s change the configurations of a tab as per our choice using the vimrc configuration file. So, within the normal mode, press “:” to get into command mode. To open the configuration vim file, add the below-stated command in the command mode and press Enter.
Now that the configuration file has been opened, set the expand tab, and the tabstop equals 1. The shift width has been set to 1 to give a space of only 1 character upon pressing the tab. After adding all these configurations, add the “retab” command in the command mode and press the Enter button to apply the new changes into the Vim editor.
After adding the “retab” command, make sure to use the source command in the command mode to save the changes in the vim configuration file. The command is as follows:
:source ~/.vimrc
Finally, the changes have been applied and are ready to be used in the Vim editor as we have been returned to the normal mode of Vim using the “Esc” key.
Open the Insert mode using the “I” key from the keyboard and place your cursor at the character “r” of the word “word” within the Vim new.txt file. Now press the “tab” key from your keyboard.
As we have pressed it two times, thus the output we have is something like shown below.
Let’s put the cursor on another word, i.e., Linux, at the character “n” and press the tab button 3 times. This action has added 3 spaces within the word Linux as shown in the attached image.
Let’s undo the changes using the “u” command in the normal mode of Vim. Now the text has been in its old situation. Let’s set the tab and space settings within the Normal mode of Vim without getting into the Vim configuration file. So, within the normal mode, press the “:” button from the keyboard to get into the command mode. Now, add the expandtab command followed by the Enter key to enable the expand tab into spaces as stated and shown below.
After enabling the setting of expand tab, let’s set the stopping position of a 1 tab. So, we have used the “:” key to get into command mode and set the value 5 to the tabstop property followed by the Enter key. It will automate the tab stop after the 5 character spaces in the file.
Now we have to set the value of 1 tab in the Vim as well. Thus, we opened the command mode with the “:” character and used the command stated in the image. We have set the value of shift as 5 to add 5 character space upon pressing the tab button 1 time from the keyboard.
Now, we have opened the insert mode using the character “I” from the keyboard and updated the text of the vim file, as shown in the image below. We have placed the cursor at the last character of the first word, “Linux” within the text within the insert mode with no spaces. After that, we have pressed the tab button only once to see how it works.
The output for 1 tab has been shown below. You can see that the configuration just set has been eventually applied successfully, and 1 tab has been given 5 spaces within the text.
Now, we have placed our cursor at the end of each word and pressed the tab button. In return, we have got the 5 character spaces after every word as shown in the Insert mode of Vim below.
Let’s disable the conversion of the tab into spaces as per the configuration we have done just now. So, within the normal mode, we have opened the command line using the “;” character. Then, we used the noexpandtab command and pressed the key “Enter” to disable this setting, as shown in the image below.
Now, use the “retab” command to apply the changes to the Vim editor, as shown below. The “!” mark is used to enforce the changes.
Conclusion:
This article is a very well explained and detailed guide to convert tabs into spaces within the Vim editor of the Ubuntu 20.04 Linux system. We have covered the ways to change the configuration for tabs using the vim configuration file and the commands. We believe that it will be quite helpful to everyone out there.