Skip to content

Linux Foundations / Find your way around Linux ยท Step 49 of 63

Lesson: Vim (Vi Improved)Switch
โ† Linux Foundations

Shell and files

Vim (Vi Improved)

Start Vim, understand its modes, and make a first edit without getting stuck.

Vim stands for "Vi IMproved". It is a keyboard-driven editor that runs in any terminal. It feels strange at first for one reason: it has modes. Once that clicks, the rest is practice.

Starting Vim

bash
vim notes.txt

If notes.txt exists, Vim opens it. If not, you get an empty buffer, and the file is created when you first save. A buffer is the copy of the text that Vim holds in memory. Your changes reach the disk only when you write them.

Empty lines past the end of the text are marked with ~. Those are not characters in your file.

If the vim command is not found, try vi. Debian and Ubuntu may provide only a small build as vi, which lacks some features and behaves more like the original. Install the vim package (Fedora: vim-enhanced) for the full editor.

The modes

  • Normal mode is where you start. Keys are commands: move, delete, copy.
  • Insert mode is for typing text. Press i to enter it.
  • Visual mode is for selecting text. Press v.
  • Command-line mode is for commands such as save and quit. Press : and the command appears at the bottom.

Press Esc to return to Normal mode. If you are unsure where you are, press Esc and you are on safe ground.

In full Vim, the bottom line tells you when you are typing text:

text
-- INSERT --

A first edit

  1. Run vim hello.txt.
  2. Press i.
  3. Type Hello from Vim.
  4. Press Esc.
  5. Type :wq and press Enter.

w writes the file and q quits. Check the result:

bash
$ cat hello.txt
Hello from Vim

If you want to leave without keeping changes, press Esc, then type :q! and Enter.

Getting help

Inside Vim, :help opens the built-in manual and :q closes the help window. :help i explains the i command. Small builds may not include the help files.

Full Vim packages also ship a hands-on tutorial. Run this from the shell:

bash
vimtutor

It opens a copy of a practice file, so you can experiment freely.

Try it

  1. Open hello.txt again with vim hello.txt.
  2. Press i, add a few words, then press Esc.
  3. Quit without saving using :q!.
  4. Run cat hello.txt and confirm that your new words are not there.
  5. If vimtutor is available, work through its first lesson.

Check your understanding

Vim (Vi Improved)

All 5 sections ยท no timer

Unlimited retries

Questions from every section are combined into one assessment. Answer them all correctly to complete the article.

Your completion saves in this browser. Sign in to sync it to your account.