How to Edit a File in Linux? [SOLVED]


Linux, Tips and Tricks

Author: Omer Cakmak
Reviewer: Deepak Prasad

In Linux operating systems, everything is a file. There are daily routine tasks within the system such as turning new features on or off or editing files. An editor is needed for these tasks, regardless of GUI or CLI.

In this article, we will explain how to edit files in Linux operating systems.

Linux users have many alternatives for editing files. Let's talk about a few applications for both GUI and CLI. At the end you will find the list of alternative apps for you.

 

Edit Files in GUI using Gedit

The first application we will describe is Gedit. Gedit is a text editor for the GNOME desktop. If you are using your Linux system with Gnome Desktop, your default text editor may already be Gedit. If you are using a different desktop, you may need to install it.

To install on Debian-based systems(Ubuntu, Mint, Pardus, etc), run the following command in terminal:

$ sudo apt install gedit -y

For installation on Redhat-based OS(Fedora, CentOS, AlmaLinux, Rocky Linux, etc):

$ sudo dnf install gedit.x86_64 -y

There are many plugins for Gedit. You can install the plugin you need from the package repository.

$ sudo dnf search gedit-plugin-
Last metadata expiration check: 0:01:40 ago on Sun 12 Feb 2023 10:04:43 +03.
========================= Name Matched: gedit-plugin- ==========================
gedit-plugin-bookmarks.x86_64 : gedit bookmarks plugin
gedit-plugin-bracketcompletion.x86_64 : gedit bracketcompletion plugin
gedit-plugin-charmap.x86_64 : gedit charmap plugin
gedit-plugin-codecomment.x86_64 : gedit codecomment plugin
gedit-plugin-colorpicker.x86_64 : gedit colorpicker plugin
gedit-plugin-colorschemer.x86_64 : gedit colorschemer plugin
gedit-plugin-commander.x86_64 : gedit commander plugin
gedit-plugin-drawspaces.x86_64 : gedit drawspaces plugin
gedit-plugin-editorconfig.x86_64 : EditorConfig plugin for Gedit
gedit-plugin-findinfiles.x86_64 : gedit findinfiles plugin
gedit-plugin-git.x86_64 : gedit git plugin
gedit-plugin-joinlines.x86_64 : gedit joinlines plugin
gedit-plugin-multiedit.x86_64 : gedit multiedit plugin
gedit-plugin-sessionsaver.x86_64 : gedit sessionsaver plugin
gedit-plugin-smartspaces.x86_64 : gedit smartspaces plugin
gedit-plugin-synctex.x86_64 : gedit synctex plugin
gedit-plugin-terminal.x86_64 : gedit terminal plugin
gedit-plugin-textsize.x86_64 : gedit textsize plugin
gedit-plugin-translate.x86_64 : gedit translate plugin
gedit-plugin-wordcompletion.x86_64 : gedit wordcompletion plugin

After installing you can invoke Gedit in the Applications menu. Or you can open the file by clicking on a file (if it is not the default editor, right click and open with "Open With Other Application" > Gedit).
Edit Files in Linux

After this stage, you can start editing your files with pleasure.

Another application you can use to edit text in Linux is Mousepad. Mousepad is a simple text editor for Xfce desktop environment.

To install on Debian-based systems(Ubuntu, Mint, Pardus, etc), run the following command in terminal:

foc@ubuntu22desktop:~$ sudo apt install mousepad -y

For installation on Redhat-based OS(Fedora, CentOS, AlmaLinux, Rocky Linux, etc)

foc@fedora:~$ sudo dnf install mousepad.x86_64 -y

If mousepad is already your default text editor, double-clicking the mouse opens the file with the mousepad. If Mousepad is not the default editor, right-click and select "Open with Other Application" → Mousepad.
How to Edit a File in Linux? [SOLVED]

Mousepad is a very stable and useful text editor.

 

Edit Files in CLI

Method-1: Using VIM

Every Linux distribution has a text editor. Built for the end user, Linux OS comes with many text editors installed, while server systems have fewer. But vim/vi is available in almost all Linux distributions.

If for some reason it is not installed, to install it on Debian based systems:

foc@ubuntu22desktop:~$ sudo apt install vim -y

To install on Redhat based systems:

foc@fedora:~$ sudo dnf install vim -y
foc@fedora:~$ vim sample.txt

Type the filename you want to edit after the vim command in the terminal. If the file does not exist before, it will create a new file and display this information on the screen with the word "New".
How to Edit a File in Linux? [SOLVED]

You can edit the file by pressing the Insert key on the keyboard.


How to Edit a File in Linux? [SOLVED]


Then press ESC to cancel editing. If you want to save :wq , to exit without saving :q! write the expressions.

How to Edit a File in Linux? [SOLVED]

Let's show you a few vim shortcuts:

dd  -   Delete entire current line
q/       -   search history in command buffer
i        -   Enter insert mode to the left of the cursor
a        -   Enter insert mode to the right of the cursor
  • CTRL+u - Move up by half a page
  • CTRL+d - Move down by half a page
  • CTRL+b - Move up by a page
  • CTRL+f - Move down by a page

Vi/vim is more than just a text editor. Some users use vim as an application development environment. So it has so many features. Check for more shortcut information.

 

Method-2: Using nano

Nano, on the other hand, is a simpler and plainer text editor. For installation(On Debian based OS):

foc@ubuntu22desktop:~$ sudo apt install nano -y

For Redhat Based OS:

foc@fedora:~$ sudo dns install nano -y

The file to be edited is typed after the nano command in the terminal. If it doesn't exist, it is created, if there is, you can start editing.
How to Edit a File in Linux? [SOLVED]

Here are some nano shortcuts:

Ctrl + X : Exit
Ctrl + W: Search
Ctrl + K : Cut line
Ctrl + U: Paste line

Check for more nano shortcuts.

 

Alternatives

Here are the application alternatives for both GUI and CLI:

 

Alternatives editor for GUI

  • Xed
  • Kate/Kwrite
  • Sublime Text Editor
  • Geany
  • Leaf Pad
  • Bluefish
  • Atom

 

Alternatives editor for CLI

  • ne
  • GNU Emacs
  • Tilde
  • Micro
  • Helix

 

What is NEXT

Show Line Numbers in Vim [Absolute, Relative, Hybrid]

 

Summary

We have shared text editors for both GUI and CLI. Habits are important when choosing a text editor. You can start using one of the text editors that we have shared and explained.

 

References

stackoverflow.com - How to edit a text file in my terminal

 

Omer Cakmak

Omer Cakmak

He is highly skilled at managing Debian, Ubuntu, CentOS, Oracle Linux, and Red Hat servers. Proficient in bash scripting, Ansible, and AWX central server management, he handles server operations on OpenStack, KVM, Proxmox, and VMware. You can connect with him on his LinkedIn profile.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

Leave a Comment