Introducing Vim

Posted by Grego on July 15, 2015

I decided to put some vim notes together for my students, and I figured I could share with the rest of the world. Below is a quick set of notes with everything you’ll need to get started with vim.

Open It Up!

Let’s start with opening vim. If you’re on the command line then the command is just vim:

vim

Typing just vim will open up vim and you’ll get a screen like:

Picture of vim starting screen

Quit vim.
You can quit vim by typing :q in Normal Mode, we’ll discuss modes in a little bit, so bear with me here.

If the :q didn’t work, try pressing ESC a few times and try again.

If you want to open a specific file, type in vim followed by the file name. Typing:

vim HelloWorld.java

will open a file called HelloWorld.java. If the file does not exist, a new file will be created once you execute the write command :w. Write in vim is equivalent to save.

Modes

Let’s talk about modes now. Vim is what’s called a modal editor (pronounced mode-uhl). What the !@#$% does that mean?!

It means that vim has multiple modes, depending on what mode your in keys pressing certain keys on the keyboard will have a different effect.

Here’s a list of modes:

  • Normal ► Keys are more than meets the eye!
  • Insert ► This is what you’re used to. You press buttons on the keyboard and the characters appear on the screen.
  • Visual ► Selects text
  • Command ► Used to issue commands to vim: save, open, quit, etc…
  • Search ► Find text in the current file.

Motion Keys

When in Normal Mode, you can use the h j k l keys on the keyboard to move around as if they were arrow keys. The advantage of this is that you get to keep your fingers close to the [home row][] of the keyboard, which dramatically saves time since you don’t have to reposition your hands again to type after.

Picture hjkl as a set of arrow keys:

    k
h       l
    j
  • h goes left, since it is the leftmost key of the group of 4
  • l goes to the right, since it is the rightmost key of the group.
  • j goes down because it sort of looks like a down arrow (↓)
  • k goes up because I said so. Don’t question it.

So,
h j k l

Other types of Motion Keys

any means any key

key description
w Move 1 word forward.
b Move 1 word backward.
g g Jump to the top of the file.
G Jump to the bottom of the file.
^ Jump to the beginning of the line (just like [regex][])
$ Jump to the end of the line (also like regex).
f + any Go forward to the next occurrence of specified character on the same line.
F + any Go backwards (reverse of f) to previous occurence of character on the same line
t + any Go until the next occurrence of specified character on same line (goes to the character before it)
T + any Reverse of t.
; Repeat last f or t motion.
, Reverse last f or t motion.

Changing Modes

So how do you get from one mode to the other?

Normal ► Insert

key description
i insert at current position
I insert at the beginning of the line
a insert after the current position
A insert at the end of the line
o insert underneath current line
O insert above current line (reverse of o)
s delete current character and insert
S delete the whole line and insert
ESC return to Normal Mode.

Normal ► Other Modes

key description
v enter visual mode, use motion keys to make a selection
V enter line visual mode, selects an entire line at a time
ctrl + v enter block visual mode, allows you to select columns
: enter command mode
/ enter search mode
r replace mode (single character only, returns to normal mode after replacement)
R replace mode

Deleting, Copying and Pasting

In vim deleting is cutting.

mov means a motion key

key description
x delete (cut) single character, remember because ✗ is bad!
d + mov deletes (cuts) a specified motion
d d deletes (cuts) a whole line
y “yanks” or copies a selection made with visual mode
p “puts” or pastes a selection that was previously yanked.

Other Useful Tricks

Undo / Redo

u will undo in normal mode.
ctrl + r will redo.

Indenting

You can indent a selection of text by making your selection with V, then using > to indent and < to reduce the indent. Using the . key will repeat the last operation. You can always use undo (u) to undo an indent to put it back if you go too far.

Command Mode… Commands.

key description
:e open a file, if no name is specified, a blank file is opened.
:w write file saves the file. If a file name is given, it will be saved with the new name.
:q quits vim

You can also combine some of the file operations, for example:

:wq will write (save), then quit.

[bear picture]: