Mastering Vim Build a software development environment with Vim and Neovim Ruslan Osipov BIRMINGHAM - MUMBAI Mastering Vim Copyright © 2018 Packt Publishing All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews. Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book. Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information. Commissioning Editor: Pavan Ramchandani Acquisition Editor: Chaitanya Nair Content Development Editor: Pooja Parvatkar Technical Editor: Divya Vadhyar Copy Editor: Safis Editing Project Coordinator: Ulhas Kambali Proofreader: Safis Editing Indexer: Rekha Nair Graphics: Tom Scaria Production Coordinator: Deepika Naik First published: November 2018 Production reference: 1291118 Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK. ISBN 978-1-78934-109-6 www.packtpub.com To my mother Lilia and my loving partner Elisabeth, who survived my anxiety over writing this book – Ruslan Osipov mapt.io Mapt is an online digital library that gives you full access to over 5,000 books and videos, as well as industry leading tools to help you plan your personal development and advance your career. For more information, please visit our website. Why subscribe? Spend less time learning and more time coding with practical eBooks and Videos from over 4,000 industry professionals Improve your learning with Skill Plans built especially for you Get a free eBook or video every month Mapt is fully searchable Copy and paste, print, and bookmark content Packt.com Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.packt.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at [email protected] for more details. At www.packt.com, you can also read a collection of free technical articles, sign up for a range of free newsletters, and receive exclusive discounts and offers on Packt books and eBooks. Contributors About the author Ruslan Osipov is a software engineer at Google, an avid traveler, and a part-time blogger. He is a self-taught engineer. He started publishing personal Vim notes in 2012, and became increasingly interested in the intricacies of the editor and its applications in optimizing development workflows. Huge thank you to Divya and Pooja, who have went out of their way to get this book through the review process. About the reviewer Bram Moolenaar is the creator and maintainer of Vim. He has been working on it for 27 years, and has no plans to stop improving it, thanks to the many volunteers who provide patches and test new versions. After studying electronics and inventing parts of digital copying machines, Bram decided that creating open source software was more useful and fun, so he worked on that exclusively for several years. He currently works for Google, one of the few companies that fully embrace open source software. In between, he did voluntary work on a project in Uganda and is still helping poor children there through the ICCF foundation. I would like to thank all the Vim developers for helping me make Vim into what it is today. Without them, only a fraction of the features would have been implemented and the quality would not have been nearly as high. I would also like to thank all the plugin writers for building on top of Vim and making complex features available to users (so that I don't have to!). And finally, I would like to thank Ruslan for writing a book that not only aids users with Vim's built-in features, but also with getting to know and use plugins. Packt is searching for authors like you If you're interested in becoming an author for Packt, please visit authors.packtpub.com and apply today. We have worked with thousands of developers and tech professionals, just like you, to help them share their insight with the global tech community. You can make a general application, apply for a specific hot topic that we are recruiting an author for, or submit your own idea. Table of Contents Preface 1 Chapter 1: Getting Started 5 Technical requirements 6 Let’s start a conversation (about modal interfaces) 6 Installation 7 Setting up on Linux 7 Setting up on MacOS 9 Using Homebrew 9 Downloading a .dmg image 13 Setting up on Windows 15 Unix-like experience with Cygwin 15 Installing Cygwin 16 Using Cygwin 18 Visual Vim with gVim 21 Verifying and troubleshooting the installation 24 Vanilla Vim vs gVim 26 Configuring Vim with your .vimrc 27 Common operations (or how to exit Vim) 29 Opening files 29 Changing text 31 Saving and closing files 33 A word about swap files 34 Moving around: talk to your editor 36 Making simple edits in insert mode 40 Persistent undo and repeat 43 Read the Vim manual using :help 44 Summary 48 Chapter 2: Advanced Editing and Navigation 49 Technical requirements 49 Installing plugins 50 Organizing workspace 51 Buffers 52 Plugin spotlight – unimpaired 54 Windows 55 Creating, deleting, and navigating windows 55 Moving windows 58 Resizing windows 64 Tabs 65 Folds 66 Table of Contents Folding Python code 67 Types of folds 69 Navigating file trees 70 Netrw 70 :e with wildmenu enabled 72 Plugin spotlight – NERDTree 73 Plugin spotlight – Vinegar 75 Plugin spotlight – CtrlP 77 Navigating text 78 Jumping into insert mode 82 Searching with / and ? 82 Searching across files 84 ack 86 Utilizing text objects 87 Plugin spotlight – EasyMotion 89 Copying and pasting with registers 91 Where do the registers come in? 92 Copying from outside of Vim 94 Summary 95 Chapter 3: Follow the Leader - Plugin Management 97 Technical requirements 97 Managing plugins 98 vim-plug 98 Honorable mentions 102 Vundle 102 Do it yourself 104 Pathogen 106 Profiling slow plugins 106 Profiling startup 106 Profiling specific actions 108 Deeper dive into modes 112 Normal mode 112 Command-line and ex modes 112 Insert mode 114 Visual and select mode 114 Replace and virtual replace mode 116 Terminal mode 117 Remapping commands 118 Mode – aware remapping 120 The leader key 121 Configuring plugins 122 Summary 126 Chapter 4: Understanding the Text 127 Technical requirements 127 [ ii ] Table of Contents Code autocomplete 127 Built-in autocomplete 128 YouCompleteMe 129 Installation 129 Using YouCompleteMe 131 Navigating the code base with tags 134 Exuberant Ctags 135 Automatically updating the tags 139 Undo tree and Gundo 139 Summary 143 Chapter 5: Build, Test, and Execute 144 Technical requirements 144 Working with version control 145 Quick-and-dirty version control and Git introduction 145 Concepts 146 Setting up a new project 146 Cloning an existing repository 148 Working with Git 148 Adding files, committing, and pushing 148 Creating and merging branches 151 Integrating Git with Vim (vim-fugitive) 153 Resolving conflicts with vimdiff 157 Comparing two files 157 vimdiff and Git 161 git config 162 Creating merge conflict 162 Resolving a merge conflict 163 tmux, screen, and Vim terminal mode 166 tmux 166 Panes are just like splits 167 Windows are just like tabs 170 Sessions are invaluable 171 tmux and Vim splits 171 Screen 173 Terminal mode 173 Building and testing 176 Quickfix list 176 Location list 179 Building code 179 Plugin spotlight: vim-dispatch 180 Testing code 181 Plugin spotlight – vim-test 181 Syntax checking code with linters 182 Using linters with Vim 182 Plugin spotlight – Syntastic 184 Plugin spotlight – ALE 186 [ iii ]