DEV Community

Cover image for Vim Tutorial
Raymundo Alva
Raymundo Alva

Posted on

Vim Tutorial

Vim is a text editor that is included in Unix-like operating systems. It is known for being fast and efficient because it is a small application that can run in a terminal and because it can be controlled completely with your keyboard. There is no need for any menus or the use of a mouse. So why use Vim? Well if you are a programmer you will find that moving your hand to your mouse and to the arrow keys can sometimes kill your flow and slow you down. Vim can help you edit large amounts of text in fewer key strokes than other editors. If you are a programmer or a linux enthusiast you will find yourself using a text editor a whole lot. I am going to show some of the commands needed to get started using Vim.

Vim is focused on changing existing text as much as adding new text. That makes it different from other editors. Because of this Vim has different modes that allow you to do different things.

Normal Mode

By default Vim starts out in “Normal Mode”. To access this from a different mode we use the Esc key. In normal mode key presses don’t behave like in other text editors. Instead of inserting text you can navigate the text file.

Moving The Cursor

  • h key moves left
  • j key moves down
  • k key moves up
  • l key moves right

This is the main way to move the cursor around. It might be a little weird but this way allows you to keep your hands on the keyboard. With many Vim commands you can add a number to the command and it will repeat. For example, if you type 5j the cursor will move down five lines. If you type 6l the cursor will move 6 spaces to the right.

You can also move to the beginning/end of the line

  • 0 will move the cursor to the beginning of the line
  • $ will move the cursor to the beginning of the line

This mode also allows you to make some changes to single characters.

  • x will delete the character that is currently selected by the cursor
  • r will replace the selected character with a new character

In this mode we can also undo and redo changes.

  • u will undo changes
  • Ctrl +r will redo (undo your undo)

Insert Mode

Insert mode will be the most familiar mode. This is where typing will insert text, just like any other text editor. You can access insert mode by using one of a few available insert commands.

  • i will immediately switch you to insert mode
  • I (capital) moves the cursor to the beginning of the line and enters insert mode
  • a stands for ‘append’ and will move the cursor to the end of the current word and enter insert mode
  • A will move the cursor to the end of the line and enter insert mode
  • o inserts a new line below the current line and enters insert mode on the new line
  • O inserts a new line above the current one and enters insert mode on the new line

There are also other ways to enter insert mode but these are the most common ones that will get you started. Remember that to get out of insert mode back into normal mode you can use the Esc key.

Visual Mode

Visual mode is used to select text, just like you would when dragging with your mouse. When selecting text we can allow commands to copy, delete, or replace the selection. In order to make a text selection we have to press the v key to enter visual mode and create a starting selection point. After that moving the cursor to the end selection point will highlight your text.

  • v will enter visual mode
  • V will enter visual mode and make text selections by line

Command Mode

Command mode has a wide variety of commands that we cannot use in normal mode. To enter command mode we type “:” from normal mode followed by a command. You will be able to see the command at the bottom of the screen. It is a popular joke that people sometimes accidentally enter vim and then don’t know how to exit the program. There are many ways to quit Vim but we can easily do it in command mode.

  • :q! will quit Vim without saving changes
  • :wq will quit Vim and save changes

Vim has many other commands that you can get from the documentation with the help command, :h or :help.

Replace Mode

In replace mode we can replace existing text by directly typing over it. Move the cursor to the character you want to replace and by pressing R (capital r) you will enter Replace Mode. Now whatever you type will replace the existing text. It is very similar to how it will be in insert mode. The difference is that the text is replaced instead of having text added.

This is all you need to start practicing Vim and editing text. Maybe at first it will be very confusing and sluggish to type on but after some time you will see that this way of typing will really make you more productive. If you want to learn more about using Vim you can open a terminal and type vimtutor to have access to some Vim lessons that should only take about 30 minutes to learn. If you want to learn Vim in a fun way you can also visit Vim Adventures. This is a web based game that will help you learn use some of the most useful Vim commands. Have fun with Vim and happy coding! 😎

Top comments (0)