How to edit a text file in Linux using different methods?

This article is all about how to edit a text file in Linux using different methods. In this article, we learn about the different methods of editing a text file in Linux and also about the text editors in the Linux operating system. There are two ways available in the Linux to 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 to 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.

Wanted to save a video from Facebook so you can watch it again but can’t because Facebook doesn’t provide download option don’t worry Here is our guide on how you can download a video from Facebook directly?

1.  When you are not using a graphical desktop, a text file can be edited by using the different types 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 gedit tool in Linux.

how to edit a text file in linux

Edit a text file using gedit

Linux operating system includes several text editor applications that 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 command 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 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. The structure of the vi command is given below

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, open the terminal on your computer, and then go to the current directory in which your file is saved and then type.

[email protected]:~# 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 press the “I” for entering in insert mode after that you can edit your file content.

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

Edit a text file using vim command

vim command is used to edit the text files in the Linux operating system when you are not using the graphical desktop in Linux. The structure of the vim command is given below.

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, open the terminal on your computer then go to the current directory in which your file is saved and then type given below command.

[email protected]:~# 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 press the “I” key for entering in insert mode after that you can edit your file content.

After editing your file content press Esc key to exit insert mode and reverts to normal mode then 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

nano command is used to edit the text files in the Linux operating system when you are not using the graphical desktop in Linux. The structure of the nano command is given below.

[CSS]nano filename [/CSS]

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, open the terminal on your computer then go to the current directory in which your file is saved and then type.

[email protected]:~# 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. Comment your thoughts

You may also like...

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: