![]()
Linux for Newbies: Part 7
Using Linux Text Editors
by Gene Wilburn
(The Computer Paper, February 2000. Copyright © Wilburn Communications Ltd. All rights reserved)
As you've undoubtedly noticed by now, Linux thrives on text files. From configuration files to web pages to program source code, text is king. No matter what else, you absolutely have to learn to use a Unix text editor to get anywhere with the operating system.
The joy of text files is that they're portable, viewable, searchable, and easily modified. They can be commented, allowing you to document your scripts and configuration files in ways that will mean something when you revisit them in six months. Text is the Unix Way. No troublesome Microsoft registry files here. Just pure text. Revel in it.
To assist you in your reveling are a broad spectrum of text viewers and editors. Viewers (or pagers as they're also known) provide you a safe way to examine files. You can't edit with them, so you can't do any inadvertent harm such as inserting a character or two accidentally. I recommend that you start with these. The classic text viewer is More. To view a file with more, you simply type more myfile.txt, e.g.'s:
# more /etc/lilo.conf # more /etc/inetd.conf # more /etc/rc.d/rc.localYou will find a version of More on every Unix system. An enhanced variant of More is the GNU Less program. This is a typical Unix naming joke because Less is more than More, more or less.
Both programs display a screenful of text at a time, allowing you to scroll forward and backward. You advance a page forward by pressing the spacebar or by typing f for forward. Typing b will scroll back a page at a time. You can search the contents of a file by pressing a forward slash / and entering a search pattern. Both programs will load several files at once if you use a wildcard, e.g., more /etc/*.conf. To get to the next file, press a colon : followed by n for next. Type q to quit either program. To find out more about More or Less, type man more and man less.
If you were ever a DOS user, you may remember Norton Commander. It was a versatile, elegant character-menu combo tool that you could use for browsing, editing, renaming, and moving files. If you liked Norton Commander, you'll be right at home with a nifty open-source clone called Midnight Commander. It's on most Linux distributions as mc. Go to a command line prompt and try it out. You get a split screen with panels left and right that you can control independently. The function keys provide easy access to the view, edit and other facilities. Midnight Commander even allows you to inspect the innards of RPM files. The program makes an excellent viewer and simple editor.
Simple Text Editors
Unix has a reputation for hard-to-learn text editors (we'll visit those in a minute) but there are a number of easy editors you can use. The text editor within Midnight Commander, for example, is very DOS like in its operation and it can be invoked independently. You can call it directly from the command line, e.g.:
# mcedit /etc/lilo.confMany a Linux newbie has been thankful to find this editor on the system. Another option: did you ever use Wordstar, or one of the Wordstar-derived editors such as the ones integrated into Turbo C and Turbo Pascal? Meet Joe (Joe's Own Editor). You might have to install this from your /RedHat/RPMS CD-ROM directory since it is rarely installed by default. As with the original Wordstar, Joe has an option for displaying help information while you're editing. To install and use joe type the following after you've inserted your Red Hat CD-ROM (note: this series is based on Red Hat Linux 6.0):
# mount /dev/cdrom /mnt/cdrom # cd /mnt/cdrom/RedHat/RPMS # rpm -Uhv joe*rpm # cd / # umount /mnt/cdrom # joe /etc/lilo.confThen type Ctrl-K H to display the help menu.
Another pleasant, easy-to-use editor is Pico, which comes with the text-based mail reader Pine. Most Linux systems have Pine installed so Pico is available for your use. As with Joe, it presents help menus, making it very easy to learn. The only thing to watch with Pico is that, by default, it wraps lines of text. This is a good thing in email, but it's a bad thing in configuration files. Hence you should invoke Pico with the "-w" flag to prevent line wrap, e.g.:
# pico -w /etc/lilo.confBlack-Belt Editors
Because Unix is based on text files, Unix editors and utilities have been designed to manipulate text in uncommonly powerful ways. Hence it is worth your while, if you want to move beyond a beginner's level, to start working with these tools. You will want to learn how to use grep and its variants (man grep) and at least one of the classic Unix text editors: Vi or Emacs. Of the two, I'd recommend Vi as your first conquest. Vi (pronounced VEE-EYE) is the most widely available editor for Unix--you will find a version of it on every Unix system you encounter, from Linux to Solaris to FreeBSD.
The Vi editor, originally created by Bill Joy at Berkeley, has more of the lean, classic Unix feel than Emacs. When you start up Vi it's fast and light. But learning it takes a fair bit of patience. What makes Vi confusing is that it's modal: it has a command mode and an edit mode. You "shift gears" by tapping the ESC key.
Here's a very brief demo on how to use Vi. The boldface items are command you type. Try this on a practice file.
# vi mytest.txt i Line one Line two Skipped a line Skipped another Last line [ESC Key] :x[ENTER Key]You've just created your first file with Vi. The "i" is the "insert" command and the "ESC : x" combination takes you out of edit mode, and into command line prompting, allowing you to type the save and exit command "x".
There are many Vi tutorials on the Net where you can go to learn more commands. Vi is also covered in most books on Unix and Linux. The Vi command set is very rich and a modern Vi such as Vim offers macro record and playback, color syntax hilighting, and split-screen display. Vim is the default Vi for Red Hat Linux.
There's a new Linux Vim-HOWTO with a Vi tutorial at www.linuxdoc.org/HOWTO/Vim-HOWTO.html and you can locate other online tutorials by typing vi tutorial in your favorite search engine.
Despite the oddball keystrokes, once you develop a comfort level with Vi, you'll discover that it is quite efficient. You never have to take your hands out of typing position. And Vi is a life skill--you can take Vi anywhere. You can even baffle your Windows friends by installing the Windows version of Vim on your other HD partition and enjoy watching their faces while you edit a file. This also gives you a chance to practice Vi while you're in a Windows session.
Emacs, in contrast, is a very large, swiss-army knife program--a powerful editor that can also be used to read email, newsgroups, and even surf the web. It was written by Richard M. Stallman, the person who started the Free Software Foundation (GNU Project) to which Linux owes much of its existence.
Emacs is powerful because it was designed to be extensible through a built-in programming language--LISP. The "macs" in Emacs stands for macros.
As with Vi, Emacs can be used at the command line, but it's a much nicer editor when you invoke it under X. In the X environment, Emacs sprouts menus and mouse support, taking the sting out of learning its complex keystrokes.
Emacs is my editor of choice for programming, writing and designing web pages. With features such as auto-indent, color syntax hilighting, compile options, bracket matching, HTML syntax verification, spell checking, multiple file buffering, unlimited un-do's, split-screen editing, and revision control built into the editor, it provides me with all my serious editing needs.
I typically use Vi to edit small files such as configuration files, and Emacs for larger files such as articles, programs and web pages.
If you find yourself struggling with Vi and Emacs and decide that neither of them is for you, there's a newer editor available that offers a more Windows-like feel. It's called Nedit and you can obtain it from the Red Hat ftp site (ftp.redhat.com) in the /pub/contrib/libc6/i386 directory as nedit-5.02-2.i386.rpm. Also pick up all the lesstif-current* files and install them first. When you're logged in as root, go to the directory where you saved the files and type:
# rpm -Uhv lesstif* # rpm -Uhv nedit*Then log in as yourself you can type nedit & while you're in X and enjoy using Nedit.
There many more editors available for Linux than the ones I've touched on here and in the course of your travels you may find one you like even better than these. Vi and Emacs are the classics. Learning them requires discipline and determination. Most of the advanced admin features of Linux also require discipline and determination, so learning these editors is a good practice for your continued development as a mid- to advanced-level Linux techie.
Next time: PPP connect to the Internet.
Gene Wilburn (gene@wilburn.ca) is a Toronto-based IT manager, musician and writer who operates a small farm of Linux servers.
-30-