Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 34 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
34
Dung lượng
513,46 KB
Nội dung
Chapter 9. Editors, Text Tools, Graphics, and Printing 262 Text that is deleted may be reinserted using the p command (for "put"). Pressing p now will return the deleted line to the buffer after the current line. Using P (uppercase) instead will insert the text before the current line. By default, p and P insert text from the "undo buffer"; you can also yank and replace text from other buffers, as we'll see later. The u command undoes the latest change (in this case, pressing u after dd is equivalent to p). If you inserted a large amount of text using the i command, pressing u immediately after returning to command mode would undo it. To delete the word beneath the cursor, use the dw command. Place the cursor on the word Diet and type dw: 9.1.4 Changing Text You can replace text using the R command, which overwrites the text beginning at the cursor. Place the cursor on the first letter in pizza , press R, and type: The r command replaces the single character under the cursor. r does not place you in insert mode per se, so there is no reason to use Escape to return to command mode. The ~ command changes the case of the letter under the cursor from upper- to lowercase, and vice versa. If you place the cursor on the o in Now in the previous example, and repeatedly press ~, you end up with the following: Another useful command for changing words is the cw command, which lets you simply type in the new word and — after pressing Escape — removes anything that might be left over from the original word. If the new text is longer than the one being changed, the space is automatically expanded as needed. Chapter 9. Editors, Text Tools, Graphics, and Printing 263 9.1.5 Moving Commands You already know how to use the arrow keys to move around the document. In addition, the w command moves the cursor to the beginning of the next word, and b moves it to the beginning of the current word. The 0 (that's a zero) command moves the cursor to the beginning of the current line, and the $ command moves it to the end of the line. When editing large files, you'll want to move forward or backward through the file one screen at a time. Pressing Ctrl-F moves the cursor one screen forward, and Ctrl-B moves it one screen backward. In order to move the cursor to the end of the file, type G. You can also move to an arbitrary line: the command 10G would move the cursor to line 10 in the file. To move to the beginning of the file, use 1G. Typing / followed by a pattern and the Enter key causes you to jump to the first occurrence of that pattern in the text following the cursor. For example, placing the cursor on the first line of text in our example and typing /burg moves the cursor to the beginning of the word "burgers." Using ? instead of / searches backward through the file. The pattern following a / or ? command is actually a regular expression. Regular expressions are a powerful way to specify patterns for search and replace operations and are used by many Unix utilities. You can find more information about regular expressions in the upcoming section, Section 9.2.9. Using regular expressions, you could, for example, search for the next uppercase letter, using the command: /[A-Z] Therefore, if the pattern you're searching for is not a static string, regular expressions can be used to specify just what you want. You can couple moving commands with other commands, such as deletion. For example, the command d$ will delete everything from the cursor to the end of the line; dG will delete everything from the cursor to the end of the file. 9.1.6 Saving Files and Quitting vi Most of the commands dealing with files within vi are invoked from ex mode. You enter ex mode when you press the : key from command mode. This places the cursor on the last line of the display, allowing you to enter various extended commands. For example, to write the file being edited, use the command :w . Typing : causes you to enter ex mode, and typing w followed by the Enter key completes the command. The command :wq writes the file and exits vi. (The command ZZ — from command mode, without the ":" — is similar to :wq , but checks first if the file has been changed, and writes it only in this case.) Chapter 9. Editors, Text Tools, Graphics, and Printing 264 To quit vi without saving changes to the file, use the command :q! . Using :q alone will quit vi, but only if modifications to the file have been saved. The ! in :q! means to quit vi — and that you really mean it. 9.1.7 Editing Another File To edit another file, use the :e command. For example, to stop editing test, and edit the file foo instead, use the command shown at the bottom of the following box: If you use :e without writing the file first, you'll get the error message: No write since last change (:edit! overrides) At this point, you can use :w to save the original file, and then use :e , or you can use the command :e! foo, which tells vi to edit the new file without saving changes to the original. This can be useful if you edit a file and realize that you have screwed up. You can then use the :e! command; if you don't specify a filename, vi discards the changes and re-edits the current file. 9.1.8 Including Other Files If you use the :r command, you can include the contents of another file in the vi buffer. For example, the command: :r foo.txt inserts the contents of the file foo.txt after the current line. 9.1.9 Running Shell Commands The :! command allows you to enter the name of a command, which is executed within vi. For example, the command: :!ls -F executes the ls command and displays the results on your screen. The :r! command is similar to :!, but includes the standard output of the command in the buffer. The command: :r!ls -F produces the following: Chapter 9. Editors, Text Tools, Graphics, and Printing 265 If you need to execute a series of shell commands, it's often easier to use the suspend key (usually Ctrl-Z), provided you're using a shell that supports job control, such as tcsh or bash. 9.1.10 Global Searching and Replacing There are many more features of vi than are documented here; most of these features are implemented through combinations of the simple features we've seen. Here are one or two other tidbits most vi users find useful. The command: :[x,y]s/pattern/replacement/flags searches for pattern between lines x and y in the buffer, and replaces instances of pattern with the replacement text. pattern is a regular expression; replacement is literal text but can contain several special characters to refer to elements in the original pattern . The following command replaces the first occurrence of weeble with wobble on lines 1 through 10, inclusive: :1,10s/weeble/wobble Instead of giving line-number specification, you can use the % symbol to refer to the entire file. Other special symbols can be used in place of x and y . $ refers to the last line of the file. Leave x or y blank to refer to the current line. Among the flags you can use are g to replace all instances of pattern on each line, and c to ask for confirmation for each replacement. In most instances, you will want to use the g flag, unless you want to replace only the first occurrence of pattern on each line. You can also use marks to refer to lines. Marks are just single-letter names that are given to cursor locations within the document. Moving the cursor to a location in the file and typing ma will set the mark a at that point. (Marks may be named any of the letters a-z or A-Z.) You can move the cursor directly to the mark a with the command ` a (with a backquote). Using a regular single quote (as in ' a ) will move the cursor to the beginning of the line that the mark a is on. Marks allow you to "remember" cursor locations that denote a region of text. For example, if you want to search and replace a block of text, you can move the cursor to the beginning of the text, set a mark, move the cursor to the end of the text, and use the command: :'a,.s/weeble/wobble/ where 'a refers to the line containing mark a, and . refers to the current line. Chapter 9. Editors, Text Tools, Graphics, and Printing 266 9.1.11 Moving Text and Using Registers One way to copy and move text is to delete it (using the d or dd commands) and then replace it with the P command, as described earlier. For example, if you want to delete 10 lines, starting with the line that contains your cursor, and paste them somewhere else, just use the command 10dd (to delete 10 lines), move the cursor to the new location for the text, and type p. You can copy text in this way as well: typing 10dd followed by P (at the same cursor location) deletes the text and immediately replaces it. You can then paste the text elsewhere by moving the cursor and using p multiple times. Similar to dd is the yy command, which "yanks" text without deleting it. You use p to paste the yanked text as with dd . But note that each yank operation will delete the previously yanked text from the "clipboard." The deletion and yank commands can be used on more general regions than lines. Recall that the d command deletes text through a move command; for example, d$ deletes text from the cursor to the end of the line. Similarly, y$ yanks text from the cursor to the end of the line. Let's say you want to yank (or delete) a region of text. This can be done with marks as well. Move the cursor to the beginning of the text to be yanked and set a mark, as in ma . Move the cursor to the end of the text to be yanked and use the command y'a. This yanks text from the cursor position to the mark a . (Remember that the command 'a moves the cursor to the mark a.) Using d instead of y deletes the text from the cursor to the mark. The most convenient way to cut, copy, and paste portions of text within vi is to use registers. A register is just a named temporary storage space for text you wish to copy between locations, cut and paste within the document, and so forth. Registers are given single letter names; any of the characters a - z or A - Z are valid. The " command (a quotation mark) specifies a register; it is followed by the name of the register, as in "a for register a . The lowercase letters and their uppercase counterparts refer to the same registers: using the lowercase letter overwrites the previous contents of the register and using the uppercase letter appends to it. For instance, if we move the cursor to the first line in our example: and use the command "ayy, the current line is yanked into the register a. We can then move the cursor to the second line, and use the command "ap to paste the text from register a after the current line: Chapter 9. Editors, Text Tools, Graphics, and Printing 267 Similarly, the command "ay'a yanks text from the cursor to mark a into register a . Note that there is no correspondence between mark and register names! Using registers allows you to copy text between files. Just copy the text to a register, use the :e command to edit a new file, and paste the text from the register. 9.1.12 Extending vi vi is extensible in many ways. Most of the commands we've introduced can be generalized to arbitrary regions of text. As we've already seen, commands such as d and y operate on the text from the cursor to a move operation, such as $ or G. (dG deletes text from the cursor to the end of the file.) Many other commands operate on text through a move command in the same way. Using marks you can operate on any region of text. As we mentioned before, vi is just a text editor; it doesn't have facilities for spell checking text, compiling programs, and other such features. However, vi executes other programs that you can use to extend the editor. The command: :x,y!command executes the named command with the text on lines x through y as standard input, and replaces the lines with the standard output of the command. As with the s (search and replace) command, other specifications, such as % and $, can be used for the line numbers. For example, let's say you want to prepend a quote character ( >) to all the lines in a region of text. One way to do this is to write a short shell or Perl script (see Section 1.5.4) that reads lines of input and outputs those same lines with the quote character prepended. (Or use a sed command; there are many alternatives.) You can then send lines of text through this filter, which replaces them with the quoted text within vi. If the script is called quote, just use a command, such as: :`a,.!quote which quotes the region of text between the cursor location and the mark a. Be familiar with the various ex commands that are available. The :set command allows you to set various options; for example, :set ai turns on auto indentation of text. (:set noai turns it off.) You can specify ex commands (such as :set) to execute when starting up vi in the file .exrc in your home directory. (The name of this file can be changed with the EXINIT environment variable.) For example, your .exrc file might contain: Chapter 9. Editors, Text Tools, Graphics, and Printing 268 set ai to turn on autoindentation. You don't need the : before ex commands in this file. A number of good tutorials and references for vi are available — both online as well as in print. Learning the vi Editor is a good place to look for more information. If you have Internet access, the comp.editors archives for vi contain a number of reference and tutorial documents, as well as interesting vi hacks. ftp://alf.uib.no:/pub/vi is the archive home site; it is mirrored at cs.uwp.edu and elsewhere. The home of vim on the Web is http://www.vim.org. 9.2 The Emacs Editor Text editors are among the most important applications in the Unix world. They are used so often that many people spend more time within an editor than anywhere else on their Unix system. The same holds true for Linux. The choice of an editor can be a religious one. Many editors exist, but the Unix community has arranged itself into two major groups: the Emacs camp and the vi camp. Because of vi's somewhat nonintuitive user interface, many people (newcomers and seasoned users alike) prefer Emacs over vi. However, long-time users of vi (and single-finger typists) use it more efficiently than a more complex editor such as Emacs. If vi is one end of the text-editor spectrum, Emacs is the other; they are widely different in their design and philosophy. Emacs is partly the brainchild of Richard Stallman, founder of the Free Software Foundation and author of much of the GNU software. Emacs is a very large system with more features than any single Unix application to date (some people would even go so far as not to call it an editor but an "integrated environment"). It contains its own LISP language engine that you can use to write extensions for the editor. (Many of the functions within Emacs are written in Emacs LISP.) Emacs includes extensions for everything from compiling and debugging programs to reading and sending electronic mail to X Window System support and more. Emacs also includes its own online tutorial and documentation. The book Learning GNU Emacs by Debra Cameron, Bill Rosenblatt, and Eric Raymond (O'Reilly) is a popular guide to the editor. Most Linux distributions include two variants of Emacs. GNU Emacs is the original version, which is still being developed, but development seems to have slowed down. XEmacs is larger, but much more user-friendly and better integrated with the X Window System (even though you can also use it from the command line, despite its name). If you are not tight on memory and have a reasonably fast computer, we suggest using XEmacs. Another advantage of XEmacs is that many useful packages that you would need to download and install separately with GNU Emacs are already shipped with XEmacs. We will not cover the differences here, though; the discussion in this section applies to both. Whenever we talk about Emacs in this section, we mean either version. 9.2.1 Firing It Up GNU Emacs is simply invoked as: $ emacs options Chapter 9. Editors, Text Tools, Graphics, and Printing 269 Likewise, XEmacs is invoked as: $ xemacs options Most of the time, you don't need options. You can specify filenames on the command line, but it's more straightforward to read them in after starting the program. In Emacs lingo, C-x means Ctrl-X, and M-p is equivalent to Alt-P. As you might guess, C- M-p means Ctrl-Alt-P. Using these conventions, press C-x followed by C-f to read in a file or create a new one. The keystrokes display a prompt at the bottom of your screen showing your current working directory. You can create a buffer now to hold what will end up being the content of a new file; let's call the file wibble.txt. We now see the following: The mode line at the bottom indicates the name of the file as well as the type of buffer you're in (which here is Fundamental). Emacs supports many kinds of editing modes; Fundamental is the default for plain-text files, but other modes exist for editing C and T E X source, modifying directories, and so on. Each mode has certain key bindings and commands associated with it, as we'll see soon. Emacs typically determines the mode of the buffer based on the filename extension. To the right of the buffer type is the word All , which means that you are currently looking at the entire file (which is empty). Typically, you will see a percentage, which represents how far into the file you are. If you're running Emacs under the X Window System, a new window will be created for the editor with a menu bar at the top, scrollbars, and other goodies. In Section 11.6.2 in Chapter 11, we discuss Emacs's special features when used within X. 9.2.2 Simple Editing Commands Emacs is more straightforward than vi when it comes to basic text editing. The arrow keys should move the cursor around the buffer; if they don't (in case Emacs is not configured for your terminal), use the keys C-p (previous line), C-n (next line), C-f (forward character), and C-b (backward character). Chapter 9. Editors, Text Tools, Graphics, and Printing 270 If you find using the Alt key uncomfortable, press Escape and then p . Pressing and releasing Escape is equivalent to holding down Alt. Already we must take the first aside on our tour of Emacs. Literally every command and key within Emacs is customizable. That is, with a "default" Emacs configuration, C-p maps to the internal function previous-line, which moves the cursor (also called "point") to the previous line. However, you can easily rebind different keys to these functions, or write new functions and bind keys to them, and so forth. Unless otherwise stated, the keys we introduce here work for the default Emacs configuration. Later we'll show you how to customize the keys for your own use. Back to editing: using the arrow keys or one of the equivalents moves the cursor around the current buffer. Just start typing text, and it is inserted at the current cursor location. Pressing the Backspace or Delete key should delete text at the cursor. If it doesn't, we'll show how to fix it in Section 9.2.8 later in this chapter. Now begin to type: The keys C-a and C-e move the cursor to the beginning and end of the current line, respectively. C-v moves forward a page; M-v moves back a page. There are many more basic editing commands, but we'll allow the Emacs online documentation (discussed shortly) to fill those in. In order to get out of Emacs, use the command C-x C-c. This is the first of the extended commands we've seen; many Emacs commands require several keys. C-x alone is a "prefix" to other keys. In this case, pressing C-x followed by C-c quits Emacs, first asking for confirmation if you want to quit without saving changes to the buffer. You can use C-x C-s to save the current file, and C-x C-f to "find" another file to edit. For example, typing C-x C-f presents you with a prompt, such as: Find file: /home/loomer/mdw/ where the current directory is displayed. After this, type the name of the file to find. Pressing the Tab key will do filename completion similar to that used in bash and tcsh. For example, entering: Find file: /home/loomer/mdw/ .bash and pressing Tab opens another buffer, showing all possible completions, as so: Chapter 9. Editors, Text Tools, Graphics, and Printing 271 After you complete the filename, the *Completions* buffer goes away and the new file is displayed for editing. This is one example of how Emacs uses temporary buffers to present information. Emacs allows you to use multiple buffers when editing text; each buffer may contain a different file you're editing. When you load a file with C-x C-f , a new buffer is created to edit the file, but the original buffer isn't deleted. You can switch to another buffer using the C-x b command, which asks you for the name of the buffer (usually the name of the file within the buffer). For example, pressing C-x b presents the prompt: Switch to buffer: (default wibble.txt) The default buffer is the previous one visited. Press Enter to switch to the default buffer, or type another buffer name. Using C-x C-b will present a buffer list (in a buffer of its own), as so: Popping up the buffer menu splits the Emacs screen into two "windows," which you can switch between using C-x o. More than two concurrent windows are possible as well. In order to view just one window at a time, switch to the appropriate one and press C-x 1 . This hides all the other windows, but you can switch to them later using the C-x b command just described. Using C-x k actually deletes a buffer from Emacs's memory. [...]... 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 \documentclass{letter} \address{ 755 Chmod Way \\ Apt 0x7F \\ Pipeline, N.M 099 15} \signature{Boomer Petway} \begin{document} \begin{letter}{O'Reilly and Associates, Inc \\ 10 05 Gravenstein Highway North \\ Sebastopol, C.A 954 72} \opening{Dear Mr O'Reilly,} I would like to comment on the \LaTeX\ example as presented in Chapter~9 of {\em Running Linux} ... requirement for doing office-type work on Linux: they can import Microsoft Word documents quite well While you may well decide, as a new Linux enthusiast, that you won't accept documents sent to you in proprietary formats, sometimes they come from your boss, and you can't refuse to read them just because you are running Linux In this case, it is good to know that Linuxbased solutions are available The... we'll discuss some of the most popular text processing systems for Linux: TEX, SGML, groff, and Texinfo 9.3.1 Word Processors One obvious sign that Linux has come of age is that many popular WYSIWYG word processing systems are available for it today For some time, it had even been rumored that Microsoft was going to port its office suite to Linux, but this is not likely to happen A Microsoft suite is not... machine in various ways Enter the following source with your text editor, and save the result as coffee.man: 290 Chapter 9 Editors, Text Tools, Graphics, and Printing 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 TH COFFEE 1 "23 March 94" SH NAME coffee \- Control remote coffee machine SH SYNOPSIS \fBcoffee\fP [ -h | -b ] [ -t \fItype\fP ] \fIamount\fP SH DESCRIPTION... place next time 5 Press C-x ) to end the macro 6 Press C-x e repeatedly to capitalize the following lines Or press C-u several times, followed by C-x e The repeated uses of C-u are prefix keys, causing the following command to execute many times If you get to the end of the document while the macro is still executing, no harm is done; Emacs just beeps and stops executing the macro 9.2.7 Running Commands... some of Ghostscript's fonts are less than optimal, Ghostscript does allow you to use Adobe fonts (which you can obtain for Windows and use with Ghostscript under Linux) Ghostscript also provides an SVGA preview mode you can use if you're not running X At any rate, after you manage to format and print the example letter, it should end up looking something like that in Figure 9-3 Figure 9-3 Sample output... SGML opens a whole new world of tools and techniques A good starting point for getting inspired and reading up on this is the web site of the Linux Documentation Project, which, as mentioned before, uses SGML/DocBook for all its documentation You'll find the Linux Documentation Project at http://www.tlpd.org 9.3.4 groff Parallel to and independent to TEX, another major text processing system emerged... LyX/KLyX does not know how to display some of the powerful formatting features that TEX provides, so if you are a power TEX user, this isn't for you LyX/KLyX isn't part of most Linux distributions; to try it you will have to get it from a Linux archive 9.3.2 TEX and LATEX TEX is a professional text processing system for all kinds of documents, articles, and books — especially those that contain a great deal... 15 on ISO standard coffee machines .SS Options TP \fB-h\fP Brew hot coffee Cold is the default .TP \fB-b\fP Burn coffee Especially useful when executing \fIcoffee\fP on behalf of your boss .TP \fB-t \fItype\fR Specify the type of coffee to brew, where \fItype\fP is one of \fBcolombian\fP, \fBregular\fP, or \fBdecaf\fP .SH FILES TP \fI/dev/cf0\fR The remote coffee machine device SH "SEE ALSO" milk (5) ,... the type of coffee to brew, where type is one of colombian, regular, or decaf 292 Chapter 9 Editors, Text Tools, Graphics, and Printing FILES /dev/cf0 The remote coffee machine device SEE ALSO milk (5) , sugar (5) BUGS May require exhausted human intervention if coffee supply is As mentioned before, groff is capable of producing other types of output Using the -Tps option in place of -Tascii produces PostScript . automatically expanded as needed. Chapter 9. Editors, Text Tools, Graphics, and Printing 263 9.1 .5 Moving Commands You already know how to use the arrow keys to move around the document. In addition,. the command: :r foo.txt inserts the contents of the file foo.txt after the current line. 9.1.9 Running Shell Commands The :! command allows you to enter the name of a command, which is executed. command: :r!ls -F produces the following: Chapter 9. Editors, Text Tools, Graphics, and Printing 2 65 If you need to execute a series of shell commands, it's often easier to use the suspend