Cut, Copy, & Paste in Vim

Cut, Copy, & Paste Before we get to any of these, remember that in a graphical Vim client like MacVim all your normal OS cut, copy, and paste commands like ⌘x, ⌘c, and ⌘v (on the Mac) will work just fine when combined with mouse selections. The following commands will make a huge difference in your speed because, they’ll enable you to efficiently rearrange code without ever reaching for the mouse. [Read More]

Editing your .vimrc file

Vim’s core configuration file can be found at ~/.vimrc Without it you’ll have the default configuration: no syntax highlighting, no line numbers, etc.. ~/.vimrc is where you put all your global stuff, like turning on syntax highlighting by default. Editing your ~/.vimrc is something you shouldn’t fear. It’s actually pretty difficult to screw up your Vim configuration so badly that it’s unusable, and in a worst-case-scenario you can always tell vim to ignore it upon launch by saying vim -u NONE and then remove whatever you borked. [Read More]

Emergency Vim Quickstart

EMERGENCY VIM QUICKSTART You’ve ssh’d onto a server. You need to edit a file now. You need to use vim, and you haven’t a clue. You don’t have time for long explanations. Let’s go! READ THIS NOW You’re in the terminal so normal mouse usage for editing isn’t an option. Sorry, that’s a terminal thing, not Vim. Vim has a writing mode, and a not writing mode. You type i to start Inserting text into the document. [Read More]

Hello World in Vim

Every programmer starts a new language by writing hello world, and as you’ll see, Vim is a small DSL for text editing. So, here goes. On the command line, tell Vim you want to create a new text document called hello_world.txt. Note, there are some great grapical Vim clients if you’re not a fan of working on the command line. $ vim hello_world.txt Once you’re in vim, tell it you want to insert some text into this file [Read More]

Line Numbers in Vim

Geeks like line numbers. I think Vim geeks really like line numbers, because we use them for more than just finding a specific place in the code. Jumping to a line number Out-of-the-box you’ve got three choices. Just pick the one you like best and don’t worry about remembering the others. I’ve used the 1st one for years and never had any need for the others. Minimal typing Type the number of the line you want to go to followed by capital G. [Read More]

Opening Files in Vim

When you launch Vim It’s important that you always launch Vim from the command line and at the root of your project. I’ll explain why later in the section on projects, but for now just remember to start your work from there. From your shell We already saw this one in the hello world: $ mvim path/to/file.txt Every time you do this you’ll open up a new window. [Read More]

Searching in Vim

In most editors searching is just a way to ask your editor where you can find something. In Vim it’s also a tool for extending your selection, deleting chunks, and more. That probably sounds weird, and slow, but trust me. Once you get the used to it, it’s spectacular, and well cover it in the sections on Cut, Copy, Paste and visual mode. Vim’s default search behavior is, in my opinion, excessively restrictive, and unhelpful. [Read More]

Split Windows In Vim

Split windows are one of my favorite features. Many modern editors allow you to see multiple documents on the same screen in a “split window” but very few of them allow you to see different parts of the same document in split windows. I find this to be incredibly useful when working on large class files. Imagine you’ve got an unfamiliar method defined near the bottom of a file, but it is called from the top of the file. [Read More]

Undo in Vim

Undo & Redo Sooner or later you’re going to make a mistake. I’m sorry to hit you with these hard truths, but well, we’ve just got to accept them and move on. Before we get to any of these, remember that in a graphical Vim client like MacVim all your normal OS undo, redo commands like ⌘z and ⌘⇧z on the Mac will work just fine. The following commands are a little faster (some of them at least) and will work in the terminal too, but if you’re still just getting started I encourage you to come back to this section later. [Read More]

Vim Buffers

You can think of a buffer as a piece of text loaded into memory. They’re especially useful when you need to move text between files using Vim’s built-in copy / paste functionality. You can yank and put between any buffers in the system, but you can’t do it between buffers in other windows. For example. I have two windows open in MacVim (this could be two separate terminal windows too). [Read More]