vi and Vim Introduction

Russell Bateman
March 2014
last update:

I wrote this for a friend contemplating getting into a real man's editor.

Here's a good cheat sheet for Vim which is a superset of vi. In Vim (http://www.vim.org/ and http://www.vim.org/download.php#mac), there's nothing that thwarts pure vi. However, nowdays, vi on anything like Linux and Macintosh is really console Vim.

http://www.viemu.com/vi-vim-cheat-sheet.gif

Of course, the problem with cheat sheets is understanding them. To help orient you, let's take the two examples from this morning. Reading the following and looking at the illustration will quickly suck the mist away from the cheat sheet.

  1. Mark a position using m to "set mark." We could attach this mark to any key on the keyboard, but we chose x ('cause "x marks the spot").
  2. Reposition cursor: this is done using arrow keys, which are an extension of vi. The normal movement keys are:
        h (left arrow)
        j (down arrow)
        k (up arrow)
        l (right arrow)
    

    These keys were chosen because the underlying rule in vi is "don't make the editor's hands stray from the keyboard proper in accomplishing anything."

  3. Having repositioned, we can do something with the range we've defined (as a delta between where the mark was dropped and the current position of the cursor).

    What we did this morning was based on row and column—delete from current character to character marked originally:

        d`x        delete[range] to x
    

    Alternatively, and perhaps more commonly, we could have used our make to handle a range of lines using the apostrophe (the other tick):

        d'x        delete [lines] to x
    

One last example to show you a peek into the sheer power of vi as a text processor. We're going to do some movement and replace tokens with other tokens. This short tutorial will take you about 5 minutes assuming you also spend the time to glance back at the cheat sheet as you do the exercises and contemplate what you're learning. If not, it will take you more like 3 minutes.

Let's replace some Java identifier with "dog".

  1. Hop into some Java code. (Don't worry, we'll make a way to escape without update.)
  2. Go somewhere down into the file, first using ^f (skips a page at a time), then ^d (skips half a page at a time). The opposite to these commands is ^b and ^u.
  3. Once you've arrived in Java code somewhere, press w repeatedly. This moves you ahead token by token (language-specific identifier by identifier). Now press W (uppercase) to see what that does. It skips from white space to white space. The opposite to w/W is b/B. To get to the end of tokens use e/E.
  4. Pick a token to fall on. Go to the beginning of that token/identifier by pressing b if you're not already there.
  5. Type cw, then type "dog", then press Escape. You've just changed whatever that identifier was to dog.
  6. Now go to another identifier nearby and press '.' (period). See how this simply repeated the command.
  7. Now go to a long token, position into the middle of it, and press dot again. See how this changed from where you were in the token to the end of it to "dog".
  8. Now, press u to undo this. u is a stack. Each time you press it, you undo the most recent change. The opposite to undo is redo; that's done using ^R. This is, however, not vi, but Vim. vi has no redo.
  9. Last, experiment with cW. This is the extended token version of cw. It will change everything to the next white space.
  10. Finally, escape out of the file without update:
        :q!
    

This last command wasn't even vi. It was ed. vi is based on ed which is the original UNIX line-oriented editor. The opposite of this command is either ZZ or :w to exit vi with update. To save without exiting, just do :w.

If you've followed along on the cheat sheet as you've done this little exercise, you should now better understand the cheat sheet and you've been given a tiny peek into vi's powerful text processing capability.