Discovering Vim - A Return to Basics

I first met Vim (or more precisely Vi) as a teenager, on my Amiga. At the time, I only mastered a few basic commands, but there was something indefinably satisfying about coding in this austere terminal that fascinated me for obscure aesthetic reasons.

Then life took its course. Decades passed, punctuated by Netbeans, PyCharm, and increasingly sophisticated IDEs. I rediscovered Vim by chance, like finding an old friend. I greeted it with nostalgia, we had a beer and chatted all night as if we’d never been apart.

Today, I’m gradually adopting it, still in “work in progress” mode, discovering a new facet of its power each day. This tutorial is as much a guide for you as it is for me — a companion in this journey of rediscovering a tool that has stood the test of time for good reason.

Welcome to this return to basics.

“Vim is like a musical instrument. At first, you produce horrible sounds, but once mastered, it’s pure symphony… even if your colleague using VS Code will never see the difference.”

PART 1: UNDERSTANDING THE BEAST

What is Vim, and why inflict this on yourself?

Vim (Vi IMproved) is a text editor created by Bram Moolenaar in 1991 as an improvement on Vi (1976). If you’re looking for arguments to defend your choice, here are a few reasons:

  • Vim works on all Unix/Linux systems (even on that prehistoric server your company stubbornly refuses to update)
  • Everything is done with the keyboard, meaning you can edit text at the speed of thought (after only… a few years of practice 😉)
  • It consumes so few resources it would even work on a connected toaster
  • The satisfaction of intimidating your colleagues when you manipulate text at superhuman speed

Vim was created on an Amiga, a computer most current developers have probably never seen outside a museum. Its creator, Bram Moolenaar, continued to maintain Vim until his death in 2023, often alongside his humanitarian work for the ICCF Holland organization. Part of the donations to Vim went directly to child aid projects in Uganda.

Unlike “normal” editors where you type and text appears, Vim uses different modes. It’s as if your keyboard had multiple personalities, each serving a different purpose.

💡 FUNDAMENTAL CONCEPT: In Vim, you spend your time alternating between inserting text and manipulating that text. This separation is at the heart of Vim’s efficiency.

Here are the main modes:

Mode Function How to enter How to exit
Normal Navigate and manipulate text Esc (from any mode) N/A (default mode)
Insert Insert text (like a normal editor) i, a, o, etc. (from Normal mode) Esc
Visual Select text v, V, Ctrl+v (from Normal mode) Esc
Command Execute complex commands : (from Normal mode) Enter or Esc

Common trap: Beginners get stuck in Normal mode, type text, and are surprised when Vim executes random commands instead of writing their words. Remember: to write, you must first switch to Insert mode!

PART 2: SURVIVING IN VIM

🔍 EXERCISE 1: ENTERING AND EXITING VIM

“How to quit Vim?” has generated more views on Stack Overflow than most serious programming questions. Let’s start there to avoid having to restart your computer.

  1. Open a terminal and type: vim
  2. Admire the minimalist welcome screen (or empty, depending on your installation)
  3. To quit without doing anything:
    • Press Esc (to ensure you’re in Normal mode)
    • Type :q then Enter
  4. If you’ve made changes:
    • :q! to quit without saving (the ! is the computing equivalent of “I know what I’m doing, don’t ask me questions!”)
    • :wq to save and quit (w for write, q for quit)

Congratulations! You’ve already surpassed 50% of people who tried Vim for the first time.

The Vim community in action: In 2017, Stack Overflow revealed that over a million people had consulted the question “How to quit Vim”. The Vim community, rather than being embarrassed, made it a badge of honor. After all, it’s easier to enter Vim than to exit it… like a cult.

🔍 EXERCISE 2: BASIC MODES AND MOVEMENTS

Now that you know how to exit Vim, let’s create a file and learn how to navigate within it:

  1. Type: vim first_file.txt
  2. Enter Insert mode by pressing i
  3. Type the following text:
1
2
3
4
First line of text
Second line with various words
A third longer line to practice our movements in Vim
And a final line for the road
  1. Press Esc to return to Normal mode
  2. Try these movements and observe the cursor:
    • h (left), j (down), k (up), l (right)
    • w to move forward one word
    • b to move backward one word
    • 0 to go to the beginning of the line
    • $ to go to the end of the line

Why hjkl? You might wonder why Vim doesn’t use directional arrows like any civilized editor. The answer takes us back to the 70s: the ADM-3A terminal on which Vi (Vim’s ancestor) was developed had these letters printed on its keys with arrows. So yes, you’re using a navigation system designed for a terminal older than most of its users.

Insider tip: Even though arrows work in modern Vim, experienced users prefer hjkl because it avoids moving hands from the keyboard’s resting position.

🔍 EXERCISE 3: FIRST EDITS

Still in your file, let’s try some simple editing operations:

  1. Place your cursor on the first line (use k until you reach the top)
  2. Press dd to delete the entire line
  3. Type u to undo (yes, Vim has the undo function, it’s not as primitive as it seems)
  4. Position yourself on a word and press cw (change word)
  5. Type a new word and press Esc
  6. Place yourself at the beginning of a line and type A to go to the end of the line in Insert mode
  7. Add text and press Esc
  8. Save with :w then Enter

PART 3: THE VIM “LANGUAGE” - OPERATORS AND MOVEMENTS

Here’s where Vim begins to reveal its true power. Vim works like a language composed of three parts:

  • Operators: actions you want to perform (delete, change, copy…)
  • Movements: where you want to perform the action (word, line, paragraph…)
  • Modifiers: how many times or how to apply the action

Simplified Vim grammar:

1
[count][operator][movement or text object]

Main operators:

  • d: delete (and place in clipboard)
  • c: change (delete and switch to Insert mode)
  • y: yank (copy - yes, “yank” is the official term in Vim)
  • >: indent
  • <: unindent
  • g~: toggle case

🔍 EXERCISE 4: PRACTICING THIS GRAMMAR

Create a new file with vim grammar.txt and insert several paragraphs of text (enter Insert mode with i, paste text if you wish, then return to Normal mode with Esc).

Try these combinations and observe the results:

  1. dw: delete a word
  2. d3w: delete three words
  3. d$: delete to the end of the line
  4. d0: delete to the beginning of the line
  5. dd: delete the entire line
  6. 2dd: delete two lines
  7. yy: copy the entire line
  8. p: paste after the cursor
  9. P: paste before the cursor

Example in context: Imagine you have this text and your cursor is positioned on the “L” of “Lorem”:

“Lorem ipsum dolor sit amet.”

  • d3w will delete “Lorem ipsum dolor”
  • c$ will delete everything to the end and place you in Insert mode
  • y$ will copy everything from the cursor position to the end of the line

Text objects: another dimension

Vim isn’t limited to simple movements. It understands “text objects” such as words, sentences, paragraphs, and even text between parentheses or quotation marks.

Syntax: [operator]i[object] or [operator]a[object]

  • i means “inner”
  • a means “around” (including the delimiters)

🔍 EXERCISE 5: TEXT OBJECTS

Create a file containing code or text with different structures:

1
2
3
4
function exampleFunction(param1, param2) {
// A comment here
return "A string with quotes";
}

Try these commands:

  1. Place your cursor inside the parentheses and type di( (delete inside parentheses)
  2. Place your cursor inside the quotes and type ci" (change inside quotes)
  3. Place your cursor in the function and type da{ (delete around curly braces)

Vim philosophy: The Vim community often talks about “thinking in Vim”. It’s that magical moment when you stop thinking “I want to delete this word” and start thinking “dw”. Advanced users report that their fingers execute commands before their brain consciously formulates the intention. It’s like learning a foreign language - you know you’ve mastered it when you start dreaming in that language.

PART 4: ADVANCED TECHNIQUES

🔍 EXERCISE 6: SEARCH AND SUBSTITUTE

  1. In a file with lots of text, press / then type the pattern to search for
  2. Press Enter to start the search
  3. Use n to go to the next occurrence, N for the previous occurrence
  4. To replace text throughout the file: :%s/old/new/g
    • % means “the whole file”
    • s for substitute
    • g for global (all occurrences on a line)

Try this more complex command: :%s/\<word\>/new_word/gc

  • \< and \> mark word boundaries
  • c asks for confirmation before each replacement

🔍 EXERCISE 6.5: DELETING ALL PYTHON COMMENTS

Imagine you have a Python script filled with comments (probably made by ChatGPT) and you want to clean it up. Create a file commented_script.py with content like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# Example script with lots of comments
def hello_world():
# This function displays Hello World
print("Hello World") # Display the message

# Another example function
def add(a, b):
# Addition of two numbers
return a + b # Returns the sum

# Function calls
hello_world()
# Calculation and display
result = add(5, 3) # Addition of 5 and 3
print(result) # Display the result

To delete all comments from this file, you can use the power of Vim’s regular expressions:

  1. Open the file: vim commented_script.py

  2. Execute the following command: :%s/#.*$//g

    • # matches the comment character in Python
    • .* captures everything that follows the # until…
    • $ which represents the end of the line
    • The replacement is with an empty string (//)
  3. To also delete lines that contain only comments (and would therefore become empty):

    • Execute: :%g/^$/d
    • This command deletes (d) all lines that match the pattern “beginning of line followed directly by end of line” (^$), i.e., empty lines

Detailed explanation: This task combines two powerful Vim concepts: substitutions with regular expressions and the global command (g). In a single command, you can modify an entire file according to complex patterns, which would need to be done manually or with a script in other editors. It’s an excellent example of the efficiency Vim can bring to real editing tasks.

Marks and jumps

Vim allows you to “mark” positions in the text to quickly return to them.

🔍 EXERCISE 7: MARKS

  1. Place your cursor at an important location
  2. Type ma to set mark ‘a’
  3. Move elsewhere in the file
  4. Type 'a to return to the marked line, or `a to return to the exact position
  5. Try other letters to create multiple marks

Macros: automating repetitive tasks

Macros allow you to record a sequence of commands to replay them.

🔍 EXERCISE 8: SIMPLE MACROS

Imagine you have a list of names that you want to transform into an HTML table:

1
2
3
Alice
Bob
Charlie

Here’s how to proceed:

  1. Place the cursor on the first line
  2. Type qa to start recording in register ‘a’
  3. Execute these commands:
    • I<tr><td> (insert at the beginning of the line)
    • Esc (return to Normal mode)
    • A</td></tr> (add to the end of the line)
    • Esc (return to Normal mode)
    • j (go down one line)
  4. Type q to stop recording
  5. Type @a to execute the macro once
  6. Type 2@a to execute the macro two more times

Expected result:

1
2
3
<tr><td>Alice</td></tr>
<tr><td>Bob</td></tr>
<tr><td>Charlie</td></tr>

Warning about errors: Macros blindly execute the recorded keystrokes. If your text structure changes (for example, an empty line), the macro might do unexpected things. Experienced Vim users build robust macros that can handle these variations.

PART 5: CONFIGURATION AND CUSTOMIZATION

Vim is highly configurable via the .vimrc file in your home directory.

🔍 EXERCISE 9: CREATING A MINIMAL .VIMRC

Create or modify your ~/.vimrc file with these basic options:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
" Basic configuration for Vim
" Lines starting with " are comments

" Enable line numbering
set number

" Enable syntax highlighting
syntax on

" Smart indentation
set smartindent
set autoindent

" Tab size: 4 spaces
set tabstop=4
set shiftwidth=4
set expandtab

" Incremental search (real-time results)
set incsearch
" Highlight search results
set hlsearch

" Display cursor position
set ruler

" Enable mouse (yes, Vim can use the mouse, scandalous!)
set mouse=a

" Enable command-line completion
set wildmenu
set wildmode=list:longest,full

" Define a custom mapping (shortcut)
" Define <leader> as the Space key
let mapleader = " "
" Pressing Space+w saves the file
nnoremap <leader>w :w<CR>

Never blindly copy a .vimrc file without understanding what it does. Add options gradually, test them, and build your custom configuration. Your .vimrc is like your Vim fingerprint - it should be unique and tailored to your needs.

The editor war: The rivalry between Vim and Emacs users is legendary in the programming world. It began in the 70s and continues today. The two camps gently mock each other with jokes like “Emacs is an excellent operating system, it just lacks a good text editor” or “Vim is so difficult to exit that it has its own Stack Overflow page”. This rivalry is the geek equivalent of the Coke vs Pepsi wars, but with more keyboard shortcuts and repetitive strain injuries.

PART 6: PRACTICAL PROJECTS

🔍 PROJECT 1: CODE REFACTORING

Download a small, poorly formatted Python script (or create one) and use Vim to clean it up:

  1. Properly indent the code (=G in Normal mode at the beginning of the file indents everything)
  2. Rename a variable throughout the file (:%s/old_var/new_var/g)
  3. Reorganize functions by cutting/pasting
  4. Add comments

🔍 PROJECT 2: EFFICIENT WRITING

Write a short paragraph using only Vim, forcing yourself to:

  1. Never use directional arrows
  2. Use efficient movement commands (w, b, e, etc.)
  3. Edit with compound operators (cw, di(, etc.)
  4. Save with a custom shortcut

CONCLUSION: THE CONTINUING ASCENT

Congratulations! You’ve survived this introduction. You probably feel like you’ve barely scratched the surface, and that’s normal - Vim users are constantly learning new tricks, even after years of use.

Consider this tutorial as the beginning of a journey. As with learning a musical instrument, regular practice is more important than occasional intensive sessions.

Resources to continue your Vim journey

  • vimtutor: The built-in interactive tutorial (launch it in a terminal)
  • Vim Adventures: Learn Vim by playing a game
  • Vim Tips Wiki: A goldmine of tips
  • Practical Vim: An excellent book by Drew Neil
  • r/vim: The Vim Reddit community

Final tip: The best way to learn Vim is to use it for your daily tasks. Yes, your productivity will drop dramatically at first. Yes, you’ll be frustrated. But one day, without warning, your fingers will start dancing on the keyboard as if Vim were an extension of your thoughts. That day, you’ll understand why so many developers swear by this decades-old editor.


“In the universe of text editors, there are tools you use and tools you master. Vim is definitely in the second category - it doesn’t just ask to be used, it asks to be tamed.”