Statistics
7
Views
0
Downloads
0
Donations
Support
Share
Uploader

高宏飞

Shared on 2026-03-31

AuthorArnold Robbins, Elbert Hannah

Among the text editors being used in the programming community, perhaps the most important family is vi and its derivatives. With this updated edition, Unix and Linux users will learn text editing basics for both vi and Vim ("vi improved") before moving on to advanced editing tools for each editor. Authors Arnold Robbins and Elbert Hannah cover the latest major releases of Vim, including 8.0 and 8.2. If you're a programmer or computer analyst, or you work with browsers or command-line interfaces, using Vim can speed up your work and make complex tasks easier. You'll examine multiwindow editing, global search and replacement, and power tools for programmers, and learn how to write interactive macros and scripts to extend the editor—all in the easy-to-follow style that's made this book a classic. • Go beyond the basics to learn which vi commands fit your specific needs • Learn advanced vi tools that shift most of the editing burden to the computer • Explore Vim tools that provide major improvements over vi • Examine Vim's multiwindow editing feature, a significant upgrade over vi • Use Vim scripts to customize and tailor Vim to your needs • Look at Vim in modern GUI environments with Graphical Vim (gvim) • See Vim in the broader programming milieu, including using it as an IDE Arnold Robbins is a professional programmer and technical author. He's worked with Unix systems since 1980, including commercial Unix systems from Sun, IBM, HP, and DEC, and he's worked with GNU/Linux systems since 1996. He is also the long-time maintainer of GNU Awk (gawk) and its documentation as well as the author and coauthor of multiple O'Reilly titles. Elbert Hannah specializes in integrating technologies. After discovering Unix early on, he built a career integrating disparate systems, ultimately integrating technologies in the finance industry. Underlying all of his accomplishments is a reliance on the power and agility of the vi text editing framework.

Tags
No tags
ISBN: 1492078808
Publisher: O'Reilly Media
Publish Year: 2022
Language: 英文
Pages: 516
File Format: PDF
File Size: 16.2 MB
Support Statistics
¥.00 · 0times
Text Preview (First 20 pages)
Registered users can read the full content for free

Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.

Arnold Robbins & Elbert Hannah 8th Edition Learning the vi & Vim Editors Power and Agility Beyond Just Text Editing
(This page has no text content)
Arnold Robbins and Elbert Hannah Learning the vi and Vim Editors Power and Agility Beyond Just Text Editing EIGHTH EDITION Boston Farnham Sebastopol TokyoBeijing
978-1-492-07880-7 [LSI] Learning the vi and Vim Editors by Arnold Robbins and Elbert Hannah Copyright © 2022 Elbert Hannah and Arnold Robbins. 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://oreilly.com). For more information, contact our corporate/institutional sales department: 800-998-9938 or corporate@oreilly.com. Acquisitions Editor: Amanda Quinn Development Editors: Gary O’Brien and Shira Evans Production Editor: Kate Galloway Copyeditor: Arthur Johnson Proofreader: Kim Wimpsett Indexer: Sue Klefstad Interior Designer: David Futato Cover Designer: Karen Montgomery Illustrator: Kate Dullea July 2008: Seventh Edition November 2021: Eighth Edition Revision History for the Eighth Edition 2021-11-17: First Release See http://oreilly.com/catalog/errata.csp?isbn=9781492078807 for release details. The O’Reilly logo is a registered trademark of O’Reilly Media, Inc. Learning the vi and Vim Editors, the cover image, and related trade dress are trademarks of O’Reilly Media, Inc. The views expressed in this work are those of the authors, and do not represent the publisher’s views. While the publisher and the authors have used good faith efforts to ensure that the information and instructions contained in this work are accurate, the publisher and the authors disclaim all responsibility for errors or omissions, including without limitation responsibility for damages resulting from the use of or reliance on this work. Use of the information and instructions contained in this work is at your own risk. If any code samples or other technology this work contains or describes is subject to open source licenses or the intellectual property rights of others, it is your responsibility to ensure that your use thereof complies with such licenses and/or rights.
To my wife, Miriam, for your love, patience, and support. —Arnold Robbins, Sixth, Seventh, and Eighth Editions To my wife, Anna, for your love, encouragement, and patience. Thank you for being there. —Elbert Hannah, Seventh and Eighth Editions
(This page has no text content)
Table of Contents Preface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv Part I. vi and Vim Fundamentals 1. Introducing vi and Vim. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Text Editors and Text Editing 3 Text Editors 3 Text Editing 6 A Brief Historical Perspective 7 Opening and Closing Files 9 Opening a File from the Command Line 9 Opening a File from the GUI 10 Problems Opening Files 10 Modus Operandi 11 Saving and Quitting a File 12 Quitting Without Saving Edits 13 Problems Saving Files 13 Exercises 14 2. Simple Editing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 vi Commands 16 Moving the Cursor in Command Mode 17 Single Movements 18 Numeric Arguments 20 Movement Within a Line 20 Movement by Text Blocks 22 Simple Edits 23 v
Inserting New Text 24 Appending Text 25 Changing Text 25 Changing Case 28 Deleting Text 28 Moving Text 32 Copying Text 33 Repeating or Undoing Your Last Command 34 More Ways to Insert Text 36 Numeric Arguments for Insert Commands 37 Joining Two Lines with J 37 Problems with vi Commands 38 Mode Indicators 38 Review of Basic vi Commands 39 3. Moving Around in a Hurry. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 Movement by Screens 41 Scrolling the Screen 42 Repositioning the Screen with z 43 Redrawing the Screen 43 Movement Within a Screen 44 Movement by Line 45 Movement by Text Blocks 46 Movement by Searches 47 Repeating Searches 48 Current Line Searches 50 Movement by Line Number 51 The G (Go To) Command 52 Review of vi Motion Commands 52 4. Beyond the Basics. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 More Command Combinations 55 Options When Starting vi and Vim 56 Advancing to a Specific Place 57 Read-Only Mode 58 Recovering a Buffer 59 Making Use of Registers 60 Recovering Deletions 60 Yanking to Named Registers 61 Marking Your Place 62 Other Advanced Edits 63 Review of Register and Marking Commands 63 vi | Table of Contents
5. Introducing the ex Editor. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 ex Commands 66 Exercise: The ex Editor 68 Problem Getting to Visual Mode 68 Editing with ex 68 Line Addresses 69 Defining a Range of Lines 69 Line-Addressing Symbols 71 Search Patterns 72 Redefining the Current Line Position 73 Global Searches 73 Combining ex Commands 74 Saving and Exiting Files 74 Renaming the Buffer 76 Saving Part of a File 76 Appending to a Saved File 76 Copying a File into Another File 77 Editing Multiple Files 77 Invoking Vim on Multiple Files 78 Using the Argument List 78 Calling in New Files 79 Filename Shortcuts 79 Switching Files from Command Mode 80 Edits Between Files 80 ex Command Summaries 81 6. Global Replacement. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 The Substitute Command 85 Confirming Substitutions 86 Doing Things Globally Across the File 88 Context-Sensitive Replacement 88 Pattern-Matching Rules 89 Metacharacters Used in Search Patterns 90 POSIX Bracket Expressions 92 Metacharacters Used in Replacement Strings 94 More Substitution Tricks 96 Pattern-Matching Examples 97 Search for General Class of Words 98 Block Move by Patterns 99 More Examples 100 A Final Look at Pattern Matching 106 Deleting an Unknown Block of Text 106 Table of Contents | vii
Switching Items in a Textual Database 107 Using :g to Repeat a Command 109 Collecting Lines 110 7. Advanced Editing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113 Customizing vi and Vim 114 The :set Command 114 The .exrc File 116 Alternate Environments 116 Some Useful Options 117 Executing Unix Commands 118 Filtering Text Through a Command 120 Saving Commands 122 Word Abbreviation 122 Using the map Command 124 Mapping with a Leader 125 Protecting Keys from Interpretation by ex 125 A Complex Mapping Example 126 More Examples of Mapping Keys 128 Mapping Keys for Insert Mode 130 Mapping Function Keys 131 Mapping Other Special Keys 132 Mapping Multiple Input Keys 134 @-Functions 135 Executing Registers from ex 136 Using ex Scripts 136 Looping in a Shell Script 137 Here Documents 139 Sorting Text Blocks: A Sample ex Script 140 Comments in ex Scripts 142 Beyond ex 142 Editing Program Source Code 143 Indentation Control 143 A Special Search Command 146 Using Tags 147 Enhanced Tags 148 Part II. Vim 8. Vim (vi Improved): Overview and Improvements over vi. . . . . . . . . . . . . . . . . . . . . . . 157 About Vim 158 viii | Table of Contents
Overview 159 Author and History 159 Why Vim? 160 Compare and Contrast with vi 160 Categories of Features 161 Philosophy 164 Aids and Easy Modes for New Users 164 Built-In Help 165 Startup and Initialization Options 167 Command-Line Options 167 Behaviors Associated to Command Name 170 System and User Configuration Files 171 Environment Variables 172 New Motion Commands 174 Visual Mode Motion 175 Extended Regular Expressions 176 Extended Undo 180 Incremental Searching 181 Left-Right Scrolling 181 Summary 181 9. Graphical Vim (gvim). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183 General Introduction to gvim 184 Starting gvim 184 Using the Mouse 186 Useful Menus 188 Customizing Scrollbars, Menus, and Toolbars 190 Scrollbars 190 Menus 191 Toolbars 199 Tooltips 202 gvim in Microsoft Windows 202 gvim in the X Window System 203 Running gvim in Microsoft Windows WSL 203 Installing gvim in WSL 2 204 Installing an X Server for Windows 205 Configuring the X Server for Windows 205 GUI Options and Command Synopsis 211 10. Multiple Windows in Vim. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213 Initiating Multiwindow Editing 215 Multiwindow Initiation from the Command Line 215 Table of Contents | ix
Multiwindow Editing Inside Vim 217 Opening Windows 218 New Windows 218 Options During Splits 218 Conditional Split Commands 220 Window Command Summary 220 Moving Around Windows (Getting Your Cursor from Here to There) 221 Moving Windows Around 222 Moving Windows (Rotate or Exchange) 222 Moving Windows and Changing Their Layout 223 Window Move Commands: Synopsis 224 Resizing Windows 224 Window Resize Commands 225 Window Sizing Options 226 Resizing Command Synopsis 227 Buffers and Their Interaction with Windows 228 Vim’s Special Buffers 229 Hidden Buffers 229 Buffer Commands 230 Buffer Command Synopsis 231 Playing Tag with Windows 231 Tabbed Editing 233 Closing and Quitting Windows 234 Summary 236 11. Vim Enhancements for Programmers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237 Folding and Outlining (Outline Mode) 238 The Fold Commands 240 Manual Folding 242 Outlining 247 A Few Words About the Other Fold Methods 249 Auto and Smart Indenting 250 Vim autoindent Extensions to vi’s autoindent 251 smartindent 251 cindent 252 indentexpr 258 A Final Word on Indentation 259 Keyword and Dictionary Word Completion 260 Insertion Completion Commands 261 Some Final Comments on Vim Autocompletion 268 Tag Stacking 269 Syntax Highlighting 271 x | Table of Contents
Getting Started 271 Customization 272 Rolling Your Own 278 Compiling and Checking Errors with Vim 281 More Uses for the Quickfix List Window 285 Some Final Thoughts on Vim for Writing Programs 287 12. Vim Scripts. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289 What’s Your Favorite Color (Scheme)? 289 Conditional Execution 290 Variables 292 The execute Command 293 Defining Functions 295 A Nice Vim Piggybacking Trick 296 Tuning a Vim Script with Global Variables 297 Arrays 299 Dynamic File Type Configuration Through Scripting 300 Autocommands 300 Checking Options 302 Buffer Variables 303 The exists() Function 304 Autocommands and Groups 306 Deleting Autocommands 306 Some Additional Thoughts About Vim Scripting 308 A Useful Vim Script Example 309 More About Variables 310 Expressions 311 Extensions 311 A Few More Comments About autocmd 311 Internal Functions 311 Resources 313 13. Other Cool Stuff in Vim. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 315 Spell It! (i-t) 315 For a Different Take on Words, Try Thesaurus 318 Editing Binary Files 318 Digraphs: Non-ASCII Characters 320 Editing Files in Other Places 322 Navigating and Changing Directories 324 Backups with Vim 326 HTML Your Text 327 What’s the Difference? 328 Table of Contents | xi
viminfo: Now, Where Was I? 330 The viminfo Option 331 The mksession Command 332 What’s My Line (Size)? 334 Abbreviations of Vim Commands and Options 336 A Few Quickies (Not Necessarily Vim-Specific) 337 More Resources 338 14. Some Vim Power Techniques. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 339 Several Convenience Maps 339 Exiting Vim Simplified 339 Resize Your Window 340 Double Your Fun 340 Moving into the Fast Lane 343 Finding a Hard-to-Remember Command 343 Analyzing a Famous Speech 345 Some More Use Cases 348 Hitting the Speed Limit 350 Enhancing the Status Line 352 Summary 353 Part III. Vim in the Larger Milieu 15. Vim as IDE: Some Assembly Required. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 357 Plug-In Managers 357 Finding Just the Right Plug-In 359 Why Do We Want an IDE? 360 Doing It Yourself 361 EditorConfig: Consistent Text Editing Setup 361 NERDTree: File Tree Traversal Within Vim 362 nerdtree-git-plug-in: NERDTree with Git Status Indicators 362 Fugitive: Running Git from Within Vim 363 Completion 365 Termdebug: Use GDB Directly Within Vim 369 All-in-One IDEs 370 Coding Is Great, but What If I’m a Writer? 372 Conclusion 373 16. vi Is Everywhere. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 375 Introduction 375 Improving the Command-Line Experience 375 xii | Table of Contents
Sharing Multiple Shells 376 The readline Library 377 The Bash Shell 377 Other Programs 380 The .inputrc File 380 Other Unix Shells 381 The Z Shell (zsh) 382 Keep As Much History As You Can 382 Command-Line Editing: Some Closing Thoughts 383 Windows PowerShell 384 Developer Tools 384 The Clewn GDB Driver 384 CGDB: Curses GDB 385 Vim Inside Visual Studio 386 Vim for Visual Studio Code 387 Unix Utilities 391 More or Less? 391 screen 393 And …, Browsers! 397 Wasavi 398 Vim + Chromium = Vimium 399 vi for MS Word and Outlook 405 Honorable Mention: Tools with Some vi Features 408 Google Mail 408 Microsoft PowerToys 408 Summary 409 17. Epilogue. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 411 Part IV. Appendixes A. The vi, ex, and Vim Editors. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 415 B. Setting Options. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 461 C. The Lighter Side of vi. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 471 D. vi and Vim: Source Code and Building. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 483 Index. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 491 Table of Contents | xiii
(This page has no text content)
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. Today, Vim is the most popular version of vi, so this eighth edition focuses on Vim as follows: • Part I, “vi and Vim Fundamentals”, teaches basic vi skills, applicable to all versions of vi, but it does so in the context of Vim. • Part II, “Vim”, devotes a number of chapters specifically to Vim’s advanced features. • Part III, “Vim in the Larger Milieu”, presents chapters relating to Vim in a larger context. Scope of This Book This book consists of 17 chapters and four appendixes, divided into four parts. Part I, “vi and Vim Fundamentals”, is designed to get you started using vi and Vim quickly, and to follow up with advanced skills that will let you use them effectively. The first two chapters—Chapter 1, “Introducing vi and Vim”, and Chapter 2, “Simple Editing”—present some simple editing 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 operations. xv
But the editors are 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”, con‐ centrate on easier ways to do tasks. During your first reading, you’ll get at least an idea of what vi and Vim 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 Chap‐ ter 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 Vim, and they show you how to issue ex commands from within vi and Vim. Part II, “Vim”, describes Vim, the most popular vi clone 21 years into the 21st century. It goes into detail on the many (many!) features Vim has over the original vi. Chapter 8, “Vim (vi Improved): Overview and Improvements over vi”, provides a general introduction to Vim. The chapter also gives an overview of the major improvements in Vim over vi, such as built-in help, control over initialization, additional motion commands, extended regular expressions, and many more. Chapter 9, “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 10, “Multiple Windows in Vim”, focuses on multiwindow editing, which is perhaps the most significant additional feature over standard vi. This chapter provides all the details on creating and using multiple windows. Chapter 11, “Vim Enhancements for Programmers”, focuses on Vim’s use as a pro‐ grammer’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 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, “Other Cool Stuff in Vim”, is a bit of a catchall chapter, covering a number of interesting points that don’t fit into the earlier chapters. Chapter 14, “Some Vim Power Techniques”, presents some useful “power techniques.” Based around the idea of personal key remappings, it shows you more ways to be productive. Part III, “Vim in the Larger Milieu”, looks at vi’s and Vim’s roles in the larger software development and computer usage worlds. xvi | Preface
Chapter 15, “Vim as IDE: Some Assembly Required”, touches the tip of the iceberg of the world of Vim plug-ins, focusing on how you can change Vim from “just” an editor into a full-fledged integrated development environment (IDE). Chapter 16, “vi Is Everywhere”, looks at other significant software environments where vi-style editing can be brought into play to increase productivity. Chapter 17, “Epilogue”, provides a brief summary to round things off. Part IV, “Appendixes”, provides useful reference material. Appendix A, “The vi, ex, and Vim Editors”, lists all standard vi and ex commands, sorted by function. 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 Vim. Appendix C, “The Lighter Side of vi”, presents some humorous material related to vi. Appendix D, “vi and Vim: Source Code and Building”, describes where to get the “Heirloom” vi, as well as how to get Vim for your Unix, GNU/Linux, MS-Windows, or Macintosh system. How the Material Is Presented Our philosophy is to give you a good overview of what we feel are vi and Vim survival materials for the new user. Learning a new editor, especially an editor with all the options of Vim, can seem like an overwhelming task. We have made an effort to present basic concepts and commands in an easy-to-read and logical manner. After providing the basics for vi and Vim, which are usable everywhere, we move on to cover Vim in depth. The following sections describe the conventions used in this book. Discussion of vi Commands For each keyboard command or group of related commands, you will find a brief introduction to the main concept before it is broken down into task-oriented sec‐ tions. 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 constant width font, as are all command names and program options. Variables (which you would not type literally but would replace with an actual value when you Preface | xvii
type the command) are shown in Constant width 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. Certain examples show the effect of commands typed at the shell prompt. In such examples, what you actually type is shown in constant width 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. In the text, italic refers to filenames, 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 -s 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/Vim commands and their results: Keystrokes Results ZZ "practice" [New] 6L, 104C written Give the write and save command, ZZ. Your file is saved as a regular disk file. In the preceding example, the command ZZ is shown in the left column. In the column to the right is a line (or several lines) of the screen that shows 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 command/result is an explanation of the command and what it does. xviii | Preface