Lesson 4: VIM – The Text Editor That Thinks You Like a Challenge


lesson4

Lesson 4: VIM – The Text Editor That Thinks You Like a Challenge

Welcome to Lesson 4 of Mastering the Linux Terminal. Today, we step into the world of VIM – the text editor that makes even the bravest Linux users sweat a little. If you’re looking for a text editor that makes you work for it, congratulations! You’ve found your match. But don’t worry, by the end of this lesson, you’ll go from “Why is this happening?!” to “Oh, this is kinda fun!”

What is VIM?

VIM is short for Vi IMproved. You might think, “Improved? Compared to what, a Rubik’s Cube?” Exactly. It’s a supercharged version of the original Vi text editor. Once you get the hang of it, you’ll feel like a keyboard ninja. But first, there will be confusion. Lots of it. Ready? Let’s go!


Step 1: Installing VIM – Your First Boss Battle

Before you can wield the power of VIM, you need to install it.

On Ubuntu (or any Debian-based distro):

sudo apt update
sudo apt install vim

Boom! You’re now the proud owner of a brand-new, shiny, slightly confusing text editor.

On Arch Linux:

sudo pacman -S vim

Nice work! You’ve installed VIM on Arch, which automatically boosts your Linux street cred by 200%.


Step 2: Starting VIM – Enter the Text Editing Arena

To start using VIM, simply type:

vim yourfile.txt

Immediately, you’ll be greeted with an empty, blinking cursor. This is where the magic happens… or doesn’t, depending on how quickly you figure things out.

You’re currently in Normal Mode, which, in VIM language, means “You can look, but don’t even think about typing.” Yes, you read that right. Let’s fix that.


Step 3: Modes in VIM – Like Hats, But For Editing

VIM isn’t just one mode. Oh no. It’s like a puzzle with different layers of chaos, each with its own set of rules. Here’s a quick guide to the main modes:

  • Normal Mode: This is the mode VIM starts in. You can move around, but typing? Ha! Not here.
  • Insert Mode: This is the mode where you can finally type. It’s like being handed a pen after begging for it.
  • Visual Mode: Think of this as VIM’s version of “highlighting text.” It’s as fun as it sounds.
  • Command Mode: Here’s where you give VIM actual instructions, like saving or quitting. You’ll become very familiar with this one.

Step 4: Navigating VIM – No Arrows Allowed!

So, you’ve opened VIM, and your hands instinctively reach for the arrow keys. But VIM doesn’t care about your instincts! In fact, it laughs at them. Instead, it uses four letters to move around the text: h, j, k, and l.

  • h = Move left. Think of it as “h for ‘help me,’ I’m lost!”
  • l = Move right. This one’s easier to remember: “l for lateral.”
  • j = Move down. Imagine the letter “j” drooping downwards like it’s lazy.
  • k = Move up. It’s like an arrow pointing up, except… it’s not.

Speed Pro Tip:

Hold down any of these keys to move faster through the text. Just be careful not to go too fast or you might overshoot and end up in places you didn’t want to be. (And then blame VIM for it.)


Step 5: Insert Mode – Now You Can Finally Type!

Tired of staring at the screen without being able to type? Let’s fix that. To switch from Normal Mode to Insert Mode, press i. That’s it. You’re now in Insert Mode! Go ahead, type your heart out.

Finished typing? Hit Esc to return to Normal Mode. Think of Esc as your panic button. Anytime you’re confused or something weird happens, just smash Esc. It’ll save you, I promise.


Step 6: Saving and Quitting – Your Biggest Challenge Yet

Once you’ve typed something, you’ll want to save your work and exit VIM. Sounds simple, right? Well, welcome to VIM, where nothing is as simple as it seems.

Here’s how to do it:

  • Press Esc to make sure you’re in Normal Mode.
  • Then type:
:wq

This is VIM-speak for “write and quit.”

If you want to quit without saving (because who needs those changes anyway?), type:

:q!

The ! lets VIM know you mean business.


Step 7: VIM’s Hidden Superpowers

VIM isn’t just about typing and quitting. It has tons of tricks up its sleeve. Let’s explore a few that will make you look like a pro:

  • Save without quitting: If you want to save your work but stick around for more editing, type:
:w
  • Undo: Press u to undo your last action. If only life had a u key!
  • Redo: Hit Ctrl + r to redo something. VIM believes in second chances.
  • Search: Type / followed by the text you want to find and press Enter. VIM will take you to the first match faster than a search engine on caffeine. If you want the next word n. If previous N.

Step 8: Advanced VIM: Changing Multiple Words – Power Mode Activated!

You’ve mastered the basics, and now it’s time to show off some advanced skills. Let’s say you need to change several instances of a word in a file. Normally, you’d scroll through and manually replace each one. But not with VIM. Oh no, VIM can do it with the speed of a ninja!

Example: Changing All Instances of a Word

To replace all instances of a word with another word throughout the entire file, use this command:

:%s/oldword/newword/g

Let’s break that down:

  • :%s – This tells VIM to search the entire file (%) for something (s stands for search).
  • oldword – This is the word you want to replace.
  • newword – This is the word you want to replace it with.
  • g – This means “global,” so VIM changes every occurrence in the file.

So, if you accidentally wrote “duck” when you meant “dog,” you’d type this:

:%s/duck/dog/g

Boom! Every duck in your file is now a dog. That’s the power of VIM magic.

Selective Changes (Because Power Needs Control):

If you want to be asked for confirmation before each change, use:

:%s/oldword/newword/gc

The c stands for “confirm,” so now VIM will ask for your permission before replacing each word, giving you complete control over the chaos.


Step 9: Master Text Manipulation – Because Why Stop at Words?

VIM doesn’t just stop at changing words; you can manipulate whole sections of text in ways that make you feel like a wizard. Here are some more tricks:

Delete Multiple Lines

Want to delete multiple lines in one go? No problem! Here’s how:

  • In Normal Mode, move to the first line you want to delete.
  • Type d followed by the number of lines you want to delete. For example, to delete 5 lines:
d5d

Change a Word with Lightning Speed

To quickly change a single word, move your cursor to the start of the word and type:

cw

This puts you in Insert Mode and lets you immediately start typing the new word. It’s like cutting out the middleman of deleting and re-typing!


Step 10: Become the VIM Master

VIM may feel like an overwhelming puzzle at first, but with time, you’ll be editing text faster than you ever thought possible. The key is practice, patience, and maybe a little bit of caffeine.


Fun Exercise: Conquer the Basics and Advanced!

  1. Install VIM if you haven’t already (see above).
  2. Open a text file with:
vim myfile.txt
  1. Enter Insert Mode with i and type a short sentence.
  2. Press Esc to go back to Normal Mode.
  3. Navigate using h, j, k, l like a pro!
  4. Replace a word using:
:%s/oldword/newword/g
  1. Save your changes with:
:w

And quit with:

:q

Congratulations! You’ve now unlocked the advanced VIM techniques. You’re no longer just editing text – you’re mastering it.



See also