Hacking Vim 7.2 H a c k i n Vim is one of the most powerful open source editors used g What you will learn from this book by programmers and system administrators around the V world. While Vim itself is inherently a powerful editor, personalizing it to suit your needs can be a daunting task. n Create, install, and use Vim scripts to i m However, it is possible to do so with this book. extend its functionality 7 This book contains examples that cover everything from n Personalize Vim to suit your needs . personalizing Vim according to the user’s work cycle 2 to optimizations that will boost the user’s productivity. The main focus of this book is to make your life, as a Vim n Optimize your Vim editor to be faster and user, easier. more responsive After an introduction covering the derivation of Vim n Navigate through files faster while editing and its relatives from the vi editor, the author explains multiple files basic changes that you can make to the appearance of the Vim editor. Further chapters cover improved navigation through files and buffers in Vim; speeding up your work n Boost your productivity by using templates, C o m m u n i t y E x p e r i e n c e D i s t i l l e d with templates, autocompletion, folding, sessions, and autocompletion, folding, sessions, and registers; and formatting text and code, including using registers external formatting scripts. The final comprehensive chapter covers everything about using Vim scripts and n Improve the formatting of your text and code scripting to extend functionality. by mastering simple tricks Hacking Vim 7.2 Who this book is written for n Extend Vim with scripts If you are a Vim user who wants to get more out of this legendary text editor, this book is for you. It focuses n Retain your Vim configuration across on making life easier for the intermediate to experienced computers by storing an online copy K Vim users. i m $ 39.99 US S c Ready-to-use hacks with solutions for common situations £ 24.99 UK h u encountered by users of the Vim editor l z community experience distilled Prices do not include local sales tax or VAT P U B L I S H I N G where applicable Packt Publishing, Birmingham - Mumbai Kim Schulz www.PacktPub.com Hacking Vim 7.2 Ready-to-use hacks with solutions for common situations encountered by users of the Vim editor Kim Schulz BIRMINGHAM - MUMBAI Hacking Vim 7.2 Copyright © 2010 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, and its dealers and distributors will be held liable for any damages caused or alleged to be 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. First published: April 2010 Production Reference: 1230410 Published by Packt Publishing Ltd. 32 Lincoln Road Olton Birmingham, B27 6PA, UK. ISBN 978-1-849510-50-9 www.packtpub.com Cover Image by Asher Wishkerman ([email protected]) [FM-2 ] Credits Author Editorial Team Leader Kim Schulz Mithun Sehgal Reviewers Project Team Leader Boris Fersing Lata Basantani Thomas Moyer Project Coordinator Shubhanjan Chatterjee Development Editors Darshana D. Shinde Amey Kanse Proofreader Lynda Silwoski Technical Editors Madhumita Singh Production Coordinator Adline Swetha Jesuthas Conrad Sardinha Cover Work Copy Editor Adline Swetha Jesuthas Sneha Kulkarni Indexer Hemangini Bari [FM-3 ] About the Author Kim Schulz has an M.Sc. in Software Engineering from Aalborg University in Denmark. He has been an active developer in the Linux and open source communities since 1997, and has worked with everything from translation and bug fixing to producing a full-blown software system. Throughout this entire time, Vim has been Kim's "weapon" of choice and it is the first program he installs whenever he sits by a new computer. Today Kim works as a Software engineer at CSR Plc, developing software for the next generation mobile and wireless technologies. Kim is also the owner of a web hosting and development company, Devteam Denmark, that specializes in hosting of websites developed using the Fundanemt Content Management System—a CMS that Kim is a co-developer of. I would like to thank my wife, Line, for letting me take the time to write this book. Without her positive attitude and help, I would never have gotten this book ready. I would also like to add a great thank you to Bram Moolenaar for developing the Vim Editor—I appreciate the fruits of your work every day. [FM-4 ] About the Reviewers Boris Fersing is an amateur photographer and student in computational linguistics at the University of Saarland, Germany. For his studies, he participated in many projects and used many programming languages (SML, C/C++, Java, Ruby, Prolog) and Vim was always his editor of choice. He also worked as system administrator for a department of the University of Saarland. With this job he learned how to use some Unix tools and improved his knowledge about the Vim editor. I really would like to thank the author for writing such a nice book. It was a pleasure to review it. The information it contains is really interesting. I recommend this book to everyone who wants to learn more about Vim. Thomas Moyer is a graduate student in Computer Science and Engineering at Pennsylvania State University. He is currently looking at areas of Computer Security including trusted computing hardware and Web 2.0 security. He spends a great deal of time using Vim for editing both code and also writing papers. He has completed a Master's degree from Penn State and is currently working on his Ph.D. I would like to thank my wife and daughter for all of their support, and also the rest of my family. I would also like to thank the members of the Systems and Internet Infrastructure Security Lab at Penn State for their continued support. [FM- 5 ] Table of Contents Preface 1 Chapter 1: Getting Started with Vim 7 Getting Vim 8 vi, Vim, and friends 8 vi 9 STEVIE 9 Elvis 10 nvi 10 Vim 11 Vile 13 Compatibility 14 Vim is charityware 15 Common terminology 15 Summary 16 Chapter 2: Personalizing Vim 17 Where are the configuration files? 18 Changing the fonts 20 Changing color scheme 21 Personal highlighting 22 Example 1: Mark color characters after a certain column 24 Example 2: Mark tabs not used for indentation in code 25 Example 3: Preventing errors caused by IP addresses 26 A more informative status line 26 Toggle menu and toolbar 28 Adding your own menu and toolbar buttons 29 Adding a menu 29 Adding toolbar icons 32 Table of Contents Modifying tabs 33 Work area personalization 37 Adding a more visual cursor 37 Adding line numbers 39 Spell checking your language 40 Adding helpful tool tips 43 Using abbreviations 46 Example 1: Using abbreviations for quick address insertion 47 Modifying key bindings 49 Summary 52 Chapter 3: Better Navigation 53 Faster navigation in a file 54 Context-aware navigation 54 Moving around within a code file 54 Moving in a code file 56 Navigating long lines 59 Faster navigation in Vim help 60 Faster navigation in multiple buffers 61 Open referenced files faster 62 Search and you will find 63 Search the current file 64 Example 1: Find the next occurrence of a word 64 Example 2: Search for a word under the cursor 65 Search in multiple files 65 Search the help system 67 X marks the spot 68 Visible markers—using signs 68 Hidden markers—using marks 71 Summary 72 Chapter 4: Production Boosters 73 Using templates 74 Using template files 74 Abbreviations as templates 76 Snippets with the snipMate script 78 Using tag lists 80 Easier taglist navigation 83 Other usages of taglists 83 Using autocompletion 84 Autocompletion with known words 84 Autocompletion using dictionary lookup 85 [ ii ] Table of Contents Omnicompletion 86 All-in-one completion 89 Using macro recording 90 Using sessions 93 Simple session usage 93 Satisfy your own session needs 96 Sessions as a project manager 97 Registers and undo branching 98 Using registers 99 The unnamed register 100 The small delete register 100 The numbered registers 100 The named registers 101 The read-only registers 101 The selection and drop registers 102 The black hole register 102 Search pattern register 102 The expression register 103 Using undo branching 103 Folding 107 Simple text file outlining 110 Using vimdiff to track the changes 111 Navigation in vimdiff 113 Using diff to track changes 114 Open files anywhere 115 Faster remote file editing 117 Summary 118 Chapter 5: Advanced Formatting 121 Formatting text 121 Putting text into paragraphs 122 Aligning text 124 Marking headlines 125 Creating lists 127 Formatting code 129 Autoindent 130 Smartindent 130 Cindent 131 Indentexpr 132 Fast code-block formatting 132 Auto format pasted code 135 [ iii ]