1. Vim basics
-
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
-
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
-
Moving the cursor around.
While in the command mode, we can move with the keys:
h
-- leftl
-- rightj
-- downk
-- up
Press a few times
h
andl
.We can also use:
0
-- to the beginning of line$
-- to the end of linew
-- to the beginning of next word or punctuation charW
-- to the beginning of next word (ignore punctuation)b
-- backwards one word or punctuation charB
-- 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 pressingw
3 times.We can also use the arrows (left, right, up, down).
-
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 commanda
.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 5Then 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
- Go to the first line: