Vim


Last updated: Nov 25, 2014

 

Table of Contents

  1. Introduction

  2. Navigation

  3. Inserting

  4. Deletion

  5. Yanking/Putting

  6. Searching

  7. Substitution

  8. Changing Case

  9. Replacement

  10. Marking

  11. Spell Checking

  12. Visual Mode

  13. Miscellaneous

  14. Files

  15. Multiple Windows

  16. Help

  17. Keyboard Mappings

  18. Vimrc Files

  19. Plugins

  20. Vim as Python IDE

 

Introduction

I have been using Vi/Vim for about 13 years now. I thought I knew most of what there was to know about Vim until this year when I decided to see what else I could do with Vim and wow, I didn't realize how little I was utilizing it's capabilities. This was primarily as a result of finding an article about using Vim as a Python IDE. I discuss this more in the plugins section.

This is not an intro to Vim document. It is meant more as a reference document after you learn the basics. There are MANY websites and books available where you can learn vi. Here are one's that I recommend:
  1. Execute the command vimtutor on Linux for a very good introduction to Vim

  2. Another two good built-in introduction to Vim documents:  :help usr_02 and :help intro - run these commands from within Vim. If you never navigated the Vim documentation before, there are some tips that I have here

  3. Learning the vi and Vim Editors - the best book available on the subject

  4. Swaroop CH's Byte of Vim - a very good introduction (plus more) to Vim. He also has a good intro to Python book if you are interested in that.

This document does not include every single Vim command especially if they are very basic (ie. what the navigating commands "hjkl" do) nor more advanced Vim topics (ie. nothing on scripting at the moment). You can think of it as a Vim cheat sheet or user manual, but I feel it is much more then that which is why I am referring to it as a reference document. I have included the commands/capabilities that I often forget or that I feel may not be as well known to the casual Vim user. Also something that I have not seen on many other Vim related websites, I include the most common commands to the plugins as well as where to go for additional information on said plugins. I hate having to track down the help for a plugin that I only use occasionally.

Note1: some of the commands are in ex mode. Make sure you notice if there is a colon (:) or not before a command. This makes a big difference.

Note2: if a letter below is capitalize, then ensure you capitalized it when using it.

Note3: below is the environment that I use Vim in. If you use something different, there may be some slight differences in the below commands/plugins: *Most of the time I have to install the full Vim package with the command:
apt-get install vim because most distros only come with the minimial versions of Vim (vim-common and/or vim-tiny).

If you feel any of the content in this document is wrong or there is something that I should add, I would appreciate it if you would contact me and let me know. Thanks!

 

[Table of Contents]

 

 

Navigation

I use the below scroll commands and record commands constantly. The record commands allow you to record a series of steps and then perform them again later on. I just learned about the move commands and I feel I will be using them more. See :help motion for more info on navigation.

Scrolling: Record Commands: Move to In ex mode (when you type a colon ":" then a command), the following are shortcuts:

 

[Table of Contents]

 

 

Inserting

 

[Table of Contents]

 

 

Deletion

 

[Table of Contents]

 

 

Yanking/Putting

Yanking and putting text is a very useful feature

 

[Table of Contents]

 

 

Searching

Help:  :help search and :help usr_27

Note, also look at the substitution section for more string matching options that you can also use when searching

 

[Table of Contents]

 

 

Substitution

Vim's substitution capabilities are one of it's most powerful features. A few helpful notes before jumping into the examples:
  1. Many examples have the percent sign character '%' which means perform the change on the entire file. If this is not included the substitution works only on the current line (or a range of lines if you specify that).

  2. The caret character '^' matches at the beginning of a line and the dollar sign '$' at the end

Now for some examples:
One of the most powerful features of substitution is being able to use regular expressions. Below are some examples I have used:
Help:  :help substitute, :help 10.2, or Substitution on vimdoc

 

[Table of Contents]

 

 

Changing Case

This sections shows the different options to change the case of characters and words

Help: Switching the case of characters

 

[Table of Contents]

 

 

Replacement

This sections deals with word and line replacement

Change:

 

[Table of Contents]

 

 

Marking

You can place an invisible marker in your file that you can reference later. Note, these are deleted once you close the file. Help:  :help mark-motions

 

[Table of Contents]

 

 

Spell Checking

Yes, that's right, Vim has spell checking and it rocks!

Here are the most useful commands:
If you are using my advanced .vimrc file, I mapped the following keys: Help:  :help spell

 

[Table of Contents]

 

 

Visual Mode

I just started using visual mode, but I really like it for certain things. My favorite use of it so far is to quickly indent or add comments to a block of code.

Note: to get out of any visual mode, hit the <ESC> key and wait about 3 seconds. Help:  :help visual-mode

 

[Table of Contents]

 

 

Miscellaneous

 

[Table of Contents]

 

 

Files

Here are useful commands when working with files. Note some of these commands talk about editing multiple files at the same time. This is similar, but different then working with multiple windows. Both of them allow you to yank, put, delete, copy between files

 

[Table of Contents]

 

 

Multiple Windows

One of the best/coolest/most useful features of Vim is the ability to work with multiple windows in a single Vim session. Here are some of the most useful commands.

 

[Table of Contents]

 

 

Help

Vim has very good built-in documentation, but it can be a bit difficult to navigate. Here are some tips to help:

 

[Table of Contents]

 

 

Keyboard Mappings

In Vim you use keyboard mappings to create keyboard shortcuts. Many people like to map the function keys (F1-F12). Mappings are an extremely useful feature, but the syntax of defining mappings is a bit complicated. Hopefully this section will clarify mappings for you and allow you to create some of your own to do what Vim does best, save you time.

To understand Vim keyboard mappings, you have to know the different Vim modes, which are the following: Now that you understand the different modes, it should be clear what the different mapping commands do: You also need to understand the "noremap" command. This basically tells Vim not to look for another keyboard mapping after loading the current one. This prevents mappings from conflicting with eachother. Each mode has their own version: When defining keyboard mappings, you need to know how these common keyboard keys are represented: It is always good to learn from examples. I took the one from Vim's manual and expanded what it does:

:map <F2> GoDate: <Esc>:read !date<CR>kJ

This will map the <F2> key (in normal, visual, and operator-pending mode) to do the following: For additional help I recommend:  :help usr_40

 

[Table of Contents]

 

 

Vimrc Files

Here are my two Vimrc files. The basic one is the one that I used for almost 10 years, but as I mentioned in the introduction this past year I set out to see what I was missing and if you compare the two files you will see it was substantial! Both files are commented so they should be pretty much self explanatory.

Here are the two files:

 

[Table of Contents]

 

 

Plugins

These are the plugins that I have installed. They are meant for programmers, but I believe any Vim poweruser will find the majority of them useful. These plugins are all included in my advance vimrc file above. I list the most common commands for each plugin as well as how to get additional help. This is something I have found lacking in other people's discussion of Vim plugins. Sure you can read the documentation, but I find it handy to have a reference for the most used commands and how to get additional help if needed.

*Note1: I have another section specifically for Python plugins below

*Note2: any where below that has :help you can shorten it to :h

 

[Table of Contents]

 

 

Vim as Python IDE

I use Vim as my Python IDE. I got the idea from this article among a few others. The plugins that I list above provide many functions of an IDE that can be used in any programming language. Below are a few other plugins that are installed which are Python specific. If you use Vim as your Python IDE and find a plugin very useful that I do not have below, I would appreciate it if you would contact me and let me know. Thanks!

 

[Table of Contents]