5.2 Insert mode and command mode |
There are two mode in vi editor. Firstly, when you run vi editor, it is command mode for input command.
For editing, you need to change mode by command.
The vi editor is called screen editor which execute a command for current cursor position basically.
5.2.1 Input text
You need to change to insert mode by command to input text.
Practice: Input text |
Specify non-existing file "viinsert.txt".
$ vi viinsert.txt ↵ |
Key | Action |
---|---|
[Esc] | Exit from insert mode Stop the inputting command |
[Delete] | Delete a character in insert mode (a character which at cursor position's) |
[BackSpace] | Delete a character in insert mode (a character which is before cursor position's) |
Table 5-4:Special Key
To input text, change to insert mode using "i" command.
Then input text such as "test".
After inputting text, press [Esc] key to exit insert mode.
test |
To input text after cursor, use "a" command.
Here, input "aTEST[Esc]". ("a" is a command, "TEST" is input string.)
testTEST |
To input text at the beginning of line, use "I" command.
Here, input "I1234[Esc]". ("I" is a command, "1234" is input string.)
1234testTEST |
To input text at the end of line, use "A" command.
Here, input "A6789[Esc]". ("A" is a command, "6789" is input string.)
1234testTEST6789 |
When you have mistaken to input, delete a character before cursor using [Delete] or [BS].
The function of [Delete] and [BS] depends on environment.
After this file is changed, save a file and exit using ":wq" command.
:wq↵ |
5.2.2 Move cursor
If you want to edit a file, you can move cursor using commands as follows.
Command | Action |
---|---|
h or ← | Move to left |
j or ↓ | Move to down |
k or ↑ | Move to up |
l or → | Move to right |
Table 5-5:Commands for moving cursor
Practice: Move cursor |
Specify non-existing file "vimove.txt".
$ vi vimove.txt ↵ |
Input "itesttest[Esc]". Now the cursor is on the last "t".
If you want to insert space between "t" and "t", move cursor between "t" and "t".
For moving cursor to left, input "h". For moving cursor to right, input "l".
It is possible to use "←" instead of "h", "→" instead of "l".
Here, moving cursor between "t" and "t", then input "i [Esc]". (After "i" command, input space.)
testtest |
testtest |
test test |
Figure 5-1: Screen
Input "test test" in 4 lines.
For moving cursor to down, input "j". For moving cursor to up, input "k".
It is possible to use "↓" instead of "j", "↑" instead of "k".
Here, move to the top line, then input "IBegin[Enter][Esc]", and move to the last line, then input
"A[Enter]End[Esc]".
test test |
Figure 5-2: Screen
Begin |
Figure 5-3: Screen
Begin |
Figure 5-4: Screen
Begin |
Figure 5-5: Screen
Because a file is changed, save a file and exit using ":wq" command.