Outline
- Introduction
- Vim as a child.
- Vim's teen years.
- Vim's license and how it helps children in Uganda.
- The Emacs "holy war".
- Getting started
- Downloading, installation, opening and closing.
- Vim modes.
- How to configure/customize Vim.
- Turning on syntax highlighting and other common settings.
- Normal mode
- Respect the power of your colon (:).
- Regular expressions are your friend!
- Common searches.
- Common search and replace strings.
- Multiple windows.
- Visual mode
- Finding your way around town.
- Selecting blocks of text.
- Other commands you can't live without.
- Unleashing Vim
- Macros and custom commands.
- Running shell commands from Vim.
- Useful Plugins.
- Expanding your Vim universe
- Books
- Web resources
- Communities
Introduction
Vim as a child.
Vim stands for Vi Improved, which was a named coined by Bram Moolenaar, who initially wrote Vim as a Vi clone for his (then) blazing fast Amiga 2000. After 20 years of stagnation, Vi was reinvented as Vim became the most popular Vi clone.
Vim has benefitted from the open source development model, which you can read more about in an excellent essay by Eric Raymod called The Cathedral and the Bazaar
. Through countless contributions, Vim has risen to the top of the Vi clones, and mostly due to some strong leadership by Bram Moolenaar, which presents itself in the form of two primary principles regarding Vim contributions:
- Additional features must have a useful purpose.
- Additional features must be documented.
Vim's teen years.
Through the years, Vim has gained popularity and now has a community, which can be seen through the collaborative effort put into vim.org
. It has grown a graphical user interface (GUI) port for windowed desktop environments in the form of gVim, and has multiple resources on the web (much like this one) that explain its history, use, and vast array of user-developed plugins and tweaks.
For a detailed history of Vim along with other information, see:
Vim's license and how it helps children in Uganda.
Unlike some of its alternatives, Vim carries with it a unique license. Vim is what is commonly referred to as "charityware", which encourages users who appreciate the usefulness of the application to donate to a particular charity. In this case, Bram Moolenaar chose the Kibaale Children's Center in Uganda. Read the license here
.
If you have wondered why you see Help poor children in Uganda! when you first open a blank document, the license is why (it's no joke!). Here is what you'd see:

In case you are wondering what is in the background, here is the full image
(it was my wallpaper at the time).
The Emacs "holy war".
As a fun side-note, I wanted to mention the Emacs vs. Vi holy war
, and how it's been extended to Vim (being that Vim is a Vi clone). I think Vim has a stronger case against Emacs, given stronger support for plugability and window management than any other Vi clones. Throw in gVim to the mix and you've got a big argument for Vim.
That said, Emacs is actually a great program, and if I had any idea how to use it I might give a workshop on Emacs too. But, I love Vim so much I don't feel the need to switch, and I'm not a huge fan of switching text editors for the sake of switching.
I encourage you to try Emacs, and give it a shot – and stick with it if that is what you feel is the best tool for you. You should always use the best tool for the job.
Keep in mind – the workshop title, "A Vim workshop for Emacs users" is just a joke, so don't flame me.
Also, (this is for you, Donnie) you don't have to be an Emacs user to come to the workshop or find this page useful. You should just have the need for a robust, flexible and very powerful text editor that reduces the amount of keystrokes required to get from point a to point b for any given task (and doesn't require any use of your mouse!).
If you have that need, and I think most technical people do at one point or another, then this is a great workshop for you.
Getting started
Downloading, installation, opening and closing.
Getting Vim
- You can get Vim in a variety of ways:
- Downloading the binary from the vim web site.
- Installing it using your package management system (YaST, Synaptic/apt-get, emerge, etc.) – this is probably the easiest.
- Downloading source code from CVS.
- I'll leave this mostly up to you. For more information, see the Vim download page
.
Ok, so now what?
- Run the installer, or run the command, depending on which download method you used.
Ok, so NOW WHAT?
- Once you have run Vim, you will find yourself at the screen above. Mess around, see what you can figure out on your own. You'll find it to be very frustrating if you haven't used Vim before, but alas - that is what we will talk about!
- For starters, to get out of Vim, type
then hit enter.
Now we're ready to get things rolling! 
Vim modes.
- Vim has two main modes:
- Visual mode – Visual mode lets you highlight/select blocks of text. If you are in visual mode, you will see a VISUAL indicator at the bottom of your Vim window.

- Insert mode – Insert mode is what you're probably most familiar with. This is when you are inserting text as you would be in something like notepad or word. When Vim is in insert mode, it displays the INSERT indicator at the bottom.

- Normal mode – Normal mode is the default, and this is the mode you use for most Vim commands, searching, replacing, etc. No indicator means you are in normal mode.
- It is important to understand which mode you are in, because the behavior of Vim changes depending on mode. If you get unexpected results when you try a command, ask yourself, "is this command for this mode?"
- On a side note, if you get unexpected results when typing commands, check to make sure you don't have Caps Lock on – this has been known to cause headaches for even experienced Vim users, since commands are case-sensitive. The headache only lasts for as long as you don't know that Caps Lock is on.

Switching between modes.
- To exit the current mode, use <ESC>.
- To enter INSERT mode, you can use <i> or any number of optional commands (see the man pages for more commands):
- <a> - starts after what is currently selected.
- <s> - replace what is currently selected and enter insert mode.
- To enter VISUAL mode, use <v>.
How to configure/customize Vim.
- Using Vim as your default editor might require a little tweaking. You'll want to set these in your shell configuration files (.bashrc for example). Depending on what you're using Vim for, the environment variables might change. For Subversion, here is an example of how you could set your default editor:
- The .vimrc file contains most configuration items for Vim. Here are some example entries:
syn on
set formatoptions=crt
set textwidth=80
set expandtab
set shiftwidth=4
set softtabstop=4
set tabstop=4
set autoindent
set number
Turning on syntax highlighting and other common settings.
Some of the more common settings you'll want to be aware of right off the bat.
" enables syntax highlighting
syn on
" sets format options in INSERT mode
" c - Automatically wrap comments. Insert the comment leader automatically.
" r - Insert comment leader in a comment when a new line is inserted.
" t - Automatically wrap text when the textwidth value is reached. If
" textwidth is not defined, text will not be wrapped.
set formatoptions=crt
"set textwidth=80
" determines how far something shifts when you use >> or <<
set shiftwidth=4
" determines how many spaces your tabs shift text on your screen
set tabstop=4
" copies the indent from the previous line when you insert a new line
" below it
set autoindent
" makes Vim show you line numbers on the left
set number
Whenever you are programming, it is good practice to make sure you are aware of any formatting/project standards that have been set for your project. This will ensure that files you edit with Vim (or any other editors, for that matter) are consistent with the rest of the code in that project.
A good thing to know, particularly, is how indentation is done in your project. Some projects use tabs, and some use spaces. Using the wrong one could get you into some serious trouble. 
Setting up Vim to only use spaces:
" use only spaces instead of tab characters when you hit <tab>
set expandtab
More information on tabs/spaces:
The bottom line is - follow whatever standard you promised to follow. If you don't, you will be asked to reformat (if you're lucky), and you will be assaulted or embarrassed in public (worse case).
Visual mode
The whole point of visual mode is selected blocks of text in order to mark it for an action. Almost everything in Visual mode follows a simple 3-step procedure:
- Enter Visual mode (<v>)
- Highlight what you want to play with
- Perform the action you want to perform
Finding your way around town.
In Visual mode, the commands for moving the cursor are about the same as normal mode. Here are some quick tips:
- <h> - move up
- <j> - move down
- <h> - move left
- <l> - move right
Selecting blocks of text.
- The first thing to do is find/highlight what you want to modify. Get your cursor at the beginning or end of the block you want to select and hit <v>.
- Use <h>,<j>,<k>,<l> to get around one column or row at a time (or other keys like <CTL-F> or <CTL-B> for jumping a page at a time, or <gg> or <G> to go to the start/end).
- You will see a visually highlighted block in your window. From here, we can perform a variety of actions, like:
- <dd> delete it
- < >> > move it to the right
- < << > move it to the left
- <y> yank it into the buffer
- See cheat sheet for other commands.
Other commands you can't live without.
Once you've got highlighting and selecting down, you can do some cooler stuff:
-
- <u> make something lower case
- <U> make something upper case
- <J> join the lines
- See cheat sheet for other cooler commands.
Normal mode
Respect the power of your colon (:).
Vim application commands can be executed by first hitting <:>. There are a number of commands you can perform from the <:> prompt, like basic commands for opening, splitting, closing, saving and many others.
Regular expressions are your friend!
Regular expressions can be used to make somewhat complex tasks very easy to do. Used in combination with ranges, you can accomplish almost any seemingly impossible search or replace task. So before you start making the same edit 200 times (once per each line) make sure to think about what you could possibly do with : and a healthy regular expression.
Common search tips.
Searches are done by hitting / and they can be a quick way to find anything in your document matching a certain string or pattern:
- /^ - go to the next new line
- /$ - go to the next end of line
- /^[a-z] - go to the next line that starts with a lower-case letter
- /^[^a-z] - go to the next line that doesn't start with a lower-case letter
Common replace tasks.
- :%s/\t/ /g - replaces all tabs with 4 spaces
- :%s/ /\t/g - replaces all 4 spaces with tabs
- :%s/foo [.]*/bar \1 whatever/g - replaces all instances of foo with bar, adds what was after it, then appends whatever.
Multiple windows
Vim offers multiple window management. Using the :split command is one of the more useful commands, so make sure to play around with these.
Unleashing Vim
Macros and custom commands.
- Type q + some other character to label your register to start recording.
- Perform your actions.
- When you are done recording your macro, hit <q> again.
- To execute your macro, type @[the register you chose].
" for example, look at the keystrokes below
qa
^
v
$
U
q
Executing the keystrokes above will get you a macro that:
- is stored in register 'a'
- goes to the beginning of the line
- highlights everything to the end of the line
- changes the selected text to upper case
To call this macro, you type <@a> because 'a' is the register you stored it in.
Running shell commands from Vim.
One of Vim's strengths is how well it is integrated into *nix systems. Running shell commands from inside of Vim is one of the cooler things you can do:
- !ls -l – lists current directory
- !ls -l | grep foo – show listing results that contain foo
Useful Plugins.
Expanding your Vim universe
Fitting all of Vim into one session would not be feasible. The best way to get more familiar with Vim is to use it.
Once you start using it, it will grow on you and you will appreciate how easy it is to edit any text document through a simple shell. The :help command is invaluable, so you should use that first. If that fails, see some of the resources listed below.
Books
- I highly recommend Vi IMproved--Vim
, a book written by Steve Oualline. You can pick this up and learn something new everything you flip to a new page.
Web resources
Communities