Early Praise for Modern Vim I’m once again impressed by the limitless possibilities of this classic editor. It goes without saying that I would—and will—recommend Modern Vim to anyone using Vim, including admins, developers, and Linux enthusiasts. ➤ Tibor Simic Software Developer, Ingemark With Practical Vim I am able to move around my text like a kangaroo. Modern Vim puts a Swiss Army knife into my pouch. ➤ Miroslav Šedivý Software Architect, UBIMET GmbH Even as an experienced Vim user and plugin author, I learned a lot with this book. ➤ Kassio Borges Senior Software Engineer, Zendesk, and author of the Neoterm plugin If you’re curious as to what’s new in Vim 8 and Neovim, or looking to extend the collection of great recipes started in Practical Vim, this book is an excellent resource. ➤ Eric Casteleijn Vim Enthusiast We've left this page blank to make the page numbers the same in the electronic and paper books. We tried just leaving it out, but then people wrote us to ask about the missing pages. Anyway, Eddy the Gerbil wanted to say “hello.” Modern Vim Craft Your Development Environment with Vim 8 and Neovim Drew Neil The Pragmatic Bookshelf Raleigh, North Carolina Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and The Pragmatic Programmers, LLC was aware of a trademark claim, the designations have been printed in initial capital letters or in all capitals. The Pragmatic Starter Kit, The Pragmatic Programmer, Pragmatic Programming, Pragmatic Bookshelf, PragProg and the linking g device are trade- marks of The Pragmatic Programmers, LLC. Every precaution was taken in the preparation of this book. However, the publisher assumes no responsibility for errors or omissions, or for damages that may result from the use of information (including program listings) contained herein. Our Pragmatic books, screencasts, and audio books can help you and your team create better software and have more fun. Visit us at https://pragprog.com. The team that produced this book includes: Publisher: Andy Hunt VP of Operations: Janet Furlow Managing Editor: Brian MacDonald Supervising Editor: Jacquelyn Carter Development Editor: Katharine Dvorak Copy Editor: Jasmine Kwityn Indexing: Potomac Indexing, LLC Layout: Gilson Graphics For sales, volume licensing, and support, please contact [email protected]. For international rights, please contact [email protected]. Copyright © 2018 The Pragmatic Programmers, LLC. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form, or by any means, electronic, mechanical, photocopying, recording, or otherwise, without the prior consent of the publisher. Printed in the United States of America. ISBN-13: 978-1-68050-262-6 Encoded using the finest acid-free high-entropy binary digits. Book version: P1.0—May 2018 Contents Acknowledgments . . . . . . . . . . . vii Introduction . . . . . . . . . . . . . ix 1. Get Modern Vim . . . . . . . . . . . . 1 Tip 1. Installing Vim 8 2 Tip 2. Switching to Neovim 4 Tip 3. Enabling Python Support in Neovim 7 2. Installing Plugins . . . . . . . . . . . 11 Tip 4. Understanding Scripts, Plugins, and Packages 11 Tip 5. Installing Plugins to Your Package 14 Tip 6. Managing Plugins with minpac 18 3. Opening Files . . . . . . . . . . . . 23 Tip 7. Finding Files Using Fuzzy Path Matching 23 Tip 8. Finding Files Semantically 30 Tip 9. Jumping to an Alternate File 35 4. Working with the Quickfix List . . . . . . . . 39 Tip 10. Running a Build and Navigating Failures 39 Tip 11. Switching Compilers 46 Tip 12. Linting the Current File 50 Tip 13. Searching Files with Grep-Alikes 56 Tip 14. Running Tests and Browsing Failures 63 5. Neovim’s Built-In Terminal Emulator . . . . . . 69 Tip 15. Grokking Terminal Mode 71 Tip 16. Running Programs in a Terminal Buffer 75 Tip 17. Managing Windows That Contain Terminal Buffers 79 Contents • vi Tip 18. Using Normal Mode Commands in a Terminal Buffer 82 Tip 19. Sending Commands to a Terminal Buffer 85 Tip 20. Applying Customizations to Your Shell in a Terminal Buffer 87 Tip 21. Avoiding Nested Neovim Instances 88 Tip 22. Using an Existing nvim Instance as the Preferred Editor 91 6. Sessions . . . . . . . . . . . . . 95 Tip 23. Saving and Restoring Sessions 96 Tip 24. Making Undo Persist Between Sessions 99 Tip 25. Restarting Terminal Processes When Resuming a Session 101 7. Configuring Vim . . . . . . . . . . . 105 Tip 26. Using Autocommands to Respond to Events 105 Tip 27. Respecting Project Conventions 112 Tip 28. Setting Buffer-Local Configuration Per Project 116 A1. What’s Next for Modern Vim? . . . . . . . . 127 Integrating with the Language Server Protocol 127 What’s Next for Vim 8 129 What’s Next for Neovim 130 Bibliography . . . . . . . . . . . . 137 Index . . . . . . . . . . . . . . 139 Acknowledgments I’m grateful to have the opportunity to work with the Pragmatic Bookshelf. Thanks to Susannah Pfalzer for encouraging me to submit a proposal for another book on Vim. And thanks to Katharine Dvorak for all your help throughout the project. This book could not have happened without the support of my wife, Hannah. I’m so grateful to you for believing in me. Modern Vim also wouldn’t have been possible without my technical reviewers. Each of you contributed something and helped shape the book. I’d like to thank Ali Alwasity, Kassio Borges, Eric Casteleijn, Tim Chase, Junegunn Choi, Javier Collado, Dave Copeland, Marco Hinz, Justin Keyes, Andy Lester, Janko Marohnic ´, Tim Pope, Steven! Ragnarok, fREW Schmidt, Miroslav Šedivý, Tibor Simic, Ken Takata, Tim Tyrrell, Andrew Wray, and Alex Young. Thanks to the folks at thoughtbot, Ben Orenstein in particular, for commis- sioning a series of video tutorials about Vim and Neovim. This collaboration pushed me to begin working on the book and gave me a running start. I’m always grateful to everyone who works on Vim, Neovim, and the assorted plugins that make my life easer. The Neovim logo was designed by Jason Long, and is licensed under the Creative Commons Attribution 3.0 Unported License.1 We’ve used the one- color flat variation of the logo, because it looks best both online and in print. The Vim logo is copyrighted. Thanks to Bram Moolenaar for granting permis- sion to reproduce a one-color flat variation of the Vim logo in this book. November 2017 was an unusually fruitful month. On November 1, we pub- lished the first beta edition of this book. On November 2, my wife gave birth to our son, Conrad. I could hardly believe my luck. 1. https://creativecommons.org/licenses/by/3.0 report erratum • discuss Introduction Vim’s core functionality makes it a good programmer’s text editor. Turning Vim into a full-blown development environment means combining it with other tools and extending its capabilities with plugins. In Practical Vim [Nei15], I focused on the core features of the editor. In this book, I show you how to extend Vim and make it the centerpiece of a Unix-based IDE. How This Book Is Structured Modern Vim is a recipe book. It’s not designed to be read from start to finish. Each chapter is a collection of tips that are related by a theme, and each tip demonstrates a particular feature in action. Some tips are self-contained. Others depend upon material elsewhere in the book. Those tips are cross- referenced so you can find everything easily. Modern Vim doesn’t progress from novice to advanced level, but each individ- ual chapter does. A less-experienced Vim user might prefer to make a first pass through the book, reading just the early tips in each chapter. A more advanced user might choose to focus on the later tips or move around the book as needed. If it helps, you can think of this as a “Choose Your Own Adventure” book. A Note on Vim Versions To follow the tips in this book, you’re going to need an up-to-date installation of Vim. (The clue is right there in the book’s title!) You have two options: use version 8 of Vim or version 0.2 of Neovim. Vim Version 8 Version 8 of Vim was released in September 2016. It introduced some new features that you’ll learn about in this book, such as packages and job control. As a minimum requirement, you’ll need to be running version 8 of Vim, report erratum • discuss