Beginner's Guide to Vim


2013-05-25

I recently switched from using Sublime Text 2 to Vim. Sublime is an awesome text editor, but I enjoy using command line tools and am intrigued by the massive following that Vim has. Also, three of the developers I work with use Vim and seem to know a lot of the tricks that make Vim indispensible. Although I haven't gleaned much from them yet, I have found my way around Vim at my own pace. Here are some tips for Vim beginners.

First, you can start of by opening Vim and editing a test file. Just open a terminal and type:

vim testvim

This will open vim with the file test open. The key thing with Vim, from the beginning, is that it has different "modes". We'll start with the two most often used: normal and insert mode. Vim opens in normal mode. This mode is used for moving around in a text file. And move Vim does, often cited for allowing you to move quickly around the file without touching your mouse or trackpad.

So, you're in normal mode now, after you typed vim testvim. Without any text, there's not much need to move about. So you should switch Vim's insert mode now, by hitting your "i" key. Now the bottom of your terminal should say -- INSERT --. This means you can insert text now. So type away, and Vim will act like your plain old text editor by dutifully placing the characters you type into your file.

Go ahead and type or paste this text so I can show you how to move around in Vim with your keyboard:

This is how we Vim. Vim, a magical editor, allows you to quickly 
manipulate files with deft fingers. This results in much happiness
and here are some "special" characters for fun: _-(&!

You can paste into most terminals with Shift+Control+V. Now that you have pasted some text in, you can exit Vim's insert mode and go back to normal mode by pressing the escape key. Here are a few key shortcuts for moving around the document in normal mode. Just press the key for each letter to use the shortcut:

  • j - go down one line
  • k - go up one line
  • l - go to the right one character
  • h - go to the left one character
  • f+any character - press "f" then any character (like "a") to go to the next occurence of that character
  • F+any character - press "F" then any character (like "a") to go to the previous occurence of that character
  • * - go to the next occurence of the word that your cursor is at
  • e - go to the end of the current word
  • b - go to the beginning of the current word
  • 0 - go to the beginning of the current line
  • ) - go to the end of the current line

There are many more shortcuts for normal mode like those above, but that will get you started.

Vim also has commands that can be given from normal mode using the ":" character. To use these commands, enter normal mode then type ":" and your command immediately following it:

:w

Typing the ":w" command above and pressing enter will tell Vim to save (or "w" for write) the file you are working on. Here are a few more commands like this:

  • :q - close the current file
  • :q! - close the current file without saving changes
  • :wq - save the file then close it
  • :e file_name - edit a different file called file_name, you can place any file name or path and file name there.
  • :20 - move to line #20
  • :s/cat/dog/g - replace all "cat"s with "dog"s on the current line
  • :%s/cat/dog/g - replace all "cat"s with "dog"s in the current file

There is a ton more features in Vim (a ton, really), but I'm going to leave it here for now since this is a beginner's guide. Get out there and discover Vim's awesomness by using your favorite search engine. And check back here for updates, as I learn more about Vim myself. Oh yeah, one last Vim trick that I use a lot is pressing "o" in normal mode. This will insert a new line below the line you are on and change to insert mode for you to start typing.