Learning the vi and Vim Editors EIGHTH EDITION Power and Agility Beyond Just Text Editing With Early Release ebooks, you get books in their earliest form—the authors’ raw and unedited content as they write—so you can take advantage of these technologies long before the official release of these titles. Arnold Robbins and Elbert Hannah 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 [email protected]. Acquisitions Editor: Amanda Quinn Development Editor: Shira Evans Production Editor: Kate Galloway Interior Designer: David Futato Cover Designer: Karen Montgomery Illustrator: O’Reilly Media, Inc. July 2008: Seventh Edition November 2021: Eighth Edition Revision History for the Early Release 2020-08-06: First Release 2020-09-17: Second Release 2020-11-17: Third Release 2021-01-25: Fourth Release 2021-04-30: Fifth Release 2021-07-28: Sixth Release 2021-10-01: Seventh 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. 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. 978-1-492-07873-9 Dedication 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 Preface A NOTE FOR EARLY RELEASE READERS With Early Release ebooks, you get books in their earliest form—the authors’ raw and unedited content as they write—so you can take advantage of these technologies long before the official release of these titles. If you have comments about how we might improve the content and/or examples in this book, or if you notice missing material within this chapter, please reach out to the author at [email protected]. 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 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. 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, concentrate 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 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 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 programmer’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 catch-all 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. Chapter 15, Vim as IDE: Some Assembly Required, touches the tip of the iceberg of the world of Vim plugins, focusing on how you can change from 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 in order 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 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 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 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.