Kahibaro
Discord Login Register

2.3.3 Introduction to vim

Getting Started with Vim

Vim is a powerful text editor that runs in the terminal. It is different from simple editors like nano because it uses modes for different tasks. Beginners often find vim confusing at first, but once you understand its basic ideas it becomes very efficient for editing text and code.

This chapter focuses on getting you comfortable with starting vim, moving around, making simple edits, and saving your work. More advanced features will be covered elsewhere.

Starting and Leaving Vim

You usually start vim from the terminal. To open vim without a file, type:

vim

To open or create a specific file, give its name:

vim notes.txt

If notes.txt exists, vim opens it. If it does not exist yet, vim prepares a new empty file with that name.

Vim is famous for being confusing to quit. The trick is that most commands that involve saving or exiting start with a colon, which you type in Command mode. A few essential exit commands are:

To quit vim safely:

  1. Press Esc to ensure you are in Normal mode.
  2. Type :q then press Enter to quit, if there are no unsaved changes.
  3. Type :wq then press Enter to save and quit.
  4. Type :q! then press Enter to quit and discard changes.

If you try :q when you have unsaved changes, vim refuses and shows a warning. Use :wq or :q! depending on whether you want to save or discard the changes.

Vim’s Modes

The most important idea in vim is that it has different modes. The same keys do different things in each mode. For basic usage you need to know only three of them.

When you first open a file, vim starts in Normal mode, also called Command mode. In this mode, keystrokes are interpreted as commands, not as text.

Beginners must remember:

  1. Normal mode is where you move around and run commands.
  2. Insert mode is where you type text.
  3. Press Esc at any time to return to Normal mode.

Normal mode is for navigation and actions such as deleting, copying, and searching. Insert mode is for entering text, similar to how most other editors behave. There is also Command line mode, where you type commands that begin with : for saving, quitting, and more.

To enter Insert mode from Normal mode, use one of these keys:

i inserts text before the cursor.
a inserts text after the cursor.
o opens a new line below the current line and starts Insert mode.
O opens a new line above the current line and starts Insert mode.

To leave Insert mode and go back to Normal mode, press Esc. If something strange seems to be happening, pressing Esc a few times is often the safest way to get back to a known state.

Moving Around in Normal Mode

In vim, you move through the file mostly in Normal mode. You can use the arrow keys, but vim also has its own movement keys that work consistently on all terminals.

The basic movement keys are:

h moves left one character.
l moves right one character.
j moves down one line.
k moves up one line.

You can also use:

0 (zero) to jump to the beginning of the current line.
$ to jump to the end of the current line.
w to move to the beginning of the next word.
b to move to the beginning of the previous word.
G to jump to the last line of the file.
gg to jump to the first line of the file.

Movements in vim can be repeated by putting a number in front of them. Many other commands follow this same pattern.

To repeat a movement or action, type a number before the command.
For example, 5j moves down 5 lines and 3w moves forward 3 words.

You can also jump to a specific line number. In Normal mode, type :10 then press Enter to go to line 10. This uses Command line mode.

Entering and Editing Text

To insert new text, first return to Normal mode with Esc, then choose how to start Insert mode.

For example, to start inserting text at the cursor, press i. Everything you type now appears as text in the file. To finish and return to Normal mode, press Esc.

A typical editing sequence looks like this:

  1. Use Normal mode commands to move the cursor to the right place.
  2. Press i, a, o, or O to enter Insert mode.
  3. Type or edit your text.
  4. Press Esc to return to Normal mode.
  5. Save with :w or save and exit with :wq.

You can also perform simple edits without entering Insert mode. Some useful Normal mode commands are:

x deletes the character under the cursor.
dd deletes the current line.
u undoes the last change.
Ctrl + r redoes an undone change.

Many commands can be combined with counts. For example, 3x deletes three characters starting at the cursor, and 2dd deletes the current line and the next one.

Saving Your Work

Vim does not save changes automatically. You need to save explicitly using Command line mode.

From Normal mode, type:

:w then press Enter to write (save) the file.
:w filename to save the current content to a new file name.
:wq to save and quit in one step.

If the file is read only or you do not have permission, vim might warn you. In that case, you usually need to save the file with elevated privileges or write to a different location.

If you look at the bottom of the screen after :w, vim reports how many lines and characters were written. This helps confirm that your changes were saved.

Copying, Cutting, and Pasting

Vim can copy and paste text using its own registers. For basic editing you can treat some operations as simple cut and paste.

In Normal mode, the commands are:

yy copies (yanks) the current line.
2yy copies two lines starting at the current line.
p pastes the yanked or deleted text after the cursor or below the current line.
P pastes before the cursor or above the current line.

When you use dd to delete a line, vim also stores that line in the same place used by yy. This means you can delete a line with dd and then paste it elsewhere with p. This is similar to cutting and pasting in other editors.

Because vim uses the same storage for yanked and deleted text, using yy after a dd replaces the previous deleted text. If you plan to paste something that you deleted, do not overwrite it with a new yank before you paste.

Searching Inside a File

Vim can search for text within the current file using Normal mode and Command line mode. To search for a word or pattern, make sure you are in Normal mode and type:

/word then press Enter.

Vim moves the cursor to the next occurrence of word. To repeat the search forward, press n. To repeat in the opposite direction, press N.

You can search upward instead of downward by using ? instead of /. For example:

?word then press Enter searches upward for word.

Search is case sensitive by default in many vim setups. You can change this in configuration, but that belongs in a more advanced discussion. For now, assume that word and Word may be treated differently unless your system is configured otherwise.

A Simple Vim Workflow

To put these pieces together, consider a very small example session.

Suppose you want to create a short note file called todo.txt:

  1. In your terminal, run vim todo.txt. Vim opens the file, which is currently empty.
  2. You are in Normal mode, so press i to enter Insert mode. At the bottom you may see -- INSERT --.
  3. Type your notes, for example:
   Buy milk
   Learn vim basics
   Update system
  1. When you finish typing, press Esc to go back to Normal mode.
  2. To correct a typo, move with h, j, k, l or the arrow keys to the mistake, press i, fix it, then press Esc.
  3. When you are satisfied, type :wq and press Enter to save and quit.

If you reopen todo.txt with vim todo.txt, your text will be there, ready for further changes.

Practicing Without Fear

Vim feels unfamiliar at first because of its modal design, but a few core habits make learning easier.

Get used to pressing Esc to return to Normal mode. This gives you a safe starting point to run commands, move around, or exit.

Practice common commands until they are automatic: i to insert, Esc to leave Insert mode, :w to save, :q or :wq to quit, h, j, k, l for movement, dd to delete a line, u to undo.

To experiment safely, open a temporary file such as vim test.txt, try commands, and then quit without saving using :q!. You can repeat this as often as you like without risking important data.

As you become more comfortable, vim can become much faster to use than simple editors, especially for larger files and code. For now, focus only on starting vim, understanding modes, doing basic navigation and edits, and saving your work.

Views: 8

Comments

Please login to add a comment.

Don't have an account? Register now!