Using Text Editors


Linux is a testament to the continued power and relevance of plain ol' text files. Virtually all configuration information is stored in text files, and many programs are really just scripts in text files. Learn how to edit text files effectively, and you're well on your way to becoming a mighty Linux (and Knoppix) guru!

Editing Configuration Files on the Command Line with vim

Want to start a discussion that will quickly become a debate and eventually escalate into an argument? With all the innocence you can muster, ask a bunch of Linux users, "Which is better? Emacs or vi?" and then step back and watch the fireworks. It's one of the oldest debates in the *nix world, and Knoppix comes down squarely in the middle: It includes both text editors, leaving the choice up to the user.

We believe that vi (actually vim, or "vi improved") is the better of the two editors, and will explore it here. If you want to learn about emacs, there's plenty of information on the Web and in a lot of books. You should still learn vim, though, because while you can't be sure that emacs is on every single *nix machine you'll run into, you can be certain that vi (and probably vim) will be installed. Learn vi, and you increase the likelihood that you can fix a machine by changing config files.

Knoppix includes both console and GUI-based versions of vim. This section covers only the console version, for a couple of reasons. First, if you learn to use vim on the console, moving to the GUI is a piece of cake. Second, the GUI may not always be available, but the console will.

Suppose that you need to edit your hosts file so that you can more easily ssh to a couple of the boxes on your LAN. Open Konsole (it's the black icon on the panel) and run cp /etc/hosts to copy the file to your home directory. Now type vim hosts and press Enter, and the newly copied hosts file opens (see Figure 1-24).

image from book
Figure 1-24: Editing a copy of the /etc/hosts file with vim.

If you've never used vim (or even emacs) before, you're probably a bit confused at this point. What do you do? Why doesn't anything appear when you start typing? How do you save? Or quit?

The first thing to understand is that vim operates in one of several different modes at a time. When you first open vim, it's in command mode, in which you use your keyboard to move the cursor around, delete information, cut, copy, and paste, and run commands that affect your file (because this is a console app, you can't use your mouse). You want to position the cursor so that you can enter some data. To do so, use the arrow keys on your keyboard (because Knoppix supports them), or use the following key commands:

  • h: Moves the cursor one character to the left.

  • j: Moves the cursor one character down.

  • k: Moves the cursor one character up.

  • l: Moves the cursor one character right.

To learn more about vim and all of its commands, just type vim on the command line, and then : help to enter vim's built-in help system (enter :q to get out of help, of course), or look on the Web by searching for "vim tutorial," "vim help," "vim tips," or ".vimrc" if you want to find sample vim config files that people have posted, which can teach you a lot.

Position the cursor at the end of the first line and press a. You're now in the insert mode, in which you can insert characters and text into your file. The letter a (append) indicates that you want to append text after the character your cursor is on, while the letter i (for insert) means that you want to begin inserting before the character your cursor is on (again, you can use other commands to signify precisely where you want to begin placing text into the file).

For this example, press Enter so you're on a new line and then enter three new lines into the hosts file, pressing Enter after each one to start the next:

 192.168.0.10     dante 192.168.0.15     chaucer 192.168.1.20     virgil 

You know you should save your work often, but you can't do that while you're inserting text. The solution? When you're in insert mode, press Esc to return to command mode. Now enter :w (colon, w) to write out (save) the file. To write and immediately quit (exit), use :wq (colon, w, q); to quit without saving your work, use :q! (colon, q, exclamation point).

For this example, there are a couple of problems in what you just entered, and here's how to fix them. Chaucer is temporarily offline, so it doesn't need to be in the hosts file. You're still in command mode, so maneuver the cursor (using the arrow keys or key commands) until it's somewhere in the Chaucer line. To delete a line, enter dd while in command mode; the entire line disappears.

The second problem is a typo in the line about Virgil: The IP address is 192.168.0.20, not 192.168.1.20. That's easy to correct: Position the cursor over the 1, press x to delete just that character, press i to enter insert mode, type a 0, press Esc to go back into command mode, and finish with :wq, which writes the file out and quits vim.

Vim really isn't that difficult to work with — it just takes some getting used to. Once you're comfortable with the basics, it's easy to keep expanding your vim knowledge base, with scripts and macros and regular expressions and all sorts of other fun, powerful stuff. The sooner you can do just a little bit with vim, the sooner you're on your way to using Knoppix more effectively.

Editing Configuration Files Using the GUI Editor Kate

At last count, there were more than 7,000 text editors available for Linux. OK, that's an exaggeration though not much of one. Knoppix includes a few text editors, and they serve their purpose. Probably the easiest-to-use-right-out-of-the-box GUI text editor on the Knoppix disk is Kate. Kate is powerful and officially targeted at developers, although anyone can use it.

From the K menu, select Editors Kate. Kate opens, as shown in Figure 1-25.

image from book
Figure 1-25: Kate, the text editor, ready to edit text

If you're reading this book, you've used a text editor sometime in your life, even if it was just Notepad. Kate is very easy to use for simple tasks because it's pretty much like every other text editor in the world, but it contains a lot of special features that make it appropriate for more sophisticated work as well.

There's only space to point out a few of Kate's features here. The best way to learn more about Kate is to go through all the menus, playing with the options and trying things out.

Ever wanted to view different parts of the same file at the same time? Select View Split Vertical, which enables you to do just that. To return to the single display of a file, select View Close Current View.

Need to run a command against the file you're working on, but don't want to switch to Konsole? Click the Terminal button at the bottom of the window; a resizable console frame appears, already targeted at the directory in which your file resides. Perfect for a quick wc -w file.txt (to find out the number of words in your file) or to run ifconfig to grab your machine's IP address and paste it into your file. To close the console, click the Terminal button again.

Finally, do you need to directly open a file on another machine accessible only via SSH, and find that downloading, editing, and uploading it again would be too much trouble? No problem. Kate supports KDE's built-in networking transparency, so select File Open, and in the Location box enter fish://username@other.computer.com (you can use an IP address instead of the domain name, if you want). You may have to accept the machine's SSH key if this is the first time you've connected to it (answer in the affirmative if asked). Enter a password when you're prompted, and boom! You're connected to that machine over a secure connection. Select the file you need, click Open, edit it, save it, close it, and you're done. You just remotely edited a file as though it were sitting right on your machine. Pretty cool, eh?

Note 

Kate, like any KDE app supporting the built-in libraries providing network transparency, can work with files using a variety of protocols, including FTP, SFTP, SMB, NFS, and WebDAV. For a quick overview of KDE's network transparency, see http://www.osdir.com/Article2159.phtml. For more information on some of these protocols and their usage in Knoppix, take a look at Chapter 3.



Hacking Knoppix
Hacking Knoppix (ExtremeTech)
ISBN: 0764597841
EAN: 2147483647
Year: 2007
Pages: 118

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net