Linux Bible. Christopher Negus

Чтение книги онлайн.

Читать онлайн книгу Linux Bible - Christopher Negus страница 55

Linux Bible - Christopher Negus

Скачать книгу

(using telnet or ssh, for example)—no graphical interface is required. They each also contain tons of features, so you can continue to grow with them.

      The following sections provide a brief tutorial on the vi text editor, which you can use to manually edit a text file from any shell. It also describes the improved versions of vi called vim. (If vi doesn't suit you, see the sidebar “Exploring Other Text Editors” for further options.)

      The vi editor is difficult to learn at first, but after you know it, you never have to use a mouse or a function key—you can edit and move around quickly and efficiently within files just by using the keyboard.

      Exploring Other Text Editors

      Dozens of text editors are available for use with Linux. Some alternatives might be in your Linux distribution. You can try them out if you find vi to be too taxing. Here are some of the options:

       nano: This popular, streamlined text editor is used with many bootable Linux systems and other limited-space Linux environments. For example, nano is available to edit text files during a Gentoo Linux install process.

       gedit: The GNOME text editor runs on the desktop.

       jed: This screen-oriented editor was made for programmers. Using colors, jed can highlight code that you create so that you can easily read the code and spot syntax errors. Use the Alt key to select menus to manipulate your text.

       joe: The joe editor is similar to many PC text editors. Use control and arrow keys to move around. Press Ctrl+C to exit with no save or Ctrl+X to save and exit.

       kate: This nice-looking editor comes in the kdebase package. It has lots of bells and whistles, such as highlighting for different types of programming languages and controls for managing word wrap.

       kedit: This GUI-based text editor comes with the KDE desktop.

       mcedit: In this editor, function keys help you get around, save, copy, move, and delete text. Like jed and joe, mcedit is screen oriented. It comes in the mc package in RHEL and Fedora.

        nedit : This is an excellent programmer's editor. You need to install the optional nedit package to get this editor.

      If you use ssh to log in to other Linux computers on your network, you can use any available text editor to edit files. If you use ssh -X to connect to the remote system, a GUI-based editor pops up on your local screen. When no GUI is available, you need a text editor that runs in the shell, such as vi, jed, or joe.

      Starting with vi

      Most often, you start vi to open a particular file. For example, to open a file called /tmp/test, enter the following command:

       $ vi /tmp/test

      If this is a new file, you should see something similar to the following:

       □ ~ ~ ~ ~ ~ "/tmp/test" [New File]

      A blinking box at the top represents where your cursor is located. The bottom line keeps you informed about what is going on with your editing (here, you just opened a new file). In between, there are tildes (~) as filler because there is no text in the file yet. Now here's the intimidating part: There are no hints, menus, or icons to tell you what to do. To make it worse, you can't just start typing. If you do, the computer is likely to beep at you. (And some people complain that Linux isn't friendly.)

      First, you need to know the two main operating modes: command and input. The vi editor always starts in command mode. Before you can add or change text in the file, you have to type a command (one or two letters, sometimes preceded by an optional number) to tell vi what you want to do. Case is important, so use uppercase and lowercase exactly as shown in the examples!

      NOTE

      On Red Hat Enterprise Linux, Fedora, and other Linux distributions, for regular users the vi command is aliased to run vim. If you type alias vi, you should see alias vi='vim'. The first obvious difference between vi and vim is that any known text file type, such as HTML, C code, or a common configuration file, appears in color. The colors indicate the structure of the file. Other features of vim that are not in vi include features such as visual highlighting and split-screen mode. By default, the root user doesn't have vi aliased to vim. If vim is not on your system, try installing the vim-enhanced package.

      Adding text

      To get into input mode, type an input command letter. To begin, type any of the following letters. When you are finished inputting text, press the Esc key (sometimes twice) to return to command mode. Remember the Esc key!

       a: The add command. With this command, you can input text that starts to the right of the cursor.

       A: The add at end command. With this command, you can input text starting at the end of the current line.

       i: The insert command. With this command, you can input text that starts to the left of the cursor.

       I: The insert at beginning command. With this command, you can input text that starts at the beginning of the current line.

       o: The open below command. This command opens a line below the current line and puts you in insert mode.

       O: The open above command. This command opens a line above the current line and puts you in insert mode.

      TIP

      When you are in insert mode, -- INSERT -- appears at the bottom of the screen.

      Type a few words, and press Enter. Repeat that a few times until you have a few lines of text. When you're finished typing, press Esc to return to command mode. Now that you have a file with some text in it, try moving around in your text with the keys or letters described in the next section.

      TIP

      Remember the Esc key! It always places you back into command mode. Remember that sometimes you must press Esc twice. For example, if you type a colon (:) to go into ex mode, you must press Esc twice to return to command mode.

      Moving around in the text

      To move around in the text, you can use the up, down, right, and left arrows. However, many of the keys for moving around are right under your fingertips when they are in typing position:

       Arrow keys: Move the cursor up, down, left, or right in the file one character at a time. To move left and right, you can also use Backspace and the spacebar, respectively. If you prefer to keep your fingers on the keyboard, move the cursor with h (left),

Скачать книгу