Creating and editing files is the most fundamental part of using a Linux-based system. There are different ways to edit files in Linux.
In this tutorial, we go over how to edit a file in Linux. You can either use a GUI text editor, or you can do it using the terminal. This tutorial will cover the tools you can use to edit a file in the terminal.
Tools/Text Editors to Edit Files in Linux
We’ve already worked with multiple text editors like vim, nano, emacs, etc. Let’s look at these different tools one by one.
1. Vim Editor
Vim text editor comes by default in most Linux distributions. One feature that sets it apart from other editors is that it provides several modes for editing text. Basically, there are two modes command mode and editing mode. Lets go over both of the modes,
Entering The Command Mode
Vim starts with command mode as default, we can use arrow keys to move around the file, but we cannot just edit the text right away in this mode. Instead, you can execute various commands, such as copying, pasting, and deleting text.
To access Vim’s Command Line mode, press the colon (:) key while in Command mode. This mode enables you to execute multiple commands, including saving files, finding text, and modifying configuration settings.
To check the version of your Vim editor, you can use the following command:
vim -v

Open a file
To open a file in the Vim editor, use the command :
$ vim [filename]

If the file does not exist, this command will create the file for you.
Entering The Editing Mode
To start editing, simply press the “i” key while in Command mode. Editing mode allows you to make changes to the text in your file and navigate around using the arrow keys. You can also take advantage of a range of editing commands, including cut, copy, and paste, while working in Editing mode.
Edit file
To edit a file, you need to enter the insert mode—Press ‘i’ to do so.

The screen will look as shown above. You can notice ‘INSERT‘ written at the bottom. It indicates that we are in insert mode. You can type out some text to put in the file. You can now edit the file.

Save and Quit
To save your changes and exit vim, use the command :
:wq [enter]
To exit without saving your changes, use the command:
:q [enter]

Note on Vim and Vi Editor
Vi editor was the pre-cursor to vim. In fact vim stands for vi improved. All of vim and vi commands are fully compatible between each other. Here are some of the vi commands to edit files in Linux,
Open a terminal window
We open a terminal window by either using the keyboard shortcut Ctrl + Alt + T or by finding the terminal application in your operating system’s application menu.
Editing Files in Vi Editor
Type “vi” followed by the name of the file you wish to edit. For example, if the file you want to edit is called “example.txt”, type “vi example.txt” and press Enter. This will open the file in the vi editor.
Press the “Esc” key
Press the “Esc” key to enter command mode. This mode allows you to enter commands for manipulating the text.
Move the cursor
Move the cursor to the desired location using the arrow keys on your keyboard. Alternatively, use the “h”, “j”, “k”, and “l” keys to move left, down, up, and right, respectively.
Editing the text in vi editor
Start editing the text by typing on your keyboard. The text you enter will be inserted at the current cursor position.
Saving the file in vi editor
To save the changes made to the file, press “:” followed by “w” and then “q”. This will write the changes to the file and exit the vi editor.
quitting the vi editor
To exit the editor without saving changes, press “:” followed by “q”.
2. Nano Editor
Nano editor comes pre-installed on most Linux distributions. To check the version of the nano editor on your system, use the command:
nano --version

If you need to install nano editor on your system, you can use the following commands:
sudo apt update
sudo apt install nano

Open and edit a file
To open a file in the nano editor, use the command :
nano [filename]
This command will open a screen where you can edit the contents of your file. Unlike Vim editor, you won’t have to enter insert mode to edit.

Save and Quit
You can see the options available in the nano editor at the bottom of the screen.

To save your changes and exit, use Cntrl + X.
The bottom of the screen will ask you if you want to save the changes.

Press Y followed by enter if you want to save the changes otherwise, press N followed by enter to quit without saving.
To learn more about nano editor, refer to their official website.
Emacs editor
Emacs is one of the oldest and most versatile text editors available for Linux UNIX-based systems. It has been around for over 20 years. It is known for its powerful and rich editing features.
To install the Emacs editor on your system, use the command :
sudo apt-get install emacs
During installation, you will be prompted to confirm the installation.

Press Y to continue.
After installation, you can check the version using the command :
emacs --version

Open and Edit a File
To open and edit a file using the command :
emacs -nw [filename]
Here -nw means no window. This makes sure that the editor window opens in the terminal itself.
This will open a screen that will look like this:

You can edit the content directly without entering any mode, like in the case of the nano editor.
Save and Exit
After editing, you can save your changes and exit using Cntrl + X, followed by Cntrl + C. The editor will ask you if you want to save before quitting Emacs.
If you only wish to save and not exit the editor, press Cntrl + X followed by Cntrl + S.
To learn more about emacs editor, refer to their official website.
Conclusion
The ability to modify documents within Linux is essential for anyone utilizing a Linux-driven platform. This guide has discussed three distinct text editing programs – Vim, Nano, and Emacs – which can be employed to alter files through the terminal. Each editor possesses its exclusive capabilities and instructions, making it crucial to select the one that best suits your needs. Whether you are a novice or an experienced user, honing your skills in document modification on Linux will enable you to be more effective and prolific in your tasks. Therefore, continue to practice and delve into the expansive realm of Linux-oriented platforms. To learn different ways of creating a file in Linux, read this tutorial.