CHAPTER 12. Using Linux Text Editors
Even though you are capable of using a desktop that has a functional graphic interface, you will find the desire to interact with the Linux environment by editing and creating files using editors that belong outside the GUI. Learning how to use text editors will allow you to make your own shell scripts and communicate with the programs that you want to run in your operating system. At the same time, you will also be able to fix possible problems in your configuration files, especially when the X Window System fails to load.
In this chapter, you will learn how to use GUI and text mode editors, which will both allow you to configure and create text files.
How to Use GUI Text Editors
The GUI desktops KDE and GNOME come with built-in text editors that have their own graphical user interfaces. You can load these editors from the main menu. For example, if you want to open the GUI text editor for GNOME, navigate to Applications->Text Editor and then select the file gedit. Once the editor loads, you can select the Open option found on the toolbar and then pull up a file that you want to edit or change directories that contain the file that you want in the dialog box labeled Open File.
GNOME’s text editor is also capable of loading multiple files at a time and even switch in between windows to work with them. A typical editing session will look like this:
Image from: Linux All-in-One for Dummies
Looking at this image, you will notice that there are two files loaded in the editor – one appears to be a new file, and the other is named motd. You can find the names of the files that are being edited in the tabs, which you can also click to switch windows.
If you open a file that is only available to be read, you will see a text that displays “Read Only” added to the filename displayed in the window title. If you have access to superuser privileges, you can of course change the ownership of the file in order for you to make edits.
If you have a KDE desktop, you can pull up the KWrite text editor by navigating to Applications -> Accessories -> Text Editor.
Just like in the GNOME text editor, you can click on the Open icon or navigate to File -> Open in order to load the file that you want to edit. This is how the KWrite editor looks like:
Image from: Linux All-in-One for Dummies
Using ed and vi as Text Editors
As you may have noticed, text editors that have GUIs allow you to edit files using your mouse and keyboard similar to how you would type a document in a word processor. However, text mode editors are more complex than that – you only have your keyboard as you input device and you will have to type in commands in order to perform tasks such as copying, pasting, or cutting texts.
Text editors that come native with Linux are called ed and vi.
ed
This line oriented editor is going to be extremely useful when you loaded a minimal version of the OS and you do not have the support that you need yet for a full-screen editor yet. You may encounter this situation when you load up Linux from a boot disk.
Using ed allows you to work in these modes:
-
Command mode – this is the default mode, in which everything that you type is being interpreted by Linux as a command. In this mode, ed has a rather simple set of commands, wherein each command is made up of one or multiple characters.
-
Text input mode – this mode is for typing longer texts. You can enter this mode when you enter the commands a (meaning append), c (meaning change), or i (meaning insert). When you are done entering several lines of text, you can leave this mode by putting only a period on an empty line.
To practice using ed as an editor, perform the following commands:
This will copy the file /etc/fstab in your home directory. To start editing this file, key in this command;
The editor will then respond to display this output:
From this example, the –p option allowed you to set the colon (the symbol :) as your prompt and also opened the fstab file copy that you have in your home directory. Each time the ed editor pulls up a file for editing, it displays the number of characters that are within the editable file and then displays the colon prompt that signals you can enter your commands through the editor.
Tip: when you want to edit using ed, see to it that you have turned on the prompt using the –p option. This will help you distinguish that you are in the text input mode and not in the command mode.
Once ed has opened up a file that you want to edit, you will immediately be in the last line of the file. To see what the current line number is, or the line where the command that you are going to input is going to be placed, you can use the .= command, which will appear like this:
From this result, you know now that the file fstab contains 9 lines. Now, if you want to see all the lines that are contained in the file, you can use the following command:
This will return with an output that appears like this:
If you want to go to a line number that you want to edit (line 2, for example), simply type in the line number on the prompt. The editor will then respond by displaying that particular line:
If, for example, you want to delete a line that contains the word cdrom, all you need to do is to search for that particular string. You can do this by typing the / sign, then the string that you need to find:
This will return with the line that contains the line that you want to edit, which becomes the current line. To delete it, simply enter d on the prompt.
To replace a specific string with a different one, the s command will be handy to use. For example, if you want to replace the string “cdrom” with “cd”, enter this command:
To input a line in front of the line that you are currently editing, use the following command:
From this point, you can enter as many lines that you want. If you are done typing, enter the period sign on an empty line to indicate that you are ending the text input mode. After doing so, you will see that ed switches back to the command mode.
If you want to save the changes that you have made to the file, enter the w command on the prompt. If you want to save the changes and exit the editor, key in wq on the prompt to perform both actions. The output will appear like this:
The editor will then save all changes that you have performed and then display the number of characters that were saved. Afterward, Linux will exit the editor. However, if you want to exit without saving any changes that you have made to the file, key in the q command to exit without writing to the file.
Of course, there are different other commands that you can use in ed. Here is a summary of the most common commands used in the ed editor:
Image from: Linux All-in-One for Dummies
vi
The editor vi is definitely easier to use compared to ed, although it is still considered as a command line editor. The vi allows you to use a text editor in a full screen mode, which means that you can view multiple lines at the same time. It also helps to know that most of the Unix systems (this includes Linux) come with this text editor; which means that once you understand how this editor works, you will be able to modify text files in any system that is based in Unix.
Note: When you edit a text file using vi, the editor reads it into a buffer memory. This means that you can change the file in the buffer. At the same time, this editor also makes use of temporary files during an edit session, which means that no changes are made in the original file unless you save any changes that you made.
To start editing with vi, key in vi followed by the filename:
This will allow vi to load the file, then display the first lines of the text file in to the screen. The cursor will also be positioned on the first line:
Image from: Linux All-in-One for Dummies
The last line that you see in this example shows the pathname, along with the number of lines and characters in the file. You will also notice that the file is read-only. This means that you are viewing the file as a normal user. You may have also noticed that since the number of lines does not occupy the rest of your screen, the unused lines are marked with the tilde (~) sign. The current line is marked by a black rectangle on top of the character that is being edited.
The vi editor allows you to enter the following modes:
-
Visual command – this is the default mode, wherein everything that you key in is considered by Linux as a command to be applied to the current line. All vi commands are the same as ed commands.
-
Colon command – this mode is set for writing or reading files, setting up options for vi, and exiting vi. As the name implies, all commands in this mode start with the colon. When you key in the colon symbol, the editor moves the cursor to the last line and then prompts you to enter the command. The editor will apply the command once you hit the Enter key.
-
Text input – this is the mode that you need to use when you want to enter text into the file. You are able to enter this mode when you use the following commands:
Once you are done typing in your text, press Esc to exit this mode and return to visual command.
Tip: It may be a bit difficult to tell what command mode you are in when you are using the vi editor. There may be circumstances that you have typed a long line of text only to realize that you are not in the text input mode, which can be a little frustrating. If you want to see to it that you are in command mode, press Esc a couple of times.
It is also helpful to know that you can make use of the arrow keys and some keyboard to move the cursor and the screen around. Try these commands out:
Image from: Linux All-in-One for Dummies
You can also jump to a specific line using the colon command. For example, if you want to switch immediately to line 6, just type 6 after the colon and then hit Enter:
Keep in mind that when you enter the colon symbol, the vi editor will display it at the last line of the screen. After doing so, vi will consider any text that you enter as a command.
To search for a particular string, key in the / symbol and then hit Enter. The / symbol will appear at the last line, which prompts you to enter the string that you want to search for. Once it is found, vi will position the cursor at the beginning of the matching entry in a line of the text. For example, if you want to search for the string “cdrom” in the /etc/fstab file, key in:
If you wish to delete the line where the cursor is placed, type the command dd. The editor will then delete that line and then change into the next line as the current line.
If you wish to enter text at the cursor, type the command i. The editor will change its mode to become text input, which will then allow you to type in your desired text. Once you are done, hit Esc. The vi editor will revert to visual command afterward.
Once you are done modifying the file, you can save changes that you have made by entering the :w command. To save the file and exit the editor, enter the :wq command. Alternatively, you can also perform save and exit at the same time by holding down the Shift key and then hitting Z twice.
To exit the editor without saving, enter the :q! command.
Here are other commands that are commonly used by the vi editor:
Image from: Linux All-in-One for Dummies