How to Edit File in Linux
One of the primary starting points that you must be familiar with is how to edit a file on the Linux system. In the process of running applications on a system, we often require prompt adjustments, and knowing how to edit files on Linux can be immensely useful. There are several text editors available to perform this function, with some of the most popular ones being nano, vi, and emacs.
In this introduction, we will explore the basic commands and usage of the nano text editor. This text editor is a popular choice amongst beginners due to its simplistic approach and user-friendly interface. Therefore, learning how to edit files through nano can be a valuable addition to your skill set as a newcomer to the Linux operating system.
Then we will shift our focus to another text editor that is widely used in the world of Linux - the vim editor. This editor has gained popularity over the years due to its advanced features and versatility. As a beginner, learning how to edit files through vim may seem overwhelming at first, but the benefits it offers make it worth the effort.
1. How to Edit a File on Linux Using the Nano Editor:
Nano editor is a simple built-in editor in Linux distributions that is used to edit files. We don't need to understand anything before utilizing this; it's straightforward. All of the fundamental operations using the symbols are listed at the editor's bottom. You may utilize them by holding down the "Control (ctrl)" key. There is no prerequisite or command required to use this.
To open the nano editor, first, check its version using the command in the terminal:
$nano –version
Simply enter "nano" followed by the file name to be modified in the editor. For example, in my system, there is a file called "testing.txt"; to open it, type:
$nano testing.txt
To edit it, move the cursor to the desired location and begin typing.
After making changes, hit the "ctrl+o" keys to save it:
The file modification was successfully saved. To quit the nano editor, hit the "ctrl+x" keys.
To test the change, open the terminal and type cat with the file name:
2. How to Edit a File on Linux Using the Vim Editor
The vim or vi editor is most likely a built-in Linux editor with several modes. Most programs only have one mode for editing documents. However, once you begin using the vim editor, you will have access to features such as insert, command, line mode, and many more.
Before you begin using the vim editor, you need to be familiar with certain fundamental mode-switching symbols:
- “i” for insert mode
- “w” to save the file
- “Esc” for normal mode
- “:q” to quit the editor without saving
- “:wq” to save the updated document and quit the editor
To check which version is installed in your system, type in the terminal:
$vi –version
To open the file, enter vim with the file name, for example, to open "test.txt," type: vim test.txt
If the system backup does not contain a "test.txt" file, this operation will create one. To edit the file, hit "i." This will activate insert mode, allowing you to add anything to the file.
Save File: Use the ":wq" command to save a modified file and exit the vi editor.
Conclusion:
There are several ways to edit a file in Linux, including using text editors like vi, nano, or gedit. These tools allow you to open a file, make changes to its contents, and save your work. Alternatively, you can use specific programs to edit certain file types, such as using a word processor to edit a document. Ultimately, the method you choose for editing a file in Linux will depend on your personal preferences and the type of file you are working with. No matter which option you choose, you will be able to easily modify your files in Linux.