Vi/Vim Tutorial For Beginners

Vim and VI are wonderful text editors that give the user a lot of power and flexibility. There is a little bit of a learning curve to use VI/VIM though, so in this post we’re going to give you a beginners-only crash course in VI and VIM. (The names are used interchangeably. They are basically the same)

to Open VI, open your terminal and type in $ vi example.txt This will open up the example.txt file. If it doesn’t already exist, vi will create a new file called ‘example.txt’

VIM Documentation

VI Modes

The first thing to understand is MODES. You have by default three different modes at your disposal.

Normal Mode is where you are able to move around your file, search, save, quit, and run a bunch of other commands to do interesting things.

To enter Normal Mode, press the ESCape key.

Insert Mode is where you are inserting new text into the file. You can use backspace to remove text while in insert mode, but if you’re deleting more than a couple characters it will be faster to switch to “Normal Mode” or “Visual Mode” and erase text that way.

To enter Insert Mode, press the i or a key while you’re in Normal mode.

Visual mode allows you to “COPY”, “PASTE”, move text around, and a few other commands. With “Visual mode” you’ll see text highlighted. The highlighted text is what will be operated on.

To enter Visual Mode, press the v key. OR press Shift+V to select an entire line in Visual Mode.

Moving Around

The Slow way to move around is to use the arrow keys. The faster way to move around your files is to use Normal Mode. While in Normal mode you can move UP, DOWN, LEFT, and RIGHT by using the keys H, J, K, and L.

h – Left
j – Down
k – Up
l – Right

Why such strange keys? Because when you’re typing, your fingers are already on those keys! Instead of moving your hand all the way to the arrow keys, you just quickly enter Normal Mode and don’t have to move your hand off the home row. It’s much faster when you get the hang of it.

Other useful commands

:w – Write (Save) the file
:q – Quit. Exit Vi/Vim
:help – See help information.
$ – Move to end of line
0 (zero) – Move to beginning of line
6k – UP 6 lines. (use any number or direction; 4h is 4 places to the right.)
w – Move to next word. Try “4w” to move four words forward.
b – Move backwards a word.
gg – Move to the top of the file.
G – Move to the BOTTOM of the file.
dd – Delete the entire line
u – Undo the last change
CTRL+r – REDO. (Undo the undo)

Visual Mode

to enter visual mode, press v. Once you’re in visual mode you’ll be able to highlight text by moving the cursor around.

y – “yank” or “Copy” the highlighted text.
p – “Paste” the highlighted text that was yanked.
Esc – Get out of Visual mode (Sometimes have to press it twice)

Search

To search a file, press the / key. Then enter your search term. Then press Enter/Return to find the first result. Press n (lower case) to see the next occurrence. Press N (capital case) to find the previous occurrence.

Plugins

If you install VIM, you’ll also have the ability to install many different plugins that the VIM community has created. If you want extra functionality, be sure to check out the Vim plugins that are available.

Conclusion:

Vim and VI are incredibly powerful text editors. the information I covered here is the absolute minimum that you’ll want to know as a Vim user. Even if you never go beyond the basics, these commands will drastically improve your efficiency when working on files.

If you use VIM, VI, or Emacs often, you’ll want to re-map your Caps Lock key to Control or Escape so you don’t have to reach all the way up to the escape key all the time.

You must have an account and be logged in to solve challenges and lessons!

Submit a Comment

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