How to Open and Edit Files in the Linux Terminal Using Vim

Neha Imran Feb 15, 2024
  1. Open a File in Linux Terminal
  2. Save a File in Linux Terminal
  3. Edit a File in Linux Terminal
  4. Delete a File Using Linux Terminal
  5. Use VIM in Windows CMD (Command Prompt)
How to Open and Edit Files in the Linux Terminal Using Vim

This tutorial will cover all the specifics of using a Linux terminal to create and manage files.

Learning to do things in different ways is always good practice. So first, we will discuss using the Linux terminal to create files and add data. Later, we’ll see how to use several commands in the Linux terminal to save, edit, and delete files. After this, we will discuss using vim in the Windows terminal.

Open a File in Linux Terminal

This first section of the article will cover how to open a new or existing file in the Linux terminal. This tutorial will concentrate on Python files with the .py extension. Let us start by making a directory, and then we’ll use the terminal to make our file in that directory.

In the Linux terminal, we use the mkdir command with the directory name to create a folder or directory. The command mkdir means Make Directory. We will write the command as mkdir DirectoryName.

To create a file in the newly created folder or directory, we must first enter that location. Then, to modify the current working directory, use the cd command, which stands for Change Directory with the directory name, for instance, cd DirectoryName.

Observe the image below to understand the flow of commands.

open and edit files in the linux terminal using vim - mkdir cd

Now we are in our working directory, i.e. pythonDir. Let us create a file, script.py, inside this directory. To create a file, we use the following command.

vim script.py

See the following screenshot as a demonstration:

open and edit files in the linux terminal using vim - vim

Click the Enter key now. If an existing file is identified, this will open it; otherwise, it will create a new file with the entered name and extension.

Currently, a new file called script.py has been created. As we can see, the word [New] is written next to our file name, indicating that this is a recently generated file.

open and edit files in the linux terminal using vim - vim new

Now let’s see how to enter any data into our file. We must first enable the INSERT mode to write anything into the file. To do this, press the I key on your keyboard, and the INSERT mode will be enabled.

open and edit files in the linux terminal using vim - vim insert

We are in the INSERT mode. You can now write into this file.

Save a File in Linux Terminal

To save a file in the Linux terminal, we have to disable the INSERT mode. To do this, press the ESC button. The INSERT mode will be disabled. Then write the command :w to save the file.

open and edit files in the linux terminal using vim - vim save

Press Enter, and it will save the file, but it will remain open. If you want to close the file right after saving it enter the command :wq, and your file will be saved and closed.

Edit a File in Linux Terminal

The steps to open and edit an existing file in the Linux terminal are the same as discussed above. Open the file using the command vim filename.extension. Enable the INSERT mode by clicking the I key.

Make your edits, and then press the ESC key to disable the INSERT mode. Finally, enter the command :w to save the file and :wq to save and exit the file.

Delete a File Using Linux Terminal

Delete a file using the Linux terminal is very easy. To delete a single file, use the rm or unlink command followed by the file name:

rm script.py

OR

unlink script.py

Use VIM in Windows CMD (Command Prompt)

Vim is a competent code editor. It is so strong that both Linux and Mac come pre-installed with it. But if you use Windows as your operating system, vim cannot be directly accessed in CMD and must instead be installed separately.

Fortunately, Microsoft makes setting up and running Vim on your PC very simple. Click here to download the vim. The following window will open. Click on a yellow highlighted link in the image below and download it.

open and edit files in the linux terminal using vim - vim windows

After downloading the file, run it; the following dialogue box will appear.

open and edit files in the linux terminal using vim - vim setup

Click on next to continue the setup. The following dialogue box will appear.

open and edit files in the linux terminal using vim - vim installation

Vim’s setup suggests a Typical installation; however, if your machine has enough space, there seems to be no harm in continuing with the full installation option and completing everything at once.

To install the full capabilities of vim on your PC, use the Full option. Then, complete the setting by clicking the Next button. You can now use vim with ease on a Windows computer. To create, edit, save, and delete files, we can now use all of the Linux terminal’s commands in the Windows terminal.

Related Article - Python Files