Loving VimFeb 6 2011
Vim has quickly become my go-to editor of choice for Windows, Mac and Linux. So far I’ve had about three months of serious Vim usage and I’m just starting to hit that vim-as-second-nature experience where the power really starts to shine. I’m shocked I’ve waited this long to put in the time to seriously learn it. Now that I’m past the beginner hump I wish I learned Vim long ago- when I tried Vim in the past, I just never got over that WTF-is-going-on-here frustration! Better late than never I suppose!
Why I Like Vim – Mac
Coming from Visual Studio, I longed for a VS like experience for programming on my Mac, whether it’s html/css/js or for my recent focus on Ruby and Rails. I checked out both Aptana and Eclipse but quickly became frustrated- it was kind of like VS but not really and it was just too weird going back and forth. Plus, my biggest pet peave with development started to emerge: I wasn’t learning a language, I was learning a tool that abstracted the language away. There’s nothing that could be worse- once your tool hides the benefits of the underlying infrastructure, you’re missing the point, and you’ll usually be behind the curve because the language always moves faster than the support.
TextMate then became the go-to: it’s widely used, powerful, and there’s a lot of resources for learning. The simplified environment mixed with the command line really created a higher degree of fluidity, and I realized how nice it can be to develop outside an integrated environment. Textmate has its features- Command-T is slick, the project drawer is helpful, and the Rails support is great along with the other available bundles. But it lacked split windows which drove me crazy. There’s nothing more essential than split windows: I want to see my specs and code side-by-side. I want to see my html and js side-by-side. And you can’t do that with the Textmate. So I turned to MacVim and haven’t looked back.
Why I Like Vim – Windows
Don’t get me wrong: I love me some Visual Studio. Visual Studio was my first IDE when programming professionally and my thought was “wow, I can really focus on building stuff rather than pulling my hair out with every build, every exception and every bug”. It was so much better than the emacs days of college. It’s my go-to for anything .NET, as it should be. But there are some text-editor needs that aren’t related to coding or .NET, and VS is too much of a beast to deal with for those things. First, for html/js/css editing that’s not part of a Visual Studio project, VS not great to work with. It’s annoying to be forced to create a Visual Studio project to house related content, especially when it’s already grouped together in the file system. Quickly checking out an html template or a js code samples becomes tedious when you just want to look around. The VS File Explorer is a step in the right direction, but it’s not there yet; I know there’s shell plugins for a “VS Project Here” shortcut but really? Is that necessary?
Then there’s the notepad issue. Notepad is barely an acceptable editor for checking out the occasional config file or random text file. Everyone knows how incredibly limited it can be, not to mention how much it sucks for large files. Pretty much everything about it sucks, actually, and everyone knows it. Notepad++ is a nice alternative, but it’s no Vim. So after I got modestly comfortable with MacVim, I thought, why not do this on Windows too? So I started using gVim and haven’t looked back.
Why I Like Vim – Linux
This blog runs WordPress on a Linux box hosted by Rackspace. Occasionally, I need to pop in via ssh, edit a config file, push some stuff, tweak some settings, etc. Nano was the lightweight go-to editor of choice, and works well. There are many differences between nano and Vim, the biggest being nano is a “modeless” editor while Vim’s power comes from the Normal, Insert, and Visual modes. Like always, stick with what works. But once you’re hooked on Vim, I’d be surprised how often you go back to Nano, especially when you get your configuration files synced up with source control, and Vim’s ubiquity on Linux.
Why I Like Vim
There’s plenty of resources out there about Vim and what makes it great- a quick google search will give you some great resources. But I really appreciated Vim when I became comfortable with the following features:
Splits and Buffers
Splits are one of my favorite features- it allows you to view more than one file at once on the same screen. The power of splits allows you to have multiple vertically and horizontally split windows so you can see anything you want. There’s also tab support, but I find using splits and managing buffers a better way to cycle through files. Buffers allow you to keep files open and active but in the background, so you can quickly swap them into the current window in a variety of ways. It’s like having a stack of papers on a table while easily going to any page instantaneously. This is different than having files within a project, which would be like having those papers in a folder- buffers provide another level of abstraction allowing you to manipulate a set of content together. In summary, you have a set of papers in a folder (the current directory) and put them on the desk to work on (using buffers) and arrange them in front of you (using splits).
A major feature of Vim, which really sets it apart from other editors, is how it separates behavior into different modes- specifically, normal and insert modes. Insert mode is simple: it allows you to write text. But normal mode is all about navigation and manipulation: finding text, cutting lines, moving stuff around, substituting words, running commands, etc. It offers a whole new level of functionality including shell commands interaction for doing anything you would on the command line. With the plethora of plugins around you can pretty much do anything within Vim you could imagine- from simple editing, to testing, to source control management, to deployments. Modes break you free from a whole slew of Ctrl + whatever commands required in other editors, allowing for precision movement with a minimal set of keystrokes. The best analogy is you can “program your editing” in a way unmatched from any other editor.
Search and Replace (aka Substitute)
Vim’s Search and Substitute features make any old find and replace dialog box seem stupid. I’ve barely started unlocking the power of search and replace, but already, I wish every application behaved this way. In normal mode you can easily create everything from simple string searches to complex regex to find what your looking for in a couple of keystrokes. On top of that, it’s only a few more keystrokes to replace text. Because it’s all driven by key commands, you can easily change or alter what you’re doing without having to start an entire search and you never have that “context switch” of filling out a form in a dialog box. It’s all right in front of you. Highlighting allows you to see matches and you can lock in a search to easily jump between results. Viewing and editing configuration files is the real win for me over other editors: whatever the size, I can open a file and type a few keystrokes to go to exactly where I need to be, even if I’m not sure where to go. This is so much better than using notepad or even Visual Studio.
Source controlled configuration
I put my vimfiles on github so I can synchronize them across platforms. This offers an unparalleled level of uniformity across environments with minimal effort. A lot of people do this, and it’s helpful to see how others have configured their environment. You’ll pick up a lot of neat tidbits by reading people’s Vimfiles!
Vim Across Platforms
Vim can run in either a gui window (like MacVim and gVim) or from a command line. These are two different executables with a slightly different feature set. Usually, you get a little more with a gui vim, especially around OS integration (like cutting and pasting text) while running shell commands are easier with command line vim. Gui Vim also offers better color support for syntax highlighting.
MacVim is the Vim app for the Mac. It has a really nice full screen mode and native Mac commands alongside the Vim ones. I love the Peepopen search plugin which is only available on the Mac (and can be used with Textmate). It’s a slick approach which is better than Textmate’s Command-T. I like running MacVim in full screen mode with the toolbar off to get the most screen real estate.
gVim is the Windows gui version of Vim, and I find it preferable over the command line Vim via cygwin. gVim has a shell extension which lets you open any file in gVim- set it as the default to avoid notepad. Note that Vim on Windows reads configuration files from the _vimrc, _gvimrc, and _vimfiles directory, which is different than the normal .vimrc and .vim location on other platforms. That hung me up when I was trying to sync configuration via git.
As for command line Vim, that’s probably what you’ll be using on Linux. In Gary Bernhardt’s Play-by-Play Peepcode video I learned a cool trick of running Vim via the command line, then using jobs to suspend (Ctrl-Z) to return to the command line, than going back to Vim via foreground (fg).
There are plenty of resources on the web for getting started with Vim. Steve Losh’s Coming Home To Vim is a great overview that points you to a lot of other helpful posts. I bought a subscription to Peepcode and picked up the Smash Into Part II episode. I didn’t watch Part I because I felt like it was too basic, but Part II has a lot of substantial content. Peepcode offers a high quality product so you probably can’t go wrong with getting both if you’d rather be safe than sorry. I also watched the Gary Bernhardt Play by Play which focuses a lot on using Vim with Rspec and Ruby, and use the Peepopen plugin for file search with Vim on my Mac.
Here are some tips to avoid the beginner frustration:
- Take it slow. There’s a learning curve, but it’s worth it.
- Don’t sweat plugins when you’re starting out. Yes, everyone says “use Pathogen, use Rails.vim, use xyz” and it’s absolutely correct. But it’s not essential when you’re starting out.
- Take it one step at a time. Learn about Vim via tutorials and blogs so you know what’s there, but don’t try and do everything at once. Keep that knowledge in the back of your head, get comfortable with one thing, then move onto the next. You’ll just end up confusing yourself if you do everything at once.
Focusing on items in the following order will allow you to build on your knowledge:
- editing text and searching, as that’s what you’ll be doing most
- file management, like opening, saving, and navigating to files
- other navigation, like jumping to lines or words and the power of hjkl (don’t use arrow keys!)
- manipulation like replacing text, cutting and pasting
- window and buffer management, including splits
- start using and learning plugins to see where you can eliminate friction
- start customizing your vimrc file to make the vim experience more comfortable now that you know the basics.