Modify Files in Linux using vi, nano or emacs


Introduction

Linux users frequently utilize Vi, Nano, and Emacs as their preferred text editors. Vi is a standard editor that is revered for its advanced functionalities and efficiency. Conversely, Nano is a straightforward editor that boasts a user-friendly interface and is easily learned by novices. Emacs is a powerful and customizable editor with a complex user interface that is ideal for advanced users.

In this article, we are going to learn how to modify files in Linux using “vi, nano, and emacs”. Our discussion will encompass the installation process of each editor, their respective functions, as well as examples demonstrating how to employ them for the purpose of modifying files. With this knowledge, users can choose the editor that best suits their needs and master its features to perform text editing tasks efficiently in Linux.

Approach 1: Using the Vi Editor to Modify Files

Vi is a standard text editor in Unix and Linux operating systems. It is a powerful editor with many advanced features, such as multiple buffers, macros, and command line editing.

It is pre-installed on most Linux distributions. If the editor is not currently present on your system, you may install it by executing the following command −

$ sudo apt-get install vim

Now, type "vi newFile.txt " to open the file in Vim editor.

$ vi newFile.txt

After executing the Vim command this will open “newFile.txt” file. After we hit ENTER key, the screen will display a similar interface −

~                                                                              
~                                                                              
~                                                                              
~                                                                              
~                                                                              
"newFile"  [New File]                                         0,0-1                 All

To begin inserting new text, switch to insert mode by pressing the "i" key and commence typing.

This is a sample text.                                                                              
~                                                                              
~                                                                              
~
~
~                                                                              
-- INSERT --                                                 3,1             All

To save the modifications made to the file and exit, enter the command ':wq'. This will write the changes to disk and quit the editor.

This is a sample text.
~                                                                              
~                                                                              
~
~                                                                              
~                                                                              
:wq

Vi has different modes that you can use to perform different tasks. There are two primary modes in the editor, namely the insert mode and the command mode. In command mode, you can navigate through the document, delete text, copy and paste text, search for text, and perform other editing functions.

Commands

Functions

h

Move left

j

Move down

k

Move up

l

Move right

i

Insert mode

dd

Delete line

yy

Copy line

P

Paste line

/text

Search text

:w

Save

:q

Quit vim editor

Approach 2: Modifying Files Using Nano Editor

Nano editor is a command-line based editor with a basic user interface. Nano is ideal for beginners who want to modify files quickly and easily.

Nano is pre-installed on most Linux distributions. If the editor is not currently present on your system, you may install it by executing the following command −

$ sudo apt-get install nano

Now, type "nano file.txt" to open the file in nano editor.

$ vi nano file.txt

After we hit ENTER key, the screen will display a similar interface.

GNU nano 4.8                           file.txt                                  






                                     [ New File ]

We can type directly in the Nano editor since it does not have modes.

  • To save modifications made by you, use “Ctrl + o”.

  • And, to exit Nano editor, use “Ctrl + x”.

GNU nano 4.8                       file.txt                             Modified  

Hello, India.



File Name to Write: file.txt                                                   
^G Get Help        M-D DOS Format     M-A Append                M-B Backup File
^C Cancel          M-M Mac Format     M-P Prepend               ^T To Files

Nano has a simple user interface that allows you to perform basic editing functions.

Commands

Functions

Ctrl + g

Help menu

Ctrl + o

Save changes

Ctrl + x

Quit Nano editor

Ctrl + k

Cut line

Ctrl + u

paste line

Ctrl + w

Search text

Approach 3: Modifying Files Using Emacs Editor

Emacs is a command-line based editor with a graphical user interface (GUI). Emacs is ideal for advanced users who want to modify files using complex commands and macros.

In the majority of Linux distributions, Emacs is not included as a pre-installed application. But you can begin the installation process by executing the following command −

$ sudo apt-get install emacs

Then type “emacs newfile.txt” in the terminal.

$ emacs newfile.txt

After hitting the ENTER, Emacs displays a GUI screen. The editing section or the main buffer is at the top, and the status bar is at the bottom, displaying file name and cursor location.

U:--- newfile.txt                   All   L1         (text)-----------------------------------------
U:%%-     *GNU Emacs*      Top  L1         (Fundamental)-------------------------------(New file)

We can type directly in the Emacs editor since it does not have modes.

  • To save modifications made by you, use “Ctrl + X + Ctrl + S”.

  • And, to exit Emacs editor, use “Ctrl + X + Ctrl + C”.

hello world.





U:--- newfile.txt                   All   L1      (text)----------------------
U:%%-     *GNU Emacs*      Top  L1         (Fundamental)----------------------
wrote /home/papan/newfile.txt

Emacs has a complex user interface so that we can perform advanced editing functions.

Commands

Functions

Ctrl + x + Ctrl + f

Open file

Ctrl + x+ Ctrl + s

Save

Ctrl + x + Ctrl + c

Quit Emacs editor

Ctrl + SPACE

Set mark

Alt + w

Copy

Ctrl + w

Cut

Conclusion

From this article, we learned use of Vi, Nano, and Emacs editors that can be used to modify files in Linux. Vi is a standard editor with advanced features. Nano is a simple editor with basic features so that new learner can understand easily. Emacs is a customizable editor with a complex user interface that makes it ideal for advanced users.

Updated on: 29-Mar-2023

364 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements