Using the Vi Editor and Other Linux Text Editors

Using the Vi Editor and Other Linux Text Editors
Page content

Linux Text Editors Vs. Word Processors

The average Personal Computer (PC) user tends to use a word processor for writing letters, reports, and papers which is fine. These applications allow them to “process” text by controlling things such as font size, color, and style. They can highlight, italicize, bold, and underline text, insert graphics and tables among other options. However, when you code in a mark-up, scripting or programming language such as Extensible Hypertext Mark-Up Language (XHTML) or Hypertext Preprocessor (PHP), you’ll want to use a text editor. Why? Because your code must be true text. If you look closely at things like quotation marks in a word processor, you’ll see that they usually appear different from how they’re made in a text editor. These “little” things can literally wreck source code. There are many Linux text editors, probably because it used to be an operating system used primarily by programmers, but this is no longer the case.

Although most word processors can save text files containing code, they weren’t designed for this type of work. Also, a text editor offers features that facilitate the tedious work of application development such as automatic highlighting. Programmers can do things like set file type and encoding, and even compile a program - tasks that aren’t related to word processing. In the screenshot below, you can see some of these choices in the Geany text editor under Puppy Linux.

A Choice of Editors

There is a very wide selection of Linux text editors from which to choose for almost any distribution (flavor of Linux). Some will be in the default installation, others will only be seen in a particular desktop environment, while still others can only be obtained from the repository for your specific distro. Linux text editors vary greatly in features and some have a much steeper learning curve than others. It’s a good idea to work with a few different ones until you settle on a suitable choice for all or most of the tasks you do.

Generally, even a distro of very small file size offers a good selection of Linux text editors from which to choose. In addition to the vi editor, I have the Geany, Leafpad, MP and e3 console editors on my Puppy system. Puppy is a small distribution. You might have also heard of the more well known emacs and vi editors. Keep in mind that some Linux text editors are console-based (such as the vi editor) while others have a GUI. The following are all Linux text editors:

emacs - This is much more than a text editor. Richard Stallman’s emacs editor can be used as a calendar and appointment book, to send e-mail, play games, and more.

mcedit - People who like DOS-type editors will like working with mcedit.

joe - Known as Joe’s Own Editor, it can be used to emulate some other editors.

vi - This one was written by Bill Joy. There’s also the vim (Vi IMproved) version available on practically every Linux and Unix system.

gedit - This is a Graphical User Interface (GUI) editor available in the GNOME desktop environment.

kedit - This is also a GUI editor available in the K desktop environment (KDE).

Among Linux text editors that aren’t always embraced is the vi editor. While it’s true that the commands to work in it are cryptic, it’s not very difficult to use on the basic level. The good news is that you can accomplish a lot of important tasks with nothing but the basics. I strongly suggest learning the bare bones of vi editor commands because of all of the Linux text editors out there, this is the only one I’ve seen available on every distro with which I’ve worked. You can pretty much count on the presence of the vi editor on any Linux, Unix, or Unix-based system.

There are times when you might have to edit configuration files to get a device working or change the way something works in a distro and you might not have access to a GUI. I have to work with configuration files when setting up Fedora distros above Fedora Core 4 to allow the installation discs to become a local repository that doesn’t need an Internet connection. If you’re familiar with only GUI-based editors or a console-based one other than vi, you might find it more difficult to edit text on a system that only has the vi editor.

Basic Vi Editor Commands

An important note to keep in mind when learning basic vi editor commands is that not all of the commands will work on every system, especially if it is a small distro. For example, the vi editor under Puppy Linux can’t process the commands that it can under a system like Fedora, Ubuntu or Debian. In Puppy, you have a very scaled down system. Nevertheless, you can still accomplish much by knowing the basics.

To open a text file in the vi editor you simply type “vi” (without the quotation marks) plus the full name of the file. It will open in the command or viewing mode by default. You can’t begin entering or editing text in this mode. Simply press “i” to enter the insert mode and begin typing. You can move up, down, to the right and left by pressing the arrow keys or “k,” “j,” “l,” and “h” respectively. When you are ready to save, you have to re-enter command mode by pressing the escape key, typing “:” and “w.” You would type “wq” if you wanted to save and quit and “q!” if you wanted to quit without saving. These few and simple vi editor commands will allow you to edit configuration files for important tasks.

The commands that can be used with the vi editor are numerous. Whether you want to learn them or not, beginning with these basic ones is a good start. The screen shot below shows how I created an empty, sample text file called test.txt and opened it the vi editor. You can also see how I typed in some text, saved and quit. Learning how to work with Linux text editors is a very good idea if you’re going to run a Linux distro.