The first day with Vim was awesome! I was under the impression that Vim was going to be much harder to learn than it has been. I didn't have a whole lot of time to devote to Vim on day one, but I did use it for absolutely every bit of editing that I needed to do including setting up Ghost for this blog. But day two things really began to sink in.

Note: In the examples I give below I try to indicate the individual or combined keystroke by surrounding them in <>.

Vimtutor

I completed the first section of Vimtutor today. It's a pretty simple section that explains the very basic of basic commands needed to navigate through a file and do basic edits. It's enough to stumble around and get things done, but not enough to do any serious editing.

Lessons

1.1) Moving the Cursor
1.2) Exiting Vim
1.3) Text Editing - Deletion
1.4) Text Editing - Insertion
1.5) Text Editing - Appending
1.6) Editing a File

Modes

First, before moving around a text file, it's important to understand that there are several different modes that you can be in while using Vim. The default mode is Normal. Normal is "navigation and manipulation" mode. This is the mode that you want to be in to navigate to other lines or to a certain place in a line. It is also the mode you want to be in to delete characters or even whole lines.1

If you find yourself in a different mode than Normal, and you want to get back just hit ESC or whatever key you have mapped to ESC.

Lesson 1.1 - Moving the Cursor

The first section of the tutor starts off by explaining how to move the cursor around a file. When I say cursor I mean the little blinking block (or vertical line) that almost all text editors use as an indicator of where text will be inserted or deleted with the next keystroke.

Moving around a text file is extremely easy. "j", "k", "h", and "l" (lower case "L") are the keystrokes that get you around while in Normal mode.

  • k = up
  • j = down
  • h = left
  • l = right

Lesson 1.2 - Exiting Vim

Needing a lesson on how to exit a text-editor may sound ridiculous, but because Vim is not contained in a Graphical User Interface it isn't immediately apparent how to exit the editor. Not only is it not apparent, but there are several different ways to exit the editor.

  • :q = Exits the editor if no changes have been made.
  • :q! = Exits the editor without saving changes
  • :x = Exits the editor. If there are changes it will save, if not it will just exit.
  • :wq = Exits the editor and always saves. If you wanted to save an empty file, this would be the command to do so.

Notice that each command is prefixed with the <:> character. Also to use these commands to exit the editor you must be in Normal mode.

Keystroke Examples:

  • <ESC><:><q> exits the editor when there are not changes to save
  • <ESC><:><wq> exits the editor and saves, changes or no

Lesson 1.3 - Text Editing - Deletion

Deletion is an important part of editing text. Especially if you are in experimental mode (not a Vim mode as far as I know, but a coding mode). There are, of course, more than one way to delete things in Vim, but the tutorial only covers one of those way in lesson 1.3.

  • x = delete one character at a time

<x> (lowercase), will delete whatever character is under the cursor at the time, and again is used while in Normal mode.

Deletion not covered in Lesson 1.3

As I said above, there are many ways to delete things in Vim.

  • dd = deletes a line
  • ci" = deletes anything inside the quotes where the cursor is
  • ci' = deletes anything inside the single-quotes where the cursor is

Keystroke Examples:

Line 1: This is the first line that we will delete
<ESC><dd> will delete Line 1 completely, assuming the cursor is somewhere on that line

Line 2: This is a line "with quotes"
<ESC><ci"> will delete all characters between the open and closed quotes. Line 2 would look like this after the command: Line 2: This is a line ""

Line 3: This i a line 'with single-quotes'
<ESC><ci'> will delete all characters between the open and closed single-quotes. Line 3 would look like this after the command: Line 3: This is a line ''

You can start to see a pattern with the <ci"> command. c means "change", and i means "inside". The third character are the tags to change the text between.

At this time, those are the only ones I know about, but I wouldn't be surprised if there were a whole slew of other deletion commands.

Lesson 1.4 - Text Editing - Insertion

Insert is another Vim Mode, and is used to insert text into the file. After navigating to the line and column that you want to insert text, press the <i> key to enter Insert mode. You can then begin typing to add text.

The concept here is pretty simple so I don't think there is much more to talk about for insert mode.

You can however delete text in this mode as well by using <backspace>.

Lesson 1.5 - Text Editing - Appending

Appending text is basically the same thing as inserting text except that you will begin inserting text behind the cursor rather than in front of it. To append text the command is simply <a>.

Lesson 1.6 - Editing a File

Lesson 1.6 is basically an exercise for you to practice making changes to a file, saving it, and reopening it to see that Vim does indeed act like a normal text editor and saves your file.

Summary

My first couple of days with Vim was not as scary as I scary as I thought it was going to be, and I wasn't entirely unproductive. I wouldn't trash my editors at work at this point, because I still don't know how to open files from within vim, or how to quickly navigate a file. All-in-all, I'm liking working in Vim. One big advantage I've already observed is the fact that Vim is everywhere. I used it when I ssh'd into the server to setup this blog, and I used it to write this blog, and the intro blog to this series.

I'm pretty excited to see what the next few days have in store for me, and can't wait to share!

Series

  1. Introduction
  2. Part Two

References: