Full width home advertisement

Post Page Advertisement [Top]

 How to edit text files graphically on Linux with gedit

 

Linux users typically edit configuration files with terminal-based tools as nano well vim. Editing a file graphically - even a system file - gedit text editor makes it painless and easy.

Files, files everywhere

A recurring phrase about Linux and other Unix-based operating systems such as macOS is "everything is a file."

Although not strictly accurate, text files are often used for system logs and configurations. You can read these files to learn more about the internal functions of the operating system, and you can edit them to change the behavior.

The default GNOME text editor is gedit, so you should find it on any system with a GNOME desktop environment. It includes Ubuntu, Fedora, Debian, CentOS and Red Hat. It is a useful tool for editing files when all you need is just enough editors to get the job done - without the learning curve for any of the power-house editors vim.

IN FAMILY: What does "Everything is a file" mean in Linux?

Launch of gedit

To start gedit Enter the command line gedit and press Enter.

The gedit text editors will appear soon.

It is a neat and clean application window. You can continue with the task of writing down what you are working on without distractions.

Of course, you can also launch gedit from the Linux desktop application menu. It is often called "Text Editor." Just search the application menu for "gedit."

 

Launch of gedit as background task

The terminal window will wait gedit to close before returning you to the command prompt. If you want to use the terminal window while gedit still open, launch gedit with this command instead. This opens gedit as a background task. You will receive the prompt message immediately, and you can continue to use the terminal window even when gedit running.

Type gedit, a room, an amperand &, and 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 opens the menu for newer files. To reopen one of the listed files, click the name of the file. If you want to open another file, click the "Other Documents ..." button at the bottom of the menu.

This opens a standard file open dialog. You can use this to scroll to the location of the file you want to edit.

Click the green "Open" button after selecting the file you want to edit.

Open a file from the command line

You can ask gedit to open a file as soon as it launches by entering the file name on the command line. This will gedit load the file so that it is ready to be edited as soon as it gedit is displayed.

gedit ana.c

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

Syntax that marks colors the words in the source file so that variables, reserved words, comments, parameters and more can be easily identified.

The name of the file you are editing appears on the toolbar. If you have modified the file, an asterisk *will appear next to the file name.

This lets you know that changes have been made to the contents of the file. It serves 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 on 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 and then select "Save As" from the menu.

This opens a standard dialog box for saving files. You can browse to the directory you want to save the file in, and you can enter a name for the file. Click the green "Save" button to save the file.

Editing system files

To edit a system file, you usually need to use sudo because it is likely to be the owner of the file root. To be strictly accurate, you will be able to open a system file even if you do not use it sudo, but you will not be able to save changes back to the file unless you have used it sudo.

sudo gedit /etc/samba/smb.conf

Warning : Do not edit system files if you do not know exactly what your changes will do to your system. Send the wrong system file and you may find yourself locked out of your computer after a reboot.

This command opens geditand loads the samba config file for editing.

Replicate ownership and permissions to a new file

A careful way to edit system files - and therefore a commendable way to edit system files - is to copy the file and then edit the copy. When you have finished editing the new file, you can copy it over the original file. If you make a mess of editing the copied file, no harm will be done. Delete it and start again.

When you copy a file, the file ownership can be changed and the movie mode permissions can be changed. You must make sure that these are exactly the same on the new file as in the original file before copying the new version over the original file. Here's how to do it.

Let's say we want to edit fstabfile.

To ensure that we have a change of file ownership and permissions mode, we create a new file and 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 when editing your own files.

touch new_fstab

We can use it lsto check the file attributes and see which movie mode permissions it has and who the file owner is.

ls -l new_fstab

 

The file owner is deaf, and the movie mode permissions are read and written for the file owner and read-only for the group and for others.

We are now copying the /etc/fstabfile 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

They fstabhave been copied over new_fstabfile. The file attributes of new_fstabhave not changed. Let's check the file attributes of the original fstabfile.

ls -l /etc/fstab

As we can see, the owner rootand the permissions for movie mode are different. The group permits are read and written. The group permissions for new_fstabare read-only. We need to correct these two attributes before copying the file back.

First we launch geditand edit the new_fstabfile to make the necessary changes.

gedit new_fstab

Once we have edited the file and saved our changes, we need to return the file ownership and movie mode rights to what they should be.

We can do this by using the --reference option chmod and chown commands.

The --reference option takes a filename as a parameter. It forces chmod and chown to take permissions and file filmoduseierverdier from the file and to copy them to the destination file. We can then use ls to check that the attributes of the edited file are set correctly before copying it 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 new_fstab over the existing fstab and our changes will have been made.

 

As these are changes to the fstab file, they will take effect when the computer was restarted, or immediately if the mount command was used as follows:

sudo mount -a

Be careful out there

My watchword is careful, and I do not exceed warnings. If you are not at all sure how the changes to a system file will cause your computer to behave, do not make the changes.

When you need to edit a text file, whether it is a system file or not, you will find that it geditis a fast and easy editor that does not bother you with too many options and still has enough capacity to let you get the job done.

 


No comments:

Post a Comment

Bottom Ad [Post Page]