Skip to main content

1. Vim basics

  1. Starting and quitting.

    vim

    The tilde char (~) at the start of a line means that there is no text on that line.

    To quit press :q

  2. Editing modes.

    Let's start it again, passing to it the name of a nonexistent file:

    rm -f foo.txt
    vim foo.txt

    Vim has a command mode and an insert mode. In the command mode the keys that we type are interpreted as commands. In the insert mode we can add text to the file.

    When it starts up, Vim is in the command mode. To switch the mode to insert let's give the command: i

    Notice the status -- INSERT -- at the bottom.

    Now let's enter some text:

    The quick brown fox jumped over the lazy dog.

    To exit the insert mode and return to command mode, press: ESC

    To save the file type: :w

  3. Moving the cursor around.

    While in the command mode, we can move with the keys:

    • h -- left
    • l -- right
    • j -- down
    • k -- up

    Press a few times h and l.

    We can also use:

    • 0 -- to the beginning of line
    • $ -- to the end of line
    • w -- to the beginning of next word or punctuation char
    • W -- to the beginning of next word (ignore punctuation)
    • b -- backwards one word or punctuation char
    • B -- backwards one word (ignore punctuations)

    Try them a few times.

    If a command is prefixed by a number, it will be repeated that many times. For example 3w is the same as pressing w 3 times.

    We can also use the arrows (left, right, up, down).

  4. Basic editing.

    With the command i we start inserting text at the current position of the cursor. To append text after the current position we can use the command a.

    Type A to start appending text at the end of the line.

    Now type: . It was cool.

    Press Enter and continue by adding these lines:

    Line 2
    Line 3
    Line 4
    Line 5

    Then press ESC to switch back to command mode, then :w to save (write to file).

    • Go to the first line: 1G
    • Go to the last line: G
    • Go to the third line: 3G

    Then:

    • Open a new line below the current one: o
    • Undo: ESC and u
    • Open a new line above the current one: O
    • Undo: ESC and u
Loading asciinema cast...