28.6 C
Jaipur
Thursday, October 28, 2021

How to Create a New File in Vim? – Linux Hint

Must read

Vim is indeed a popular text editor widely used in the command-line interface of Linux operating systems. Since Linux makes heavy use of configuration documents, you’ll have o modify them regularly, and Vim has become an excellent tool for doing so. Vim seems to have a distinct operational technique, with two key modes. One of them is command mode, and the other one is a merge of all additional modes. In this discussion, we will discuss the process of creating a Vim file using the Vim text editor in the Linux System.

Prerequisites:

Make sure to have any Linux distribution installed on your operating system, e.g., we are using Ubuntu 20.04 for this example. Also, ensure to have a root account created in the Linux system. Login from the Linux root account to start working on Vim without any problem.

Install Vim Utility:

Right after you login from your root account or any account of the Linux system, go to the upper left corner in the Activity area. You will have a search bar there. Type the word “terminal” in this search area and hit the “Enter” using your keyboard. You can also use the shortcut key Ctrl+Alt+T to open it. Now, you must check whether the Vim text editor is already installed on your Linux system or not. For that, you have to try out the below “vim” command in the terminal. The output screen shows that it is not installed yet and also provides some of the commands to install it. Hence, you have to install it first to work on it.

So, to install the Vim editor on your Linux distribution, you have to try out the below-mentioned command in your command terminal. The installation process needs the password of the root account to make changes to the system. Therefore, you need to type the password next to the line: [sudo] password for account_name, and press the “Enter” via keyboard. It will start the installation process.

Meanwhile, it will affirm the installation action by throwing a question in the middle of the installation process, e.g., Do you want to continue? [Y/n]. To install the Vim editor, type the “Y” key from the keyboard, or hit the “N” button to stop execution. As we pressed the “Y” key, it starts working again on the installation of Vim.

Sit back and relax while the setting up has been accomplished. The completion of the Vim installation can be similar to the output shown in the image underneath.

Again try out the “vim” command as below.

You can see that it has been properly installed as below.

Create Vim File:

To create a new Vim file, you have to use the vim command and the name of a file you want to create. This file can be of any type and extension. Make sure to navigate towards the directory where you want to save this file. Suppose, you want to create a file “new.html” in the Home directory of your Linux system using the vim command having “html” as an extension. Type the below command to create it via Vim editor and hit on the “Enter” button.

Right after hitting the “Enter” button, you will get a new window as shown in the below snapshot. This new window is an interface of the newly created file “new.html”. The lower side of the header shows the name of this file as “new.html”. You will see that you would not be able to do anything on it because you have a normal mode opened at the moment. To add some data or text to it, you have to jump to its “Insert” mode by pressing the “i” key from the keyboard.

The insert mode is something like the picture in the snapshot below. You can see that the insert mode allows you to write something in this file. You can also see a mode writer’s name at the most left corner of the vim file.

Now, you have to add some data or text in this file to see if it’s working in an insert mode. Suppose, we have added the below text in this file as presented in the picture. After editing, you have to save this text in the “new.html” vim file. For that, you need to jump to a normal mode again. That is why you need to press the “Esc” key from your typewriter to exit from the Insert mode of Vim editor. This will navigate you to the normal mode, and you will see that the “INSERT” keyword will be removed from the bottom part of the Vim file.

You need to save this data into this vim file, e.g. “new.html” and also quit the file. For this purpose, we need to add the “wq” command along with the colon “:” sign as shown below. In this command, “w” stands for writing the data and “q” stands for quit the file. So, add the below “:wq” command and hit the “Enter” key to save and quit the file. You can see this command at the most left bottom side of the Vim editor.

:wq

Finally, you are out of the Vim editor, and your vim file “new.html” has been saved into the Home directory. You can check for your vim file in the Home directory. The below image shows the newly created vim file in the Ubuntu 20.04 Linux distribution’s Home directory. You can open it using any browser to see how its code works.

Conclusion:

We have efficaciously set up the Vim editor in our Ubuntu Linux system and perform all the necessary steps to create a Vim file using the Vim editor in this tutorial.

Source link

- Advertisement -

More articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisement -

Latest article