2009-07-19

Vim: an Introduction

If you saw my favorite linux applications post, then you'll know that Vim is my text editor of choice. Vim is a very powerful text editor, but it does come with a bit of a learning curve. I figured I would present an introduction into Vim that should help in getting to the point where using Vim is not a challenge.

I suppose the first thing that we should do is cover how to open a file in Vim:

$ vim filename

This will open the file filename, and if the file does not exist, Vim will create the file once you save. Probably the first thing everyone notices when they first open up Vim is that they cannot immediately enter text into the file. The reason for this is that Vim is a modal editor with the following modes: Insert, Replace, Visual, Visual Line, and Normal modes. Vim opens up by default in Normal mode which is where you can issue commands to the Program.

Another common problem that people face when they are first learning to use Vim is that they have a hard time keeping track of which mode they are in. The first thing I will point out is that at the bottom of the Vim window, it does tell you what mode you're in; it will display the following:
  • -- INSERT --: When in Insert Mode

  • -- REPLACE --: When in Replace Mode

  • -- VISUAL --: When in Visual Mode

  • -- VISUAL LINE --: when in Visual Line Mode

  • Or it will be blank when in Normal Mode

Part of the reason that people have a hard time keeping track of what mode they are in is that once the open up Vim, they set it to Insert Mode and leave it there. If you only set Vim to Insert mode when you are actually inserting text into the file and leave it in Normal mode otherwise, this will cut down on the confusion and encourage you to learn Vim commands, rather than just using Vim like Notepad.

OK, enough for my introductional rant, let's get down to the business at hand and start talking about how to use Vim:

  • Changing Modes
    Before we start discussing the commands and how to use the various modes, it may be a good idea to know how to change between them so that you don't get yourself stuck somewhere in unfamiliar territory. The first thing I will point out is that from Normal mode, you can get to any of the other modes, but from any other mode, you have to return to Normal mode before you can change to a different mode. Here is a list of hot-keys that you can use to change modes:
    • i - Puts you into Insert Mode where the cursor currently sits

    • I - Puts you into Insert Mode at the beginning of the current line

    • a - Puts you into Insert Mode one character after where the cursor currently sits

    • A - Puts you into Insert Mode at the end of the current line

    • s - Puts you into Insert Mode and deletes the character immediately under the cursor

    • S - Puts you into Insert Mode and deletes the current line

    • r - Puts you into Replace Mode for only one character where the cursor currently sits

    • R - Puts you into Replace Mode and keeps you there where the cursor currently sits

    • v - Puts you into Visual Mode where the cursor currently sits

    • V - Puts you into Visual Line mode on the current line

    • Esc - Returns you back to Normal Mode



  • Moving around in Normal Mode
    The first thing I will say about Normal Mode is that there are, what I call, hot-keys and commands. Hot-keys are keys that when pressed do something immediately. Commands start with either the : character for normal commands or the / character for search commands. Now I will run down a list of hot-keys and commands for moving around in Normal Mode in Vim
    • h - Moves the cursor to the left one character

    • j - Moves the cursor down one line

    • k - Moves the cursor up one line

    • l - Moves the cursor to the right one character

    • w - Moves the cursor to the right one word

    • b - Moves the cursor to the left one word

    • $ - Moves the cursor to the end of the line

    • ^ - Moves the cursor to the beginning of the line

    • gg - Moves the cursor to the first line of the file

    • G - Moves the cursor to the last line of the file

    • :n - Moves the cursor to line number n


  • Searching for and Replacing Text
    I shouldn't even have to explain how important it is to be able to do find and replace commands inside a text editor. I use them all the time and they make things go a whole lot faster. Searching for text in Vim is very easy:

    /text

    Typing the above while in Command Mode will move the cursor to the next appearance of text after the cursor's current position. To move to the next appearance, simply type n; to move to the previous appearance, type N.

    Replacing text is a little bit more complicated, but here is the basic syntax:

    :[region]s/[text to find]/[text to replace with]/[options]


    This may look a little daunting, but let's just break it down one piece at a time:
    • [region]
      The region tells the command what section of the file to do the find and replace on. You can use a number to do the replace only on one line, you can use two numbers separated by a comma to replace between two lines inclusive, or you can use the % character to replace throughout the entire document. Here are three examples:

      :5s/find/replace/
      :5,10s/find/replace/
      :%s/find/replace/

      The first example with substitute the first instance of find with replace on line number 5. The second example will substitute the first instance of find with replace on each line between line numbers 5 and 10, inclusive. The third example will substitute the first instance of find with replace on each line in the entire file.

    • [text to find] and [text to replace with]
      These two sections are where the bulk of the work for the substitute command is done. This is where you identify the text that you want to replace and what you want it to be replaced with. The real power of these areas comes when you start using regular expressions, which I will cover in a future post.

    • [options]
      The options are where you tell the substitute command how to behave. If you do not specify any options, then it only performs the substitution on the first match on each line in the specified region. Here is a list of the options available:
      • g - This option will perform the substitution on all matches in the specified region

      • c - This option will prompt you for confirmation before making substitutions

      • i - This option will ignore case when looking for matches

      The above options can be combined. For example if you wanted to find do a substitue on al matches ignoring case, you could do:

      %s/find/replace/gi


    • Working with files
      To really be able to use Vim effectively, you're going to have to know how to open and save files as well as exit the program when you are done. Here is a list of commands for handling these actions:
      • :e [filename] - This will open the file [filename] in Vim

      • :w - This will save the open file

      • :sav [filename] - This will save the open file as [filename]

      • :q - This will exit Vim

      • :q! - This will exit Vim, discarding any changes since the last save

      • :wq - This will save the open file and exit Vim

      • :x - This behaves much like :wq, it will save the open file and exit Vim, but will not save if no changes have been made

    Well, that does it for a pretty basic introduction into Vim. See you next time.
  • 1 comment:

    1. wow. nice work. might not pry me away from my echo/pico/gedit/kate texteditor schema (ok, let's face it, my favorite is really doing TeX/LaTeX with lyx), but this will inspire some major exploration; every *nix system has vi and/or vim - even LiteBriteOS - so it's dumb not to learn. keep up the amazement, hardcore one.

      --easytoguesswho

      ReplyDelete