An Introduction To Vim/vi

One of the most used text editors on Linux and Unix systems is Vim. Standing for VI iMproved it has replaced the original vi editor on many Linux distributions. It’s done this to such an extent that when you use the vi command on Ubuntu, CentOS or other distros what you are really getting is Vim. It has a reputation for breaking beginners to Linux. Those who accidentally open a file using the command then have no idea how to edit the file or even exit the program. Fear not though, while the interface may seem complicated and frightening to start with, it can be quite simple to use once you understand it; we promise.

Vim: Insert, Command and Last-Line Modes

The first thing to note is that Vim, and vi before it, are programs that date back to a time before the common key combinations we now take for granted came into common consciousness. This happened with the advent of the graphical user interface. As such the control of Vim/vi relies on the software working in multiple modes rather than using modifier keys like ctrl or alt to signify commands. There are three modes when working with Vim, first we have insert mode which is used to add text to and edit documents, command mode is used to issue commands to Vim and finally there’s last-line mode.

Starting Vim In Command Mode

When you start Vim, whether loading a file or starting with a blank file, you will begin in command mode. This allows you to enter any commands that you require. Watch out for the first level of confusion that happens for beginners. In modern text editors, the alphanumeric keys are only used for entering text unless some form of modifier is selected. In Vim these keys double as command keys for navigation in the file, entering insert mode in a number of different ways and entering more complicated commands. Entering a colon (:) in command mode puts you into last-line mode, allowing longer commands to be entered on the bottom line of the editor. From command mode the simplest way to enter insert mode is to press the i key (note the lower case, upper case key presses can have different functions to their lower case versions). The escape (Esc) key is used to escape from insert mode back to command mode.

Open File And Enter Insert Mode

On the basis that many people are looking at how to get into Vim, edit the file and get out quick for systems that lack a simpler text editor, here’s what you need to know. Once you open the file, press the i key to enter insert mode, make your changes as though you were in a simpler editor and then hit the escape key to return to command mode. Press : then w then q and hit enter. You’ve now saved your changes and exited the file.

For a touch more detail, the :w command informs Vim to save (write out) the file, the :q command tells Vim you want to quit. As seen above the :w and :q commands can be chained to become :wq to save and exit. If you’ve made changes and you don’t wish to save, then you can use the command :q! with the exclamation mark noting you are happy to lose changes.

Moving Around in Vim

So let’s look at moving around in Vim. As well as the arrow keys, you can use hjkl to move around. This may seem strange at first, but it allows touch typers to move the cursor around without having to lift their hands from the keyboard, saving time and effort. Let’s look around some common navigation commands:

h moves the cursor to the left

j moves the cursor down

k moves the cursor up

l moves the cursor right

^ moves the cursor to the start of a line

$ moves the cursor to the end of a line

w moves forward one word

b moves backward one word

These commands can be prefixed with a number, meaning that by typing 5b you can move backwards by 5 words, 15j moves down 15 lines and 7l moves 7 characters to the right.

Sometimes you aren’t happy with your changes, but fortunately Vim has an undo function:

u is used to undo the last operation

Ctrl-r will redo the last undo

Again prefixing the undo command with a number will undo that many changes.

Finally, we’ll quickly touch on copy and pasting, a common function to want to perform. In Vim this is done by highlighting the text then yanking it and pasting it back.

First navigate to the text you want to copy then activate highlighting with:

v highlights by character

V highlights by line

Ctrl-v highlight by column

Once you’ve activated highlighting, move the cursor around to highlight the text. Once highlighted, press the y key to “yank” the text into the copy buffer. You can then move the cursor to where you want to paste the text and press the p key to paste it.

That pretty much covers some very basics about using Vim, although to say we’ve barely scratched the surface is somewhat of an understatement.

Never miss another post. Sign up for the weekly 100TB newsletter and follow us on Facebook and Twitter.

Originally published at

Show your support

Clapping shows how much you appreciated’s story.