1. Vim Primer
It seems that starting something new is always the hardest part, and the same is true of the topic of this book, Vim. But after that initial tensiononce the ball starts rollingwe usually find that what we struggled with in the beginning now becomes easier, as we start flowing with the tide, rather than trying to fight against it. This common stumbling block is an important first point to remember: everything gets easier with experience and confidence.
This book is primarily focused on teaching you how to use the Vim text editor. Over the course of this introductory chapter, Ill be giving a brief explanation of Vims history, some alternatives, and why you should be excited to learn Vim.
For the moment, its only fair to forewarn you (if you werent already aware) that Vim has a reputation, for both beginners and advanced technical users, of being difficult to learn and awkward to use. But fear not! I hope to alleviate those concerns and to demonstrate throughout this book how learning Vim isnt as hard as you may have been led to believe.
In the latter chapters, Ill also cover a popular terminal-based tool called tmux. These two tools (Vim and tmux) go hand-in-hand, and its beneficial (once youve acquired a solid understanding of Vim) to also see how you can use it alongside tmux to improve your day-to-day workflow. Now, tmux isnt a requirement for using Vimits a supplement for more advanced technical usersso dont feel obliged to read that section, if its not of interest or relevance to you (although I would recommend reading through it and trying to understand more about what tmux can offer you).
Before we get started, there are a few quick points about Vim and tmux that I want to share with you and have you memorize.
Neither Vim nor tmux is as hard to learn as you may have previously thought.
You dont have to learn everything all at once. Start small and build up your knowledge incrementally.
Youll become more efficient the more you use the two tools.
A new world of command-line tooling will open up to you as you become more comfortable working inside a terminal environment.
Vim and tmux are a lot of fun to use.
That last point is one of the most important ones! As with anything in life, to be truly good at something typically means you enjoy it as well. Im very passionate about the tools I use, and that helps me delve deeper into what they can do and how they work.
Although being passionate about Vim isnt a prerequisite to using Vim, it will help the learning curve, if youre someone who genuinely enjoys improving his or her workflow and tooling. If youre happy with your current editor of choice (of which there are many) and youre being forced to read this book by a colleague who feels youd be a better writer/author/programmer/whatever by using Vim, or if youre here because you think learning Vim makes you smarter than your colleagues using an alternative editor, then youre probably not going to reap the benefits as much as someone who is holding this book because he or she saw it and really wanted to read it.
Also, although the title of this book is Pro Vim , it doesnt mean you require any prior knowledge of Vim. This book is aimed at total beginners and covers all the practical sides of Vim, such as the fundamentals of how it works, and offers real-world advice and experience, in the hope that you can avoid similar pitfalls that traditionally both new and experienced users of Vim stumble across.
The reason this book is considered pro is simply because there isnt really a lot of middle ground when it comes to using tools such as Vim and tmux. They are inherently advanced tools that offer a lot of complex functionality. If there were such a thing as a beginners book to Vim, chances are it would be a lot shorter and would avoid discussing a lot of important fundamental concepts, which ultimately means you wouldnt be using the tool to its potential and, subsequently, you would become less productive using it. Even beginners have to properly understand the tool theyre using. A lack of understanding the fundamentals will only introduce frustration with the tools and ultimately result in a user abandoning them for less powerful ones.
What Is Vim?
Vim ( www.vim.org/about.php ) is a free, open source text editor. It was originally released in 1991 for Unix-based computer systems and is considered to be an enhancement to the original Vi text editor ( http://en.wikipedia.org/wiki/Vi ), which was released way back in 1976 (to put this into context, I wasnt born until six years later).
Note
Vim is an acronym for Vi Improved. The majority of the standard editing commands available within Vim are actually provided by Vi.
One of the big selling points, if you like to think of software that way, is that Vim (and especially its predecessor Vi) is available almost everywhere. If youre running a Unix- or Linux-based system (e.g., the popular Macintosh operating system is Unix-based), youll likely find that Vim is installed by default.
Vim traditionally does not have a GUI (graphical user interface) like other text/code editors you may have used in the past (e.g., Sublime Text, Chocolat, Coda, Aptana, NetBeans, Eclipse, DreamWeaver, etc.), although there is a separate install of Vim available called gVim that provides a GUI.
Note
There are a few GUI-based versions of Vim, notably MacVim ( https://code.google.com/p/macvim ), which has better integration with the Macintosh operating system than the standard implementation of Vi and Vim. Most of the Vim specific features I cover in this book are still relevant to MacVim, so you shouldnt encounter any issues.
The standard installation of Vim is usually run from within a terminal/shell environment rather than as a separate application. Because the terminal is designed for power users and has the potential to do a lot of damage to your computer (if misused), it can be a scary and hostile environment. Again, fear not. Although visually, the terminal isnt the prettiest of beasts, Ill show you how you can spruce it up and get it looking a little bit more presentable and easier on the eyes. A nice-looking terminal theme can make the world of difference when youre going to be spending as much time as we are in the shell environment.
Modern Editors?
At the time of writing (2014), the hot editor of choice for a lot of developers would be GitHubs Atom editor ( https://atom.io ). This editor is heavily based on the Sublime Text editor ( www.sublimetext.com ), which, until Atom was released, was considered to be the best editor for developers. This brings me to my first concern: longevity. It seems we spend a large amount of time committing to memory key bindings and features that are specific to the editor were using, and yet within the space of a year, maybe two years, that knowledge is made redundant by the need to move to the next great editor. Vim and its predecessor Vi have been used consistently since their inception.
Both Atom and Sublime Text mix together a graphical interface alongside a need for more straightforward coding features. This was an attempt to move away from the late-90s-era editors such as Adobe DreamWeaver (which was aimed more at web designers than programmers). This was also to move away from very heavyweight and sluggish IDEs (Integrated Development Environments) such as Eclipse, which included everything and the kitchen sink.
Neither Atom nor Sublime Text is as lightweight as Vim, nor are they as ubiquitous as Vim. This is another reason why Vim is so enticing. Vim is effectively available everywhere by default (even on much older operating systems that only have Vi installedthe editing commands are the same, and this allows you to get to work fixing problems immediately, not trying to figure out how youre going to get another editor installed).