M19 (MRBO02) exercises and additional material
Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Back to homepage

Introduction to Vim

What is vim?

Vim is a command line text editor on Linux. You could edit files totally via your keyboard without using mouse. It has a lot of hotkeys. Different from those GUI text editors like Notepad++, etc on Windows. Scientific computing is usually done on Linux system and graphical interface is not an indispensable part of it. That’s why people use command line tools on Linux. When you use to vim, you will realize vim is also very convenient. But it is indeed difficult for new users to learn.

Introduction to Vim

Let’s open a file or creat a new file called test.txt. Here vi is the executable command for vim.

$ vi test.txt

Vim Modes

Upon opening the file, vim has two basic modes that allows you to deal with your text:

  • command mode (default mode): You could save or quit the program in this mode (and execute other advanced commands).
  • insert (or edit) mode: You could edit text contents in this mode.

Vim is by default in command mode when one open the file. To change to insert mode by typing i. Then you could edit files freely. After you have finished editing the file, press esc to go back to command mode. In brief, i and esc are two hotkeys to vary between command mode and insert mode.

Vim command mode hotkeys

In command mode, if you didn’t edit the file, just quit vim editor, type :q

If you edit it and want to save a file, type :w

To save and quit a file, type :wq

To quit without saving a modified file, type :q!

To jump to line number 52, type :52

To search a word, say “Ja”, in a file, type :/Ja, then type n to search next “Ja” or type N to search previous “Ja”

Some other hotkeys in command mode

key action
to move to top of file
to move to bottom of file
to delete current line
to undo
to redo

Here is a vim cheat sheet to help you search for the function you need.