Kahibaro
Discord Login Register

Introduction to vim

Why vim?

vim is a powerful, keyboard‑driven text editor that comes preinstalled on almost every Linux system. Even if you prefer other editors, knowing basic vim is extremely useful because:

This chapter gives you just enough vim to open, edit, save, and quit files without panic.

Starting and Exiting vim

Opening files

bash
  vim filename.txt
bash
  vim

Inside vim, your whole terminal becomes the editor.

The three main modes (just the basics)

vim is “modal”: the same keys do different things depending on the mode.

For now, remember only these three:

You’ll mostly switch between Normal and Insert.

How to know your mode

When in doubt, press Esc once (or a couple of times). You’ll end up in Normal mode.

Quitting vim

These commands are typed from Normal mode, starting with ::

If vim refuses to quit with :q it’s usually because there are unsaved changes; use :wq to save them or :q! to discard them.

Basic Editing Workflow

Entering Insert mode (to type text)

From Normal mode, use one of these:

After pressing one of these keys, you’re in Insert mode and can type normally.

Leaving Insert mode

Develop the habit: type → Esc → move → i or a → type → Esc, etc.

Saving while editing

Moving Around in vim

Movement commands are given in Normal mode.

Arrow keys

Most terminals let you use arrow keys:

These are fine when starting; classic vim users often prefer h j k l.

Basic vim movement keys

You can use these instead of arrow keys:

Try resting your right hand on the keyboard: index on j, middle on k, ring on l, pinky on ;. Then h is just to the left of j.

Moving by words and lines

All from Normal mode:

Moving by lines and file

You can see line numbers by:

Deleting and Undoing

Deleting characters and words

All in Normal mode:

Deleted text is copied into a buffer (vim’s clipboard); basic paste is covered below.

Undo and redo

This works for almost all edits: inserts, deletes, etc.

Copying and Pasting (Yank and Put)

vim uses its own terms:

All commands here are in Normal mode:

For example:

  1. Move to a line.
  2. Press yy to copy it.
  3. Move somewhere else.
  4. Press p to paste it below the current line.

Combining Commands with Counts

Many Normal mode commands can be repeated by prefixing them with a number.

Examples:

Pattern: $N$<command>$ applies the command $N$ times.

Searching Inside a File

Search is done from Normal mode.

Basic search

After a search:

Search is case‑sensitive by default. To make it case‑insensitive just for one search, you can use \c:

Searching for the word under cursor

From Normal mode:

Then use n / N to move between matches.

Opening and Saving Files from Inside vim

These commands assume you’re already in vim, in Normal mode.

Opening another file

If the current file has unsaved changes, vim may warn you. Save first (:w), or force the edit with :e! otherfile.txt (discarding unsaved changes).

Saving under a new name

You’ll still continue editing the original file unless you also :e newname.txt.

Basic Visual Selection (Optional but Useful)

Visual mode lets you select text before operating on it.

From Normal mode:

Then:

Press Esc to exit Visual mode without changing anything.

Very Minimal Configuration for Beginners

vim is highly configurable but you don’t need much to start. For small quality-of-life improvements, you can create a simple configuration.

Creating a basic vim config

Create (or edit) ~/.vimrc using an editor you’re comfortable with (even vim itself):

vim ~/.vimrc

Add a few simple settings:

set number        " show line numbers
set ignorecase    " case-insensitive search by default
set smartcase     " but case-sensitive if search contains capitals
set hlsearch      " highlight search matches
set incsearch     " show matches as you type the search

These lines:

Save and quit (:wq). Next time you run vim, these options will be enabled.

Safe Practice Ideas

To build confidence:

  1. Create a test file:
bash
   vim practice.txt
  1. Try this sequence a few times:
    • Enter Insert mode: i
    • Type some text.
    • Exit Insert mode: Esc
    • Move with h j k l.
    • Delete a line with dd.
    • Undo with u.
    • Save with :w.
    • Quit with :q or :wq.
  2. Practice searching:
    • Add repeated words.
    • Use /word then n and N.

The goal at this stage is not to master vim, but to be comfortable enough that you can open, edit, save, and quit without getting stuck.

Views: 26

Comments

Please login to add a comment.

Don't have an account? Register now!