How to edit a text file using different methods in Linux


This article is all about the text editor in Linux operating system. In this article, we learn about the different methods of edit a text file in Linux and how to edit a text file in Linux using the different command in a terminal. There are two ways present in the Linux for edit a file in which one is graphical and another is by command line. To edit a file in the graphical mode such tools are present for this work like in Debian we use gedit tool for edit a file in a graphical way. Many other distributions have different types of software to do this work. But one thing is similar for all the distributions of Linux to edit a text file that is the command line. There are such tools are present In all the Distributions of Linux operating system and they are controlled by some specific commands and they provide you a platform to edit text files in Command mode. So, let’s start here how to edit a text file using different methods in Linux.

1.  When you are not using a graphical desktop, a text file can be edited by using the different type of commands such as vi, vim, nano.

2. When you are using a graphical desktop, a text file can be edited by open a file directly from their location, and by using a gedit command in terminal to open a file editor.

different ways to edit a text file in linux

Edit a text file using gedit

Linux operating system includes several text editor applications which allow you to view and modify plain text files. These plain text files are those files which contain text without any font and style formatting applied to it, like configuration files and system log files.

gedit is a graphical text editor. It works similar to ms office in windows operating system, it can open, edit, and save plain text files.

To open gedit text editor

First, we will click on the show application and then we will search for the text editor or you can open text editor by typing gedit in the terminal and then click enter.

After that there will be application appear of name text editor, now you can open it by clicking on it and from here you can edit your file.

There is another option to edit your text file directly. To open your file directly in text editor follow these steps:

First, open the terminal on your computer and then you will type

sudo gedit a.txt

Here a.txt is your file name and you will type your file name instead of a.txt which file you want to edit. After editing the file you can save your file by click on the save option.

Edit a text file using vi command

Here vi command is used to edit the text files in the Linux operating system when you are not using the graphical desktop in Linux. And the structure of the vi command to open the file will be in following way:

vi filename 

Steps to edit the file using vi commands

Now consider that you already have a file which you want to edit is a.txt.

First, you will open the terminal on your computer and then you will go to the current directory in which your file is saved and then you will type.

root@kali:~# vi a.txt

The above command will open a file in normal mode from where you can view and run built-in commands on the file but you can not add text to it.

Now you will press I for entering in insert mode, and after press I key, you can edit your file content.

After editing your file content you will press Esc to exit insert mode and reverts to normal mode and then you will type :wq to save and exit from the vi command mode.

Edit a text file using vim command

Here vim command is used to edit the text files in the Linux operating system when you are not using the graphical desktop in Linux. And the structure of the vim command to open the file will be in following way:

vim filename 

Steps to edit a file using vim command

now consider that you already have a file which you want to edit is a.txt.

First, you will open the terminal on your computer and then you will go to the current directory in which your file is saved and then you will type.

root@kali:~# vim a.txt

The above command will open a file in normal mode from where you can view and run built-in commands on the file but you can not add text to it.

Now you will press I key for entering in insert mode after press I key, you can edit your file content.

After editing your file content you will press Esc to exit insert mode and reverts to normal mode and then you will type :wq to save and exit from the Vim command mode.

There are various keys which can be used in the normal mode of the vi and vim command mode these keys are as follows:

:wq       to save the file

:q          to quit the file without saving

:wq!     to forcefully save the file

:q!         to forcefully quit the file without saving

:w          to write without quitting

:e!          to reload currently open file from disk

:w a.txt    to write buffer to file a.txt

:e b.txt     to open file b.txt

dd         to remove a line

5dd      to removing five lines

            to replace a character

x             to delete a character

5x           to delete 5 character

u             to undo the last section

j               to join current lines and next lines

h              to move left one character

j                to move down one character

k               to move up one character

l                 to move right one character

$                to the end of the line

^                to the beginning of the line

Edit the text file using nano command

Here nano command is used to edit the text files in the Linux operating system when you are not using the graphical desktop in Linux. And the structure of the nano command to open the file will be in following way:

nano filename 

Steps to edit a file using nano command

Now consider that you already have a file which you want to edit is a.txt.

First, you will open the terminal on your computer and then you will go to the current directory in which your file is saved and then you will type.

root@kali:~# nano a.txt

  After that, your file will open in nano command mode and here you can edit your file content.

  After editing the content, you will save your file by press the ctrl+x key and then press the Y key and then press the enter button. After that, your file is edited and saved successfully.

                                                                 THAT’S IT

These are the possible ways by which you can easily edit a text file using both the ways that are graphical and command line. If you have any queries regarding this then solved out through the comment section. If you have found this article helpful for you then provide feedback to us. Because your feedback is valuable for us and also subscribe our blog for further updates.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s