How to Edit Files in Linux?

Edit File In Linux

Creating and editing files is the most basic part of using a Linux based system. There are different ways to edit files in Linux.

This tutorial we go over the ways that you can use 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. To check the version of your Vim editor you can use the following command:

vim -v 
Vim Version

Open a file

To open a file in Vim editor use the command :

$ vim [filename]
Vim File

If the file does not exist then this command will create the file for you.

Edit file

To edit a file you need to enter the insert mode. Press ‘i’ to do so.

Insert Mode
Insert Mode

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

Vim Editor

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]
Vim Wq
Vim Wq

2. Nano Editor

Nano editor comes pre-installed on most Linux distributions. To check the version of nano editor on your system use the command:

nano --version
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
Install Nano

Open and edit a file

To open a file in 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.

Nano Editor

Save and Quit

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

Nano Bottom Part

To save your changes and exit use Cntrl + X.

The bottom of the screen will ask you if you want to save the changes.

Save Changes
Save 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 Emacs editor on your system use the command :

 sudo apt-get install emacs

During installation you will be prompted to confirm the installation.

Emacs Install

Press Y to continue.

After installation you can check the version using the command :

 emacs --version 
Version
Version

Open and Edit a File

To open and edit a file use the command :

emacs -nw [filename] 

Here -nw mean no window. This makes sure that the editor window open in the terminal itself.

This will open a screen that will look like :

emacs Screen
emacs

You can edit the content directly without entering any mode like in the case of 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

This tutorial was about the different ways you can use to edit a file in Linux. To learn different ways of creating a file in Linux read this tutorial.