An Introduction to Vim

This post will represent some running notes regarding the use of the Vim editor. Why should you use something as arcane as Vim you ask? Well, sometimes you find yourself on a server or something that doesn’t have any other editors, so you should be know some Vim basics. Plus, it’s fun.

Vim is built on vi, an even more bare-bones editor. At the outset you should know that Vim has two modes, normal or command mode, and edit or insert mode. You add words and stuff in edit mode, and you perform operations like searching, saving, and moving around in normal, or command mode. (I prefer the edit/command terminology.) You can always get into the command mode by hitting ESC.

Oh my God. How do I get out.

This will probably be your initial reaction once you start using Vim. In order to quit the application you need to hit ESC to enter command mode, and then either :q! to quit without saving, or :wq for write and quit. Below is a table of useful get-me-out commands. Remember to hit ESC to get into command mode, and then hit Enter to execute the command. You’ll see the command at the bottom line of the terminal.

Command Action
:w Save (write)
:w! Save, ignoring warnings
:q Quit
:q! Quit, ignoring warnings
:x Save and quit
:wq Save and quit
ZZ Save and quit

You might be asking yourself, or me, “Where did ZZ come from?” I do not know. It was there when I learned all of this.

Moving around using commands instead of your arrow keys

Moving around at the dead nuts level is accomplished using the HJKL keys. The logic is that using these little guys, you don’t have to move your right hand four inches to the right to fiddle with the arrow keys. When you get used to it, you get it, but that’s how it is with most things.

Command Action
h Left
j Down
k Up
l Right

So that’s cool, but it kind of feels like you’re crawling through words. Stay cool, it gets better. You can move through a line of text by word, by sentence, or paragraph. You can emulate the same thing in other text editors by holding CTRL while you use the arrow keys. Here, w,W means that you can use either a lower case w, or an upper case W. These same goes for the rest of the comma delimited commands.

Command Action
w,W Go to the next word
b,B Go back a word
e,E Go to the end of a word
0 Go to the beginning of the line
^ Like 0, but ignores whitespace
$ Go to the end of the line
),( Move fw/bw by sentence
},{ Move fw/bw by paragraph
g Jump to the top of the document
G Jump to the bottom of the document
#G Jump to line #, e.g., `45G`
CTRL+O Jump to your previous location
CTRL+I Jump to you next location

If you want to scroll down, you can do so by full screen, or by half screen. The mnemonics are forward and backward for whole screens, and down and up for half screens. To execute these commands, hold the CTRL key, and hit f, b, d, or u.

Command Action
CTRL+F,CTRL+B Scroll fw/bw by a whole screen
CTRL+D,CTRL+U Scroll fw/bw by half a screen

Writing pretty words

So, now you can quit/save, and you can move around, but how do you actually write stuff? Well, the easiest way is to hit i, for insert. This takes you out of command mode and into edit mode, or insert mode, as it is variously called. At that point you can just type your little heart out, for instance, you can finally write that novel you’ve been yakking up on Tweeter.

Command Action
i,a Insert at/after the cursor
I,A Insert at the beginning/end of a line
o,O Open a new line below/above the cursor

Deleting some of those words

Not to be downer, but at some point you’re going to mess up some of those words you just typed. Fortunately, there’s a number of creative ways to strike those embarrassments from the digital record.

Command Action
x,X Delete under/before the cursor
dd Delete the whole line
dnd Delete n lines
d$, D Delete from the cursor to the end of the line
cw Delete the rest of a word, and enter edit mode
cc Delete the entire line, and enter edit mode
r Replace a single character, stay in command mode
s Delete a character and enter edit mode
S Delete the rest of the line and enter edit mode

Cut, Copy and Paste

Okay, this is where things get real. First, enter visual mode by hitting v, or V to operate on complete lines. Then manipulate the highlighted region using your movement operations, hjkl etc. Once you have your region of interest highlighted, either hit d to cut, or y to yank, which is Vim for copy. In bulleted format we have:

  1. Enter visual mode using v, or V for whole lines
  2. Manipulate the shaded region by moving the cursor around
  3. Cut with d, or copy/yank with y
  4. Move somewhere to paste the cut or copied text
  5. Use p to paste after the cursor; use P to paste before the cursor


You can search forward in the text for something in command mode by entering a forward (linux) slash, and then a “pattern” describing what you are looking for, and then hitting enter. You can cycle through the matches in the text by hitting the forward slash again, until you run out of matches. You can search backwards using a question mark, and cycle backwards using the question mark, also. So, what is a “pattern”? Welp, that is definitely something for another post. The short answer is that Vim supports regular expressions and POSIX bracket expressions. Searching will work as expected for regular words.

Command Action
/word Search for `word`, use `n` for the *next* occurrence
:%s/old/new/g Replace all instances of *old* with *new*
#/* Jump to the previous/next item matching the text under the cursor


These guys don’t fit in anywhere else, really.

Command Action
u Undo, think: CTRL+z
CTRL+R Re-do, or undo your undo
U Restore the whole line
. Repeat the last operation
:w !diff % - Diff between your latest unsaved changes and your last save

Leave a Reply

Your email address will not be published. Required fields are marked *