Learning the vi and Vim Editors www.it-ebooks.info
Other resources from O’Reilly Related titles vi Editor Pocket Reference Unix in a Nutshell Classic Shell Scripting The Productive Programmer Unix Power Tools Mac OS X for Unix Geeks oreilly.com oreilly.com is more than a complete catalog of O’Reilly books. You’ll also find links to news, events, articles, weblogs, sample chapters, and code examples. oreillynet.com is the essential portal for developers interested in open and emerging technologies, including new platforms, pro- gramming languages, and operating systems. Conferences O’Reilly Media brings diverse innovators together to nurture the ideas that spark revolutionary industries. We specialize in documenting the latest tools and systems, translating the inno- vator’s knowledge into useful skills for those in the trenches. Visit conferences.oreilly.com for our upcoming events. Safari Bookshelf (safari.oreilly.com) is the premier online refer- ence library for programmers and IT professionals. Conduct searches across more than 1,000 books. Subscribers can zero in on answers to time-critical questions in a matter of seconds. Read the books on your Bookshelf from cover to cover or sim- ply flip to the page you need. Try it today for free. www.it-ebooks.info
SEVENTH EDITION Learning the vi and Vim Editors Arnold Robbins, Elbert Hannah, and Linda Lamb Tomcat ™ The Definitive Guide Jason Brittain and Ian F. D rwin Beijing • Cambridge • Farnham • Köln • Sebastopol • Taipei • Tokyo main.title Page iii Monday, May 19, 2008 11:21 AM www.it-ebooks.info
Learning the vi and Vim Editors, Seventh Edition by Arnold Robbins, Elbert Hannah, and Linda Lamb Copyright © 2008 Arnold Robbins, Elbert Hannah, and Linda Lamb. All rights reserved. Printed in the United States of America. Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472. O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (http://safari.oreilly.com ). For more information, contact our corporate/ institutional sales department: 800-998-9938 or corporate@oreilly.com. Editor: Andy Oram Production Editor: Sarah Schneider Copyeditor: Genevieve d’Entremont Proofreader: Sarah Schneider Indexer: Joe Wizda Cover Designer: Karen Montgomery Interior Designer: David Futato Illustrator: Robert Romano Printing History: July 2008: Seventh Edition November 1998: Sixth Edition October 1990: Fifth Edition June 1988: Fourth Edition August 1987: Third Edition April 1986: Second Edition February 1986: First Edition Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of O’Reilly Media, Inc. Learning the vi and Vim Editors, 7th Edition, the image of a tarsier, and related trade dress are trademarks of O’Reilly Media, Inc. Many of the designations uses by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and O’Reilly Media, Inc. was aware of a trademark claim, the designations have been printed in caps or initial caps. While every precaution has been taken in the preparation of this book, the publisher and authors assume no responsibility for errors or omissions, or for damages resulting from the use of the information con- tained herein. ISBN: 978-0-596-52983-3 [M] 1215018617 www.it-ebooks.info
To my wife, Miriam, for your love, patience, and support. —Arnold Robbins, Sixth and Seventh Editions www.it-ebooks.info
Table of Contents Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiii Part I. Basic and Advanced vi 1. The vi Text Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 A Brief Historical Perspective 5 Opening and Closing Files 6 Quitting Without Saving Edits 10 2. Simple Editing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 vi Commands 13 Moving the Cursor 14 Simple Edits 18 More Ways to Insert Text 30 Joining Two Lines with J 31 Review of Basic vi Commands 32 3. Moving Around in a Hurry . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 Movement by Screens 35 Movement by Text Blocks 38 Movement by Searches 39 Movement by Line Number 43 Review of vi Motion Commands 44 4. Beyond the Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 More Command Combinations 47 Options When Starting vi 48 Making Use of Buffers 51 Marking Your Place 52 Other Advanced Edits 53 Review of vi Buffer and Marking Commands 53 vii www.it-ebooks.info
5. Introducing the ex Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 ex Commands 55 Editing with ex 58 Saving and Exiting Files 63 Copying a File into Another File 65 Editing Multiple Files 65 6. Global Replacement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 Confirming Substitutions 72 Context-Sensitive Replacement 73 Pattern-Matching Rules 74 Pattern-Matching Examples 81 A Final Look at Pattern Matching 89 7. Advanced Editing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95 Customizing vi 95 Executing Unix Commands 99 Saving Commands 103 Using ex Scripts 114 Editing Program Source Code 120 8. Introduction to the vi Clones . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125 And These Are My Brothers, Darrell, Darrell, and Darrell 125 Multiwindow Editing 126 GUI Interfaces 127 Extended Regular Expressions 128 Enhanced Tags 129 Improved Facilities 134 Programming Assistance 138 Editor Comparison Summary 140 Nothing Like the Original 141 A Look Ahead 141 Part II. Vim 9. Vim (vi Improved): An Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145 Overview 146 Where to Get Vim 150 Getting Vim for Unix and GNU/Linux 151 Getting Vim for Windows Environments 156 Getting Vim for the Macintosh Environment 157 Other Operating Systems 157 viii | Table of Contents www.it-ebooks.info
Aids and Easy Modes for New Users 157 Summary 158 10. Major Vim Improvements over vi . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159 Built-in Help 159 Startup and Initialization Options 160 New Motion Commands 167 Extended Regular Expressions 169 Customizing the Executable 171 11. Multiple Windows in Vim . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173 Initiating Multiwindow Editing 174 Opening Windows 177 Moving Around Windows (Getting Your Cursor from Here to There) 180 Moving Windows Around 181 Resizing Windows 183 Buffers and Their Interaction with Windows 186 Playing Tag with Windows 190 Tabbed Editing 191 Closing and Quitting Windows 192 Summary 193 12. Vim Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195 What’s Your Favorite Color (Scheme)? 195 Dynamic File Type Configuration Through Scripting 205 Some Additional Thoughts About Vim Scripting 213 Resources 218 13. Graphical Vim (gvim) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219 General Introduction to gvim 220 Customizing Scrollbars, Menus, and Toolbars 225 gvim in Microsoft Windows 236 gvim in the X Window System 237 GUI Options and Command Synopsis 237 14. Vim Enhancements for Programmers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239 Folding and Outlining (Outline Mode) 240 Auto and Smart Indenting 251 Keyword and Dictionary Word Completion 259 Tag Stacking 268 Syntax Highlighting 270 Compiling and Checking Errors with Vim 279 Some Final Thoughts on Vim for Writing Programs 284 Table of Contents | ix www.it-ebooks.info
15. Other Cool Stuff in Vim . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285 Editing Binary Files 285 Digraphs: Non-ASCII Characters 287 Editing Files in Other Places 289 Navigating and Changing Directories 290 Backups with Vim 292 HTML Your Text 293 What’s the Difference? 294 Undoing Undos 296 Now, Where Was I? 297 What’s My Line (Size)? 300 Abbreviations of Vim Commands and Options 302 A Few Quickies (Not Necessarily Vim-Specific) 303 More Resources 304 Part III. Other vi Clones 16. nvi: New vi . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307 Author and History 307 Important Command-Line Arguments 308 Online Help and Other Documentation 309 Initialization 309 Multiwindow Editing 310 GUI Interfaces 311 Extended Regular Expressions 311 Improvements for Editing 312 Programming Assistance 315 Interesting Features 315 Sources and Supported Operating Systems 315 17. Elvis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317 Author and History 317 Important Command-Line Arguments 317 Online Help and Other Documentation 319 Initialization 319 Multiwindow Editing 320 GUI Interfaces 323 Extended Regular Expressions 328 Improved Editing Facilities 328 Programming Assistance 332 Interesting Features 335 elvis Futures 340 x | Table of Contents www.it-ebooks.info
Sources and Supported Operating Systems 340 18. vile: vi Like Emacs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 343 Authors and History 343 Important Command-Line Arguments 344 Online Help and Other Documentation 345 Initialization 346 Multiwindow Editing 347 GUI Interfaces 349 Extended Regular Expressions 357 Improved Editing Facilities 359 Programming Assistance 365 Interesting Features 368 Sources and Supported Operating Systems 374 Part IV. Appendixes A. The vi, ex, and Vim Editors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 377 B. Setting Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 415 C. Problem Checklists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 431 D. vi and the Internet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 435 Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 447 Table of Contents | xi www.it-ebooks.info
Preface Text editing is one of the most common tasks on any computer system, and vi is one of the most useful standard text editors on a system. With vi you can create new files or edit any existing text-only file. vi, like many of the classic utilities developed during the early years of Unix, has a reputation for being hard to navigate. Bram Moolenaar’s enhanced clone, Vim (“vi Improved”), has gone a long way toward removing reasons for such impressions. Vim includes countless conveniences, visual guides, and help screens. It has become prob- ably the most popular version of vi, so this seventh edition of this book devotes seven new chapters to it in Part II, Vim. However, many other worthy clones of vi also exist; we cover three of them in Part III, Other vi Clones. Scope of This Book This book consists of 18 chapters and 4 appendixes, divided into 4 parts. Part I, Basic and Advanced vi, is designed to get you started using vi quickly, and to follow up with advanced skills that will let you use it effectively. The first two chapters, Chapter 1, The vi Text Editor, and Chapter 2, Simple Editing, present some simple vi commands with which you can get started. You should practice these until they are second nature. You could stop reading at the end of Chapter 2, having learned some elementary editing tools. But vi is meant to do a lot more than rudimentary word processing; the variety of commands and options enables you to shortcut a lot of editing drudgery. Chapter 3, Moving Around in a Hurry, and Chapter 4, Beyond the Basics, concentrate on easier ways to do tasks. During your first reading, you’ll get at least an idea of what vi can do and what commands you might harness for your specific needs. Later, you can come back to these chapters for further study. Chapter 5, Introducing the ex Editor, Chapter 6, Global Replacement, and Chapter 7, Advanced Editing, provide tools that help you shift more of the editing burden to the computer. They introduce you to the ex line editor underlying vi, and they show you how to issue ex commands from within vi. xiii www.it-ebooks.info
Chapter 8, Introduction to the vi Clones, provides an introduction to the extensions available in the four vi clones covered in this book. It centralizes in one place the descriptions of multiwindow editing, GUI interfaces, extended regular expressions, facilities that make editing easier, and several other features, providing a roadmap to what follows in the rest of this book. It also provides a pointer to source code for the original vi, which can be compiled easily on modern Unix systems (including GNU/ Linux). Part II, Vim, describes Vim, the most popular vi clone in the early part of the 21st century. Chapter 9, Vim (vi Improved): An Introduction, provides a general introduction to Vim, including where to get binary versions for popular operating systems and some of the different ways to use Vim. Chapter 10, Major Vim Improvements over vi, describes the major improvements in Vim over vi, such as built-in help, control over initialization, additional motion com- mands, and extended regular expressions. Chapter 11, Multiple Windows in Vim, focuses on multiwindow editing, which is per- haps the most significant additional feature over standard vi. This chapter provides all the details on creating and using multiple windows. Chapter 12, Vim Scripts, looks into the Vim command language, which lets you write scripts to customize and tailor Vim to suit your needs. Much of Vim’s ease of use “out of the box” comes from the large number of scripts that other users have already written and contributed to the Vim distribution. Chapter 13, Graphical Vim (gvim), looks at Vim in modern GUI environments, such as those that are now standard on commercial Unix systems, GNU/Linux and other Unix work-alikes, and MS Windows. Chapter 14, Vim Enhancements for Programmers, focuses on Vim’s use as a program- mer’s editor, above and beyond its facilities for general text editing. Of particular value are the folding and outlining facilities, smart indenting, syntax highlighting, and edit- compile-debug cycle speedups. Chapter 15, Other Cool Stuff in Vim, is a bit of a catch-all chapter, covering a number of interesting points that don’t fit into the earlier chapters. Part III, Other vi Clones, describes three other popular vi clones: nvi, elvis, and vile. Chapter 16, nvi: New vi, Chapter 17, Elvis, and Chapter 18, vile: vi Like Emacs, cover the various vi clones—nvi, elvis, and vile—showing you how to use their extensions to vi and discussing the features that are specific to each one. Part IV, Appendixes, provides useful reference material. xiv | Preface www.it-ebooks.info
Appendix A, The vi, ex, and Vim Editors, lists all vi and ex commands, sorted by func- tion. It also provides an alphabetical list of ex commands. Selected vi and ex commands from Vim are also included. Appendix B, Setting Options, lists set command options for vi and for all four clones. Appendix C, Problem Checklists, consolidates checklists found earlier in the book. Appendix D, vi and the Internet, describes vi’s place in the larger Unix and Internet culture. How the Material Is Presented Our philosophy is to give you a good overview of what we feel are vi survival materials for the new user. Learning a new editor, especially an editor with all the options of vi, can seem like an overwhelming task. We have made an effort to present basic con- cepts and commands in an easy-to-read and logical manner. After providing the basics for vi, which are usable everywhere, we move on to cover Vim in depth. We then round out our coverage of the vi landscape by looking at nvi, elvis, and vile. The following sections describe the conventions used in this book. Discussion of vi Commands A picture of a keyboard button, like the one on the left, marks the main discussion of that particular keyboard command or of related commands. You will find a brief introduction to the main concept before it is broken down into task-oriented sections. We then present the appropriate command to use in each case, along with a description of the command and the proper syntax for using it. Conventions In syntax descriptions and examples, what you would actually type is shown in the Courier font, as are all command names. Filenames are also shown in Courier, as are program options. Variables (which you would not type literally, but would replace with an actual value when you type the command) are shown in Courier italic. Brackets indicate that a variable is optional. For example, in the syntax line: vi [filename] filename would be replaced by an actual filename. The brackets indicate that the vi command can be invoked without specifying a filename at all. The brackets themselves are not typed. i Preface | xv www.it-ebooks.info
Certain examples show the effect of commands typed at the Unix shell prompt. In such examples, what you actually type is shown in Courier Bold, to distinguish it from the system response. For example: $ ls ch01.xml ch02.xml ch03.xml ch04.xml In code examples, italic indicates a comment that is not to be typed. Otherwise, italic introduces special terms and emphasizes anything that needs emphasis. Following traditional Unix documentation convention, references of the form printf(3) refer to the online manual (accessed via the man command). This example refers to the entry for the printf( ) function in section 3 of the manual (you would type man 3 printf on most systems to see it). Keystrokes Special keystrokes are shown in a box. For example: iWith a ESC Throughout the book, you will also find columns of vi commands and their results: Keystrokes Results ZZ "practice" [New file] 6 lines, 320 characters Give the write and save command, ZZ. Your file is saved as a regular Unix file. In the preceding example, the command ZZ is shown in the left column. In the window to the right is a line (or several lines) of the screen that show the result of the command. Cursor position is shown in reverse video. In this instance, since ZZ saves and writes the file, you see the status line shown when a file is written; the cursor position is not shown. Below the window is an explanation of the command and its result. Sometimes vi commands are issued by pressing the CTRL key and another key si- multaneously. In the text, this combination keystroke is usually written within a box (for example, CTRL-G ). In code examples, it is written by preceding the name of the key with a caret (^). For example, ^G means to hold down CTRL while pressing the G key. Problem Checklist A problem checklist is included in those sections where you may run into some trouble. You can skim these checklists and go back to them when you actually encounter a problem. All of the problem checklists are also collected in Appendix C, for ease of reference. xvi | Preface www.it-ebooks.info
What You Need to Know Before Starting This book assumes you have already read Learning the Unix Operating System (O’Reilly), or some other introduction to Unix. You should already know how to: • Log in and log out • Enter Unix commands • Change directories • List files in a directory • Create, copy, and remove files Familiarity with grep (a global search program) and wildcard characters is also helpful. Comments and Questions Please address comments and questions concerning this book to the publisher: O’Reilly Media, Inc. 1005 Gravenstein Highway North Sebastopol, CA 95472 800-998-9938 (in the United States or Canada) 707-829-0515 (international or local) 707-829-0104 (fax) To ask technical questions or comment on the book, send email to: bookquestions@oreilly.com The web site for this book lists examples, errata, and plans for future editions. You can access this page at: http://www.oreilly.com/catalog/9780596529833 For more information about our books, conferences, software, resource centers, and the O’Reilly Network, see our web site: http://www.oreilly.com Safari® Books Online When you see a Safari® Books Online icon on the cover of your favorite technology book, that means the book is available online through the O’Reilly Network Safari Bookshelf. Safari offers a solution that’s better than e-books. It’s a virtual library that lets you easily search thousands of top tech books, cut and paste code samples, download chapters, Preface | xvii www.it-ebooks.info
and find quick answers when you need the most accurate, current information. Try it for free at http://safari.oreilly.com. About the Previous Editions In the fifth edition of this book (then called Learning the vi Editor), the ex editor com- mands were first discussed more fully. In Chapters 5, 6, and 7, the complex features of ex and vi were clarified by adding more examples, in topics such as regular expression syntax, global replacement, .exrc files, word abbreviations, keyboard maps, and edit- ing scripts. A few of the examples were drawn from articles in Unix World magazine. Walter Zintz wrote a two-part tutorial* on vi that taught us a few things we didn’t know, and that also had a lot of clever examples illustrating features we did already cover in the book. Ray Swartz also had a helpful tip in one of his columns.† We are grateful for the ideas in these articles. The sixth edition of Learning the vi Editor introduced coverage of four freely available “clones,” or work-alike editors. Many of them have improvements over the original vi. One could thus say that there is a “family” of vi editors, and the book’s goal was to teach you what you need to know to use them. That edition treated nvi, Vim, elvis, and vile equally. The sixth edition also added the following features: • Many minor corrections and additions were made to the basic text. • For each chapter where appropriate, a command summary was added at the end. • New chapters covered each vi clone, the features and/or extensions common to two or more of the clones, and multiwindow editing. • The chapters for each vi clone described a bit of that program’s history and goals, its unique features, and where to get it. • A new appendix described vi’s place in the larger Unix and Internet culture. Preface to the Seventh Edition This seventh edition of Learning the vi and Vim Editors retains all the good features of the sixth edition. Time has proven Vim to be the most popular vi clone, so this edition adds considerably expanded coverage of that editor (and gives it a place in the title). However, to be relevant for as many users as possible, we have retained and updated the material on nvi, elvis, and vile. * “vi Tips for Power Users,” Unix World, April 1990; and “Using vi to Automate Complex Edits,” Unix World, May 1990. Both articles by Walter Zintz. (See Appendix D for the web location of these articles.) † “Answers to Unix,” Unix World, August 1990. xviii | Preface www.it-ebooks.info
Comments 0
Loading comments...
Reply to Comment
Edit Comment