How to graphically edit text files on Linux with gedit

Linux users typically edit configuration files using terminal-based tools such as nano and vim . If you want to graphically edit a file, including a system file, you can use the gedit Text editor makes it painless and easy.

Files, files everywhere

An often repeated phrase in relation to Linux and other Unix-based operating systems such as macOS is “everything is a file”.

While not entirely accurate, text files are widely used for system logs and configuration. You can read these files to learn more about the inner workings of your operating system, and you can edit them to change how it behaves.

The default GNOME text editor is gedit , so you should find it on any system with a GNOME desktop environment. These include Ubuntu , Fedora, Debian , CentOS and Red Hat. It’s a handy tool for editing files when you just need a decent editor to get the job done – without the learning curve of some of the powerful editors like vim .

What does “everything is a file” mean in Linux?

Starting gedit

Begin gedit Enter on the command line gedit and hit Enter.

the gedit Text editor will appear shortly.

gedit text editor with empty document under Linux

It’s a tidy and clean application window. You can get on with the task of typing whatever you’re working on without the distraction.

Of course, you can also start gedit from the application menu on your Linux desktop. It is often referred to as a “text editor”. Just search the application menu for “gedit”.

Launch gedit from the application menu on the Ubuntu GNOME desktop

Start gedit as a background task

The terminal window is waiting gedit to close before returning to the command prompt. If you want to use the terminal window while gedit is still open, start gedit instead with this command. That opens up gedit as a background task. You will get the command line prompt back immediately and you will be able to continue using the terminal window even if gedit runs.

Type gedit , a space, an ampersand & , then press Enter-like this:

                      gedit &

Open an existing file

To open an existing text file, click the “Open” button in the gedit Toolbar. You can also press Ctrl + O to open a file.

This will open the recently used files menu. To reopen any of the files listed, click the name of the file. If you want to open another file, click the “Other documents …” button in the menu at the bottom.

Open file menu in gedit

This opens a standard dialog for opening files. You can use it to navigate to the location of the file you want to edit.

Click the green “Open” button when you have highlighted the file you want to edit.

Open gedit file dialog

Open a file from the command line

You can ask gedit to open a file as soon as it starts by specifying the file name on the command line. That makes gedit Download the file for immediate editing gedit appears.

                      gedit ana.c

The syntax highlighting feature of gedit makes it especially nice to edit program source code files and shell scripts.

Syntax highlighting highlights the words in the source file in color so that they can easily identify variables, reserved words, comments, parameters, and more.

gedit with ana.c file open for editing

The name of the file you are editing is displayed in the toolbar. If you changed the file, it will be an asterisk * appears next to the file name.

modified file in gedit

This will let you know that changes have been made to the contents of the file. It acts as a reminder that if you want to keep the changes, you must save the file.

Save changes to a file

To save your changes, click the “Save” button in the toolbar. You can also press Ctrl + S to save the file.

To save your file with a different name or location, click the menu button on the toolbar, then choose Save As from the menu.

Menu button with highlights

This will open a standard dialog for saving files. You can navigate to the directory where you want to save the file and you can provide a name for the file. Click the green Save button to save the file.

Save gedit file dialog

Editing of system files

To edit a system file, you usually have to use sudo because the owner of the file is likely root . To be precise, you can open a system file even when you’re not using it sudo , but you won’t be able to save changes to the file unless you’ve used sudo .

                      sudo gedit /etc/samba/smb.conf

warning : Do not edit system files if you are not sure what your changes will do to your system. If you mess up the wrong system file, you could be locked out of your computer after a restart.

This command opens gedit and loads the Samba configuration file for editing.

gedit edit smb.conf

Replicate ownership and permissions to a new file

One careful way of manipulating system files, and therefore a laudable way of manipulating system files, is to copy the file, and then edit the copy. When you have finished editing the new file, you can copy it back over the original file. If you edit the copied file in a confusing way, it won’t do any harm. Delete it and start over.

When you copy a file, the file ownership can change and the file mode permissions can be changed. You need to make sure these are exactly the same in your new file as they were in the original file before copying the new version over the original file. Here’s how you can do that.

Let’s say we want to edit that fstab File.

To make sure we change file ownership and mode permissions, we’ll create a new file and then copy the existing file over it. This step is for demonstration purposes only, to ensure that the new file does not have the same mode permissions and ownership as the original file. You do not need to do this if you are editing your own files.

                      touch new_fstab

We can use ls to check the file attributes and see what file mode permissions it has and who the file owner is.

                      ls -l new_fstab

The file owner is dave and the file mode permissions are read and write for the file owner and read-only for the group and others.

Now let’s copy them /etc/fstab File over the new file we just created. We then check the file attributes to see if they have changed.

                      sudo cp /etc/fstab new_fstab
                      ls -l new_fstab

the fstab was copied over new_fstab File. The file attributes of new_fstab have not changed. Let’s check the file attributes of the original fstab File.

                      ls -l /etc/fstab

As we can see is the owner root and the file mode permissions are different. The group permissions are read and write. The group permissions for new_fstab are read-only. We need to correct these two attributes before copying the file back.

First we start gedit and edit the new_fstab File to make the necessary changes.

                      gedit new_fstab

Now that we’ve edited the file and saved our changes, we need to reset the file ownership and file mode permissions back to the values ​​we want.

We can do this with the --reference Option of chmod and chown Commands.

the --reference Option takes a file name as a parameter. It forces chmod and chown to take the file mode permissions and file ownership from this file and copy them to the target file. We can then use ls to check whether the attributes of the edited file are set correctly before we copy them back over the original file.

                      sudo chmod --reference=/etc/fstab new_fstab
                      sudo chown --reference=/etc/fstab new_fstab
                      ls -l new_fstab

The file permissions and ownership are now correct. We can copy that new_fstab about the existing fstab and our changes will have been made.

Since these are changes to the fstab file, they will take effect the next time you restart your computer, or immediately if the mount command is used as follows:

                      sudo mount -a

Be careful out there

My motto is caution and I am not afraid to repeat warnings. If you are unsure how your changes to a system file will make your computer behave, do not make the changes.

If you need to edit a text file, whether it is a system file or not, you will find gedit is a fast and simple editor that won’t overwhelm you with too many options and still give you enough options to get your work done.

Related Posts