Bookmark this page

Editing Text Files

Objectives

  • Use command-line and graphical editors to modify text files.

Linux Text Editors

Red Hat Enterprise Linux includes many text editors in its software repositories, which you can use to view and modify text files, including plain text files. Plain text files do not contain a font formatting, special styles, or a file header.

Examples of plain text files include configuration files or source code. In some operating systems, plain text files are identified with the .txt extension. In Linux, you do not need to specify an extension to the file name to identify it as a text file.

Graphical Text Editors

Modern graphical text editors for Linux bridge the gap between the needs of programmers and everyday users. These applications offer a range of functionality, such as clean interfaces and advanced functions that are needed for programming. A graphical text editor that includes these advanced functions is usually known as an Integrated Development Environment (IDE).

An IDE is a software application that helps programmers develop software code. IDEs provide syntax validation and intelligent auto-completion, which are important for catching errors that are at best annoying and at worst harmful. IDEs also provide integration with the rest of the system so that you can test scripts as you write them. If something goes wrong, a good IDE has a debugger to help identify the problem and, in some cases, resolve it. Other features include plugins, Git integration, quick access to a shell, and more.

Editing Text Files with gedit

The official text editor of the GNOME desktop environment is gedit.

Note

The gedit text editor is the default editor in Red Hat Enterprise Linux 9, but the development project is no longer active. The GNOME text editor is a popular editor and the default editor in Fedora. The two text editors have similar interfaces and functionalities, and you might use them interchangeably.

To open gedit, click Activities in the upper left of your desktop (or press the Super key). In the search field, type gedit, and then click Text Editor.

Figure 3.3: The gedit editor

Similar to other text editors, gedit provides basic features such as saving files, printing files, finding and replacing text, and exiting. In addition to these common functions, you can change the appearance or behavior of gedit through the Preferences menu.

To access the Preferences menu, click the application's main menu in the upper right, and then select Preferences.

Figure 3.4: The gedit editor menu

Like many IDEs, gedit provides syntax highlighting for various markup, programming, and scientific languages. To change the syntax or language, click the current syntax that is displayed at the bottom of the main window, and then select the desired syntax from the type-ahead list.

Figure 3.5: Selecting a syntax in gedit

Other code-oriented functions include line numbering, bracket matching, text wrapping, highlighting the current line, automatic indentation, and automatic file backup.

Additionally, gedit includes multi-language spell checking and a flexible plug-in system that enables you to add new functions. Many plugins come with gedit, and more plugins are available in the gedit-plugins package.

Command-line Text Editors

Linux system administrators often edit text files by using the command line. Command-line editors are essential for servers, which do not have a graphical desktop environment installed. The following command-line text editors are commonly used:

nano

nano is a small text editor that implements features such as opening multiple files, scrolling per line, undoing and redoing actions, syntax coloring, line numbering, and smoothing lines that are too long.

vim

Vim (Vi IMproved) is a text editor that can edit various types of plain text files, and is especially useful for editing programs. Vim has many improvements over Vi, such as multilevel undo features, multiple windows and buffers, syntax highlighting, command-line editing, file name completion, online help, visual selection, and more.

Editing Text Files with nano

To open an existing file, or to create a file, use the nano command with a file name as the argument:

[user@host ~]$ nano nano.txt

This command opens the editor window where you can view and edit your file.

There are four main sections of the editor:

The nano text editor

1

The titlebar shows the program version, the name of the file that is being edited, and whether the file has changed.

2

The edit window shows the file content that is being edited.

3

The status bar shows important messages.

4

The help lines show commonly used command shortcuts in the editor.

The most common commands appear in the bottom section of the main window, but many more shortcuts exist.

The nano text editor does not use the Shift key. All shortcuts use lowercase letters and unmodified numeric keys.

For example, to see the list of shortcuts, press Ctrl+G, and the window displays the following screen:

Figure 3.6: Help text in nano

Note

The caret symbol (^) indicates that a key combination includes the Ctrl key. The letter M appears when the combination includes the Super key.

For example, the Ctrl+\ key combination is a shortcut for the search and replace functionality. To complete a search and replace, the nano text editor prompts for the string to search, the new string, and the option to replace a single instance or all instances of the string.

The nano text editor is available on many Linux distributions. However, nano does have its limitations compared to more robust editors like Vim or Emacs. For example, nano has a smaller feature set and it is harder to work on more than one document at a time.

Editing Text Files with Vim

To open an existing file, or to create a file, use the vim command with a file name as the argument:

[user@host ~]$ vim vim.txt
Figure 3.7: The Vim text editor

The Vim editor offers several editing modes that determine how the editor works. The two basic modes are: Command (or Normal) mode and Insert mode.

When you open the editor, you enter Command mode. To switch to Insert mode, press i and start editing the file. To return to Command mode, press Esc.

Figure 3.8: Switching modes in Vim

In Command mode, you can run common editing and navigational commands, such as saving files and exiting the program. You can accomplish common tasks by using the following commands:

  • The :w command writes and saves the file.

  • The :q command quits the editor.

  • The :w <filename> command saves the file with the specified name.

  • The :q! command quits without saving the changes to the file.

Vim has a built-in tutor that you can access by entering vimtutor on the command line:

[user@host ~]$ vimtutor
Figure 3.9: Vim tutor

Vim tutor has lessons on specific topics at a range of levels, from beginner to advanced. This tool is highly recommended for beginners.

References

nano(1), vim(1), and gedit(1) man pages

For more information about text editors in Red Hat Enterprise Linux, see the Which Text Editors are Available in the Official Red Hat Repositories? solution at https://access.redhat.com/solutions/5750011

Revision: rh104-9.1-3d1f2bc