← formation/terminal
⌨️ Terminal

Linux - Editor - vim

📖 Cours🌶️ Facile

Introduction

To edit files, about every Linux or Unix operating system provides at least the vi editor. vi stands for visual editor. As the shell will only allow character based input and output, it is not as visual as editors that you may have used in a graphical environment.

vim is an improved version of vi that replaces vi in modern Linux operating systems. Don’t worry, vi and vim work the same for the basic commands introduced in this quest.

When you start vim, the first impression is very minimalistic. The minimalism has historic reasons: vi was first released in 1976.

🎯 Objectives

  • ✅ Know what vim is
  • ✅ Know the basic commands to use vim

👉 Start editing files with vim

To start the vi editor from the shell, just type:

vim hello.txt

When you start vim, you are in command mode. Typing characters will act as commands, so be careful what you type. You may navigate within the file with the cursor keys, but since the file hello.txt did not exist before we started the editor, a new empty file is created and there is nothing to navigate through.

To add some content, type the i character to enter insert mode. You can now type some text in the file. To leave insert mode, press the Esc key. You are now back in command mode.

  • To save the file, type : followed by w to write the content to hello.txt
  • To leave the editor, type : followed by q to quit

In case you get lost, you can almost always leave vim by pressing Esc followed by :q!

You can watch this video to learn some more basic commands and how to use vim:

Lessons learned

Using vim has its pros and cons. A major pro is that you can do everything with the basic keyboard: no need for the mouse or special keys like the cursor keys. If you are fast with the keyboard, vim is a very fast editor.

The major cons are the commands: you have to remember which character corresponds to a certain action. Using a cheat sheet as a reference for the most important commands will help you learn the basics really fast.

Some cheat sheets with the basic commands:

You can also use your search engine to find a cheat sheet that suits your preferences of color, or one with more detailed descriptions.

🔬 Exercise

To get used to the basic commands in vim, you have to practice. Type in some text: it can be anything. If you have no idea at all, describe a hobby or sport you did, or look for some news of the day. You should have at least 10 lines of about 60 characters each.

📝 Quiz

1. How do you undo the last undo?

Voir la réponse

In command mode, Ctrl + r (redo). u undoes, Ctrl + r redoes what was undone.

2. What does the D command do when used in command mode?

Voir la réponse

It deletes everything from the cursor to the end of the line.

3. What happens when you add an exclamation mark to the q command?

Voir la réponse

:q! forces vim to quit without saving: all unsaved changes are lost.

☝️ Summary

This quest introduced the vim editor. You now know the basics and can use vim to create text files.

💪 Challenge

The challenge for this quest is practice. Work with vim for at least one hour and learn at least 10 additional commands. Write down a few notes about the commands you learned.

Things to look for:

  • Change from lowercase to uppercase, or uppercase to lowercase
  • Copy and paste lines or blocks
  • Search and replace

🧐 Acceptance criteria

  • ⭙ You used at least 10 vim commands on a file and feel reasonably comfortable
  • ⭙ You bookmarked a vim cheat sheet that you can use in the future

Don’t worry, most people do not use vim for software development: it is basically a minimal common denominator available on all Linux operating systems.