Working with Text


Text editors are among the most commonand essentialapplications. You'll need to know how to edit text to accomplish just about any of the tasks described throughout the rest of this book, and it's a good idea to find a text editor that you like as early as possible in your FreeBSD career.

Unlike a word processor, a text editor simply works on plain-text files (which, in FreeBSD, can be denoted with any kind of filename, with any kind of extensionnot just .txt). It does not have the ability to store font changes, styles, margins, or any other such information in the document because plain-text files don't support such formatting information. Plain-text files make up a great deal of the FreeBSD system, from configuration files to documentation; you'll be working with them all the time. Here are some common uses for a text editor:

  • Editing system configuration files FreeBSD, like most other versions of UNIX, relies heavily on text-based configuration files to control system behavior. To edit these files, you will need a text editor that can write a plain-text file. Word-processing applications, such as KWord or OpenOffice, are overkill for such basic functionsaside from the fact that they require a graphical desktop environment such as KDE or GNOME, they also tend to save files in formats other than plain text, which makes them unreadable to the system when it needs to launch applications or boot. (The system configuration files and how to interact with them will be covered in Chapter 14, "System Configuration and Startup Scripts.")

  • Creating or modifying programming source code You will use a text editor to write the code that tells the computer what you want it to do. Although in many languages this code will be converted to machine language by a program called a compiler, your original instructions are still written in plain text. These instructions are called source code. Chapter 10, "Shell Programming," and Chapter 11, "Introduction to Perl Programming," introduce two popular programming languages in FreeBSD in which you would use a text editor to write the code.

  • Creating or modifying web pages Web pages are written in HTML, which stands for HyperText Markup Language. Although there are many GUI-based, WYSIWYG (What You See Is What You Get) programs available for designing web pages, the end result is still a plain-text file with formatting control tags that the web browser understands. Many people still choose to write web pages by hand in a plain-text editor because it gives them total control over all the features of HTML. In addition, you would use a text editor to write PHP code, JavaScript, and other such extended features of web design that allow you to give your pages interactive content.

  • Sophisticated typesetting Some very feature-rich typesetting languages are available for FreeBSD. Like HTML, typesetting languages generally use plain-text files with special formatting tags in them to control the layout and appearance of text. Although the word processor has largely replaced these languages (the word processor is easier to work with, and it shows you what the output will actually look like), there are still situations in which you might want or need to use a typesetting language for a particular task. TeX and its extended macro package LaTeX might interest you if you are a scientist or engineer because of their extremely sophisticated mathematical equation-formatting capabilities.

In the following sections, we'll look in detail at some of the built-in text editing solutions available in FreeBSD, namely the ee and vi editors. After that, we'll look at a couple of more modern graphical text editors that are part of the KDE or GNOME user environments.

Using ee (Easy Editor)

ee (Easy Editor) is installed by default as part of FreeBSD. As its name indicates, the Easy Editor is a basic text editor that is designed to be easier to work with than some other traditional UNIX text editors.

ee can be invoked either by simply typing ee at the command line or by typing ee followed by the name of a file you want to edit. Figure 6.2 shows a sample ee session with a blank document.

Figure 6.2. A sample ee session. You can use the key combinations shown at the top of the screen to access various features.


Using ee Key Combinations

The caret (^) in each of the listed key commands means the Ctrl key. Most of the options in the menu are self-explanatory, but there are a few that deserve greater discussion. Table 6.1 shows these options and their meanings.

Table 6.1. Ctrl Key Options in ee

Key Combination

Action

Ctrl+O

Invokes a prompt where you can enter an ASCII value. Useful for inserting special characters that do not exist on the keyboard and require their ASCII values to be entered directly.

Ctrl+C

Changes the top menu and also invokes a prompt at the bottom of the screen where you can type one of the commands listed at the top. Simply press Enter to leave the prompt without entering a command.

Ctrl+Y

Invokes a search prompt. Here, you can type an expression to search for in the file. When you press Enter, the first occurrence of the expression after the current location of the cursor will be found.

Ctrl+X

Repeats the previous search done with Ctrl+Y, causing ee to find the next occurrence of the expression.

Ctrl+G and Ctrl+V

As the menu says, these will move forward and backward one page at a time. You can also use the Page Up and Page Down keys on your keyboard to accomplish the same thing. Ctrl+G and Ctrl+V exist in case you are on a terminal that does not have Page Up and Page Down keys.


Setup and Configuration of ee

If a file called .init.ee exists in your home directory, ee will read the configuration options in this file each time it starts. You can create this file by hand, or you can choose setup options from the ee setup options list. To access this list, press Esc within ee to open the main menu, shown in Figure 6.3. Press E to select the Settings menu or use the down arrow to highlight the Settings option in the menu and then press Enter to open the modes menu (see Figure 6.4).

Figure 6.3. The main menu in ee.


Figure 6.4. If you choose the Settings option from the ee main menu, you open this list of setup options.


In the Modes menu, shown in Figure 6.4, you can toggle an option on or off by pressing the letter corresponding to the option, or by using the arrow keys to highlight the desired option and then pressing Enter.

Table 6.2 shows the various setup options and what they do.

Table 6.2. The ee Setup Options

Option

Description

Tabs to Spaces

Off by default. This converts hard tabs into the equivalent number of spaces. It is useful for some programming languages where whitespace is significant (for example, Python and FORTRAN).

Margins Observed

Off by default. When this option is on, ee wraps at the right margin and starts a new line automatically. When it is off, wrapping does not occur unless a new line is inserted manually.

Info Window

On by default. When this option is on, the top part of the screen shows the window with the listing of key commands. When it is off, this window is not displayed. You should probably leave this option on until you are familiar with the various key commands.

Right Margin

The column at which the right margin is set. By default, it is set for the width of a standard 80-column terminal display.

16 Bit Characters

This controls how 16-bit characters are handled (whether they are handled as two 8-bit characters or one 16-bit character). You shouldn't have to worry about this option unless you are using a character set that uses sixteen bits per character, such as Unicode.


The final option in the menu, Save Editor Configuration, writes the configuration information to the file .init.ee. When you choose to save the configuration information, you will be asked whether you want to write the configuration file to the current directory or to your home directory. If you write it to your home directory, the configuration will be the default configuration for all files you create and open in ee. If you write it to the current directory, the settings will override the default settings from the configuration file in the home directory any time ee is started from the directory you're in.

The vi Editor

vi was one of the first editors developed for UNIX-like operating systems. To this day, it is still one of the most powerful editors available, and it comes standard on virtually every UNIX-like operating system. It has also been ported to several non-UNIX systems, including Microsoft Windows and OS/2. Unfortunately, vi also has a reputation among newbies for being notoriously arcane and difficult to learn. Unlike in editors such as ee, you can't just navigate with the arrow keys to a point in the file and start typingyou have to know certain kinds of editing modes and key combinations to accomplish the tasks you're used to in more modern editors.

So why learn vi? There are two primary reasons, really. The first is that you can be virtually guaranteed that vi will be available on any UNIX-like system you might ever have to work on. If you find yourself working on an unfamiliar Linux or UNIX system in the future, and you need to edit a text file, vi might be the only editor you will have available.

The second reason for learning vi is that once you have learned its various keystrokes and commands, you'll find it to be a very powerful editor. If you are a touch typist, it is also an extremely fast way to workit's designed so that you don't have to remove your hands from the home keys to access most vi commands.

Tip

A third reason to learn vi is that its key command structure crops up in lots of unexpected places throughout FreeBSD. For example, if you're using less to page through a file, you can use vi's search commands to navigate it and look for particular text strings. You'll also find that vi uses many of the same conventions as appear in regular expressions, such as $ for the end of a line. This will help keep your mind tuned to the proper mode if you program in Perl or other languages that use regular expressions.


To start vi with an empty file, type vi. To start vi and load a file for editing, type vi followed by the filename. If the filename you specify doesn't already exist, vi will assume that this is a new file. Figure 6.5 shows a vi session with a new file ready for editing.

Figure 6.5. The vi editor, ready to edit a new file. The status line at the bottom of the screen gives information about the file being edited; note the temp file location.


Looks rather plain and boring, doesn't it? One of the first things you may notice if you start trying to type text is that vi does nothing except beep at you; it doesn't insert any of the text you are typing. You may also hit certain keys and see strange-looking messages on the status line at the bottom of the screen. This is because vi has different modes of operation, and you don't start out in one where typing does any good.

When vi first starts, it is in command mode, in which keypresses are interpreted as commands to the editor rather than as text to be inserted into the document. To switch into the mode in which you can enter text into the document, you must press the A, I, or O key, or use Shift to invoke their alternative command meanings, depending on where you want the text to appear in the document:

  • The a command stands for append. In this mode, the text you type will be inserted after whatever character the cursor is currently on.

  • The A command (Shift+A) puts the cursor at the end of the current line and lets you begin entering additional text.

  • The I command stands for insert. In this mode, the text you type will be inserted before whatever character the cursor is currently on.

  • The o command stands for open. This will cause a new line to be inserted after the line the cursor is currently on. Then, the cursor will move to the new line, and vi will enter insert mode, allowing you to enter text on the new line.

  • The O command (Shift+O) is like o, except that it puts a blank line above the line the cursor is on and lets you insert text into it.

Note

In vi, key commands are differentiated on the basis of capitalization because they're intended to be combined with other keypresses in sequence to create a complex command that can be written out (for example, 75G). Thus, for example, when you see a reference to "the q command", it means to press Q by itself; but a reference to "the Q command" means for you to press Shift+Q. These two commands have very different meanings!


If you want to get back into command mode after entering text-entry mode, press the Esc key. By default, vi will beep when entering command mode.

Tip

Remember that if you are trying to enter text and vi simply keeps beeping at you or doing other unexpected things, you are probably in command mode. Press A, I, or O to enter textentry mode. Also, if you are ever unsure what mode you are in, simply press Esc to move into command mode (if you are already in command mode, it will have no effect). You can then press A, I, or O to enter text-entry mode. If you mistakenly pressed keys in command mode and caused errors in your document, you can often press U to undo them.


Moving Around in vi

In text-entry mode, you can usually use the arrow keys and the Page Up/Page Down keys to move around. However, vi is designed with specific editing functions in mind, not the ad-hoc freedom you might be used to in more WYSIWYG-style text editors. This means there are many other movement keys available in command mode than just the arrow keys; they're all described in Table 6.3.

Table 6.3. The Movement Keys Available from vi's Command Mode

Command

Action

h

Moves the cursor left one character

j

Moves the cursor down one character

k

Moves the cursor up one character

l

Moves the cursor right one character

w

Moves the cursor forward one word

b

Moves the cursor back one word

e

Moves the cursor to the end of the next word

0

Moves to the beginning of the line

$

Moves to the end of the line

)

Moves to the beginning of the next sentence

(

Moves to the beginning of the previous sentence

}

Moves to the beginning of the next paragraph

{

Moves to the beginning of the previous paragraph

G

Moves to the bottom of the current document

^

Moves to the first character in the line that is not a space

H

Moves the cursor to the first line on the screen

L

Moves the cursor to the last line on the screen


Note that each of the commands in the table (with the exception of G) uses the unit of one, by default. The command j moves the cursor down one line, k moves it up one line, w moves one word to the right, and so on. You can modify the default behavior by typing a number before the command. For example, the following line causes the cursor to move down five lines instead of one line:

5j


The following line causes the cursor to move to the 75th line in the current file that is being edited:

75G


The following line causes the cursor to move to the line that is five lines up from the bottom of the screen:

5L


This syntax works for all of the commands in Table 6.3, except for the ^ command, which moves to the first non-space character in the document.

Tip

If you enter a number followed by one of the previous commands, and vi seems to do nothing except beep, it probably means you entered a number out of the viable range. For example, if you enter 560G to move to line 560, and the document has only 557 lines, vi will simply beep at you, indicating that it can't perform the requested function. Unfortunately, no error message will be printed telling you what exactly is wrong.


Other Movement Keys

In addition to the cursor-movement keys described previously, there are also some key combinations that are related to scrolling the screen (Table 6.4 lists them).

Table 6.4. Scrolling in vi

Key Combination

Action

Z then Enter

Moves the line the cursor is on to the top of the screen

Z then -

Moves the line the cursor is on to the bottom of the screen

Z then .

Moves the line the cursor is on to the center of the screen

Ctrl+U

Scrolls up one-half screen

Ctrl+D

Scrolls down one-half screen

Ctrl+F

Scrolls forward one full screen

Ctrl+B

Scrolls backward one full screen

Ctrl+E

Scrolls down one line

Ctrl+Y

Scrolls up one line


Text-Editing Commands

Modifying existing text is where using vi gets particularly tricky. The Backspace and Delete keys will not do what you might expect. Instead, you will have to use various keystrokes from vi's command mode in order to delete text in the manner to which you're accustomed or to overwrite existing text. Table 6.5 lists the various text-editing commands available in vi.

Table 6.5. Commands for Text Editing in vi

Command

Action

D

Deletes the text from the cursor position to the end of the line.

dd

Deletes the entire current line.

ndd

Here, n is the number of lines you want to delete. For example, 5dd will delete the current line, as well as the next four lines.

p

Pastes the contents of the memory buffer (which might have been "cut" to there with a deletion command or copied with a "yank" command, which is discussed a little later).

rc

Here, c is a character. This will replace the character under the cursor with the character that follows r.

R

The text typed after R will overwrite the current text, starting at the cursor position until you press Esc to get back into command mode.

S

Deletes the current line and begins inserting text in the now-blank line.

x

Deletes the character under the cursor and moves the character to the right over to close the gap.

X

Deletes the character before the cursor and moves the characters on the right to close the gap.

~

Changes the case of the letter under the cursor.

J

Joins the current line with the previous line and removes the resulting blank line.


File Operations and Exiting vi

Several vi commands relate to loading and saving files. Table 6.6 shows the various command sequences available for these actions.

Table 6.6. File Operations in vi

Command

Action

ZZ

Saves changes to the current file and then exits.

:wq

Saves changes to the current file and then exits (the same as ZZ).

:w

Saves changes to the current file.

:w!

Saves changes to the current file, overwriting a file of the same name if it already exists.

:q

Quits vi. If there are unsaved changes, vi complains and does not quit.

:q!

Quits vi even if there are unsaved changes. All unsaved changes will be lost.

:e filename

Loads the specified file into vi for editing. If the specified file does not exist, a new file will be created.

:e!

Loses all changes and reloads the saved file from the disk.


Caution

Note that the ! option on the end of several of the commands in Table 6.6 is a stronger variant of another command without the !. Usually it means to suppress warnings and take whatever action you're requesting without complaint. For example, :q! will exit vi immediately without asking first whether you want to save any changes. Use the ! option with care.


Searching and Replacing Text in vi

Several commands are available for performing a search-and-replace operation in vi. Table 6.7 lists these commands and their actions.

Table 6.7. Search and Replace Commands in vi

Command

Action

/pattern

Here, pattern is what to search for. vi searches forward in the file for the first occurrence of the specified pattern.

/ or n

Repeats the last search, finding the next occurrence of the pattern in the file.

?pattern

Here, pattern is what to search for. vi searches backward in the file for the first occurrence of the specified pattern.

?

Repeats the last search, finding the previous occurrence of the pattern in the file.

%

Moves to the matching parenthesis or brace for the one that the cursor is currently on. This is useful to programmers.

:s/pattern1/pattern2

Replaces each occurrence of pattern1 with pattern2 on the current line.

:%s/pattern1/pattern2

Replaces every occurrence of pattern1 with pattern2 in the entire file.


Tip

Many of these commands are similar to how the less pager worksyou can use vi search commands when reading system documentation with less.


This table doesn't include a few other search-and-replace functions that aren't often used. The operations listed in the table are the ones you will probably use most often.

Copying, Cutting, and Pasting Text in vi

To copy text to a buffer in vi, use the y command. This command, in popular vi parlance, "yanks" text into the buffer. Table 6.8 shows the various ways the Y command can be used.

Table 6.8. yank Commands in vi

Command

Action

yw

Yanks the word that the cursor is currently on into the buffer.

y$

Yanks from the current cursor position up to the end of the current line into the buffer.

yy

Yanks the entire current line into the buffer.


After you have yanked text into the buffer, you can paste it anywhere in the document by moving to the appropriate location and then using p or P to "put" the text. The p command places the text into the document after the cursor, whereas P places the text into the document before the cursor. After you have "put" the text, it continues to remain in the buffer. You can use p or P again to copy the text to another location in the document.

If you want to cut instead of copy, use one of the deletion commands. For example, 5dd will cut the current line as well as the next four lines, as well as copying them into the buffer. They can than be pasted into the document in another location by using p or P.

Caution

vi stores only the last text that was yanked or deleted. In other words, if you use dd to delete a line of text, and you later use yy to copy a line of text, the text from the dd operation will be overwritten in the buffer with the text from the yy operation. This means the text from the dd operation will no longer be available, nor will you be able to undo the delete.


vi has many more powerful features that cannot be covered here without making this into a vi textbook. When you get to know vi, you'll recognize it as a very powerful editor that offers a fast and reliable way to edit documents. If you would like to learn more about vi, a few books are available on the subject, including Learning the vi Editor and vi Editor Pocket Reference, both available from O'Reilly.

Editing Text Graphically in KEdit or gedit

KDE provides a fully functional text editor, similar to Notepad in Windows, called KEdit. This program can be found in the Editors submenu of the K menu. Similarly, GNOME provides a Notepad-style text editor called gedit, found in the Accessories submenu of the Applications menu, and labeled simply Text Editor. gedit is shown in Figure 6.6.

Figure 6.6. gedit, the built-in text editor in GNOME. Its equivalent in KDE is KEdit.


Operating either of these text editors is very similar to working with Notepad in Windows: you use the File menu to perform operations such as opening and saving files, and you use the Edit menu (and its associated keyboard shortcuts) for operations such as copying and pasting text you select with the mouse. As shown in Figure 6.6, these editors even provide functionality that you won't have seen in Notepadgedit, for example, automatically renders parts of recognizable program files (such as shell scripts) with boldface and differently colored text, even though this special formatting isn't saved in the plain text of the file. This formatting is of special importance to programmers, for whom having variables shown in one style of text and flow-control statements in another is very helpful.

Note

If you're running KDE or GNOME as a non-root user (as you should be), you'll find that you can't directly edit system configuration files that are owned by root. In KDE you can get around this by opening up the Options pane of the Run Command dialog box and turning on the Run as a Different User check box, where you can specify root's username and password so you can edit the file with root privileges (which apply only to that KEdit process). GNOME doesn't have this functionality, however; you have to either run GNOME as root (which is dangerous), or simply save your edited files in a location where you have write access, and then use su in a terminal window to assume root privileges and move the edited file back into its proper location. The inconvenience of this workflow is why even KDE or GNOME users often prefer to use CLI-based text editors to work on system configuration files.





FreeBSD 6 Unleashed
FreeBSD 6 Unleashed
ISBN: 0672328755
EAN: 2147483647
Year: 2006
Pages: 355
Authors: Brian Tiemann

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