Ceci est une ancienne révision du document !
Table des matières
Version : 2024.01
Last update : 2024/11/04 13:30
RH12402 - The VI Editor
Module content
- RH12402 - The VI Editor
- Module content
- Presentation
- LAB #1 - Creating, opening and closing files
- 1.1 - Commands
- 1.2 - Creating a new file with VI
- 1.3 - Open a file in read-only mode with the view command
- 1.4 - Opening a file in read-write mode with the vi command
- LAB #2 - The set command
- 2.1 - Commands
- 2.2 - Enabling line numbering with the set command
- LAB #3 - Moving within a File
- 3.1 - Commands
- LAB #4 - Inserting Text
- 4.1 - Commands
- 4.2 - Inserting text
- LAB #5 - Text Search
- 5.1 - Commands
- 5.2 - Finding and replacing text
- LAB #6 - Deleting Text
- 6.1 - Commands
- 6.2 - Deleting Lines
- LAB #7 - Copy, Cut and Paste
- 7.1 - Commands
- 7.2 - Copying, Cutting and Pasting Text
- LAB #8 - Configuring a Personal VI Interface
Présentation
VI is a powerful text editor with three modes:
- Command
- Ex-mode
- Insert
In command mode, you can enter commands such as i to insert text.
In Ex-Mode, most VI commands are preceded by the : key, for example :q to quit.
In insert mode, you can:
- edit text,
- add text
- search for text
- copy text
- paste text
- cut text,
- replace text.
LAB #1 - Create, open and close files
1.1 - Commands
Command | Description |
---|---|
vi fileName | Open or create a file |
vi | Opening a new file |
vi -r filename | Recovering a file after a failure |
view filename | Open a file in read-only mode |
:wq | Exit by saving and changing the modification date |
ZZ | The same as :wq |
:x | Exit by saving without modifying the modification date if the file has not been modified |
:q | Exit if the file has not been modified or if the view command has been used |
u | Undo last command |
U | Undo changes made to current line. |
:q! | Exit without saving |
1.2 - Creating a new file with VI
Copy the 25 lines below:
This is line 1 This is line 2 This is line 3 This is line 4 This is line 5 This is line 6 This is line 7 This is line 8 This is line 9 This is line 10 This is line 11 This is line 12 This is line 13 This is line 14 This is line 15 This is line 16 This is line 17 This is line 18 This is line 19 This is line 20 This is line 21 This is line 22 This is line 23 This is line 24 This is line 25
Create a new file called vitext using the vi command:
[root@redhat9 inode]# exit logout [trainee@redhat9 /]$ cd ~ [trainee@redhat9 ~]$ vi vitext
To do - Press the i key on your keyboard to switch to insert mode. Click the middle button (the wheel) of your mouse to paste lines 1 to 25 into the file. Then use the Escape key to switch to Command mode. Press the : key followed by the X key to save and exit VI.
1.3 - Opening a file in read-only mode with the view command
Now open the file /home/trainee/vitext in read-only mode:
[trainee@redhat9 ~]$ view vitext
You will get a result similar to this one:
This is line 1 This is line 2 This is line 3 This is line 4 This is line 5 This is line 6 This is line 7 This is line 8 This is line 9 This is line 10 This is line 11 This is line 12 This is line 13 This is line 14 This is line 15 This is line 16 This is line 17 This is line 18 This is line 19 This is line 20 This is line 21 This is line 22 This is line 23 This is line 24 This is line 25 ~ ‘vitext [readonly] 25L, 391B 1,14 All
Warning: Make sure you only see the first 25 lines of this file.
Important: Note that the last line is marked [readonly].
1.4 - Opening a file in read-write mode with the vi command
Exit view with the :q command and open the /home/trainee/vitext file in read-write mode:
[trainee@redhat9 ~]$ vi vitext
You will get a result similar to this one:
This is line 1 This is line 2 This is line 3 This is line 4 This is line 5 This is line 6 This is line 7 This is line 8 This is line 9 This is line 10 This is line 11 This is line 12 This is line 13 This is line 14 This is line 15 This is line 16 This is line 17 This is line 18 This is line 19 This is line 20 This is line 21 This is line 22 This is line 23 This is line 24 This is line 25 ~ ‘vitext 25L, 391B 1,14 All
Important: Note that vi is run in Command mode.
LAB #2 - The set command
2.1 - Commands
Command | Description |
---|---|
:set nu | Display line numbering |
:set number | Display line numbering |
:set nonu | Suppress line numbering |
:set nonumber | Suppress line numbering |
:set ic | Search without regard to case |
:set noic | Search case sensitive |
2.2 - Enable line numbering with the set command
Activate line numbering with the :set nu command or the :set number command. You will obtain a result similar to the following:
1 This is line 1 2 This is line 2 3 This is line 3 4 This is line 4 5 This is line 5 6 This is line 6 7 This is line 7 8 This is line 8 9 This is line 9 10 This is line 10 11 This is line 11 12 This is line 12 13 This is line 13 14 This is line 14 15 This is line 15 16 This is line 16 17 This is line 17 18 This is line 18 19 This is line 19 20 This is line 20 21 This is line 21 22 This is line 22 23 This is line 23 24 This is line 24 25 This is line 25 ~ :set nu 1,14 All
LAB #3 - Moving within a file
3.1 - Commands
Command | Description | |
---|---|---|
h or ← or Backspace | Move cursor one character to the left | |
j or ↓ or ↵ Enter | Move cursor one line down | |
k or ↑ | Move cursor one line up | Move cursor one character to the right |
l or → or Spacebar | Move cursor one character to the right | |
b | Move cursor one word to the left | |
w | Moves cursor one word to the right | |
e | Move cursor to end of current word | |
H | Moves cursor to the top of the screen | |
M | Moves cursor to the middle of the screen | |
L | Moves cursor to the bottom of the screen | |
G or :$ | Move cursor to last line of file | |
G or :0 | Move cursor to first line of file | |
27G | Move cursor to line 27 | |
Ctrl+f | Scroll forward a screen page | |
Ctrl+d | Scroll forward half-screen page | |
Ctrl+b | Scroll back one screen page | |
Ctrl+u | Scroll back one half-screen page |
To do: Test each command to see the results. Then return to the first screen and position your cursor at the beginning of line 13.
Important: To find out why the H, J, K and L keys are used as a directional pad, see this page.
LAB #4 - Text insertion
4.1 - Commands
Key(s) | Description |
---|---|
i | Insert text before the cursor |
I | Insert text at beginning of the line |
a | Insert text after the cursor |
A | Insert text at end of the line |
o | Insert line after the current line |
O | Insert line before the current line |
R | Replace existing text |
Echap | Switch from Insert mode to Command mode |
4.2 - Inserting text
Insert a line below the current line using the o command. Note that you are now in Insert mode. Then type Linux is super. You will get a result similar to this one:
1 This is line 1 2 This is line 2 3 This is line 3 4 This is line 4 5 This is line 5 6 This is line 6 7 This is line 7 8 This is line 8 9 This is line 9 10 This is line 10 11 This is line 11 12 This is line 12 13 This is line 13 14 Linux is super 15 This is line 14 16 This is line 15 17 This is line 16 18 This is line 17 19 This is line 18 20 This is line 19 21 This is line 20 22 This is line 21 23 This is line 22 24 This is line 23 25 This is line 24 -- INSERT -- 14,15 Top
Warning - Do not change the size of your terminal. You should ONLY view the first 25 lines.
Now switch to Command mode by pressing the Escape key, then position yourself on the last line of the screen using the L command. Go to the end of the line in Insert mode using the A command and enter the phrase Linux is super again. You'll get a result similar to this one:
1 This is line 1 2 This is line 2 3 This is line 3 4 This is line 4 5 This is line 5 6 This is line 6 7 This is line 7 8 This is line 8 9 This is line 9 10 This is line 10 11 This is line 11 12 This is line 12 13 This is line 13 14 Linux is super 15 This is line 14 16 This is line 15 17 This is line 16 18 This is line 17 19 This is line 18 20 This is line 19 21 This is line 20 22 This is line 21 23 This is line 22 24 This is line 23 25 This is line 24 26 This is line 25Linux is super -- INSERT -- 26,30 All
Now switch to Command mode by pressing the Escape key, then position yourself at the start of the first line of the screen using the H command. Position yourself at the fourth word using the w command three times. Switch to Insert mode using the i command, then type the phrase Linux is super again. You will get a result similar to this one:
1 This is line Linux is super1 2 This is line 2 3 This is line 3 4 This is line 4 5 This is line 5 6 This is line 6 7 This is line 7 8 This is line 8 9 This is line 9 10 This is line 10 11 This is line 11 12 This is line 12 13 This is line 13 14 Linux is super 15 This is line 14 16 This is line 15 17 This is line 16 18 This is line 17 19 This is line 18 20 This is line 19 21 This is line 20 22 This is line 21 23 This is line 22 24 This is line 23 25 This is line 24 26 This is line 25Linux is super -- INSERT -- 1,27 All
Now switch to Command mode by pressing the Escape key, then move to the beginning of the first line of the screen using the H command.
LAB #5 - Text search
5.1 - Commands
Key(s) | Description |
---|---|
/ string | Search string downwards |
// | Search for the next occurrence down from the last search |
? | Search for string upwards |
?? | Search for the next occurrence up from the last search |
n | Search for the next occurrence of string in the direction of the search |
N | Search for the previous occurrence of string in the direction of the search |
1 This is line Linux is super1 2 This is line 2 3 This is line 3 4 This is line 4 5 This is line 5 6 This is line 6 7 This is line 7 8 This is line 8 9 This is line 9 10 This is line 10 11 This is line 11 12 This is line 12 13 This is line 13 14 Linux is super 15 This is line 14 16 This is line 15 17 This is line 16 18 This is line 17 19 This is line 18 20 This is line 19 21 This is line 20 22 This is line 21 23 This is line 22 24 This is line 23 25 This is line 24 26 This is line 25Linux is super :g/super/s//wonderful/g
1 This is line Linux is wonderful1 2 This is line 2 3 This is line 3 4 This is line 4 5 This is line 5 6 This is line 6 7 This is line 7 8 This is line 8 9 This is line 9 10 This is line 10 11 This is line 11 12 This is line 12 13 This is line 13 14 Linux is wonderful 15 This is line 14 16 This is line 15 17 This is line 16 18 This is line 17 19 This is line 18 20 This is line 19 21 This is line 20 22 This is line 21 23 This is line 22 24 This is line 23 25 This is line 24 26 This is line 25Linux is wonderful 3 substitutions on 3 lines 26,1 All
LAB #6 - Deleting text
6.1 - Commands
Key(s) | Description |
---|---|
x | Delete current character |
X | Delete character to left of the cursor |
5x | Delete 5 characters from current character |
dw | Delete current word |
5dw | Delete 5 words from current character |
dd or :d | Delete current line |
5dd | Delete 5 lines starting from current line |
:5,7 d | Delete lines 5, 6 and 7 |
6.2 - Deleting lines
Go to line 14 and delete it using the dd command. You will obtain a result similar to the following:
1 This is line Linux is wonderful1 2 This is line 2 3 This is line 3 4 This is line 4 5 This is line 5 6 This is line 6 7 This is line 7 8 This is line 8 9 This is line 9 10 This is line 10 11 This is line 11 12 This is line 12 13 This is line 13 14 This is line 14 15 This is line 15 16 This is line 16 17 This is line 17 18 This is line 18 19 This is line 19 20 This is line 20 21 This is line 21 22 This is line 22 23 This is line 23 24 This is line 24 25 This is line 25Linux is wonderful ~ 3 substitutions on 3 lines 14,1 All
Now delete lines 4, 5 and 6 using the :4,6 d command. You will get a result similar to this one:
1 This is line Linux is wonderful1 2 This is line 2 3 This is line 3 4 This is line 7 5 This is line 8 6 This is line 9 7 This is line 10 8 This is line 11 9 This is line 12 10 This is line 13 11 This is line 14 12 This is line 15 13 This is line 16 14 This is line 17 15 This is line 18 16 This is line 19 17 This is line 20 18 This is line 21 19 This is line 22 20 This is line 23 21 This is line 24 22 This is line 25Linux is wonderful ~ ~ ~ ~ 3 fewer lines 4,1 All
LAB #7 -Copy, Cut and Paste
7.1 - Commands
Key(s) | Description |
---|---|
yy or Y | Copy current line |
V | Select a block |
p | Paste the line cut or copied below the current line |
P | Paste the line cut or copied above the current line |
:2,3 co 7 | Copy lines 2 to 3 to line below 7 |
:2,3 m 7 | Moves lines 2 to 3 to the line below line 7 |
7.2 - Copy, cut and paste text
Place your cursor on line 3. Copy it using the yy command. Place your cursor on line 5 and use the p command. You will get the following result:
1 This is line Linux is wonderful1 2 This is line 2 3 This is line 3 4 This is line 7 5 This is line 8 6 This is line 3 7 This is line 9 8 This is line 10 9 This is line 11 10 This is line 12 11 This is line 13 12 This is line 14 13 This is line 15 14 This is line 16 15 This is line 17 16 This is line 18 17 This is line 19 18 This is line 20 19 This is line 21 20 This is line 22 21 This is line 23 22 This is line 24 23 This is line 25Linux is wonderful ~ ~ ~ 3 fewer lines 6,1 All
Place your cursor on line 4 and use the Y command. Place your cursor on line 6 and use the P command. You will get the following result:
1 This is line Linux is wonderful1 2 This is line 2 3 This is line 3 4 This is line 7 5 This is line 8 6 This is line 7 7 This is line 3 8 This is line 9 9 This is line 10 10 This is line 11 11 This is line 12 12 This is line 13 13 This is line 14 14 This is line 15 15 This is line 16 16 This is line 17 17 This is line 18 18 This is line 19 19 This is line 20 20 This is line 21 21 This is line 22 22 This is line 23 23 This is line 24 24 This is line 25Linux is wonderful ~ ~ 3 fewer lines 6,1 All
Use the command :4,5 co 15. You will get the following result:
1 This is line Linux is wonderful1 2 This is line 2 3 This is line 3 4 This is line 7 5 This is line 8 6 This is line 7 7 This is line 3 8 This is line 9 9 This is line 10 10 This is line 11 11 This is line 12 12 This is line 13 13 This is line 14 14 This is line 15 15 This is line 16 16 This is line 7 17 This is line 8 18 This is line 17 19 This is line 18 20 This is line 19 21 This is line 20 22 This is line 21 23 This is line 22 24 This is line 23 25 This is line 24 26 This is line 25Linux is wonderful :4,5 co 15 17,1 All
Note that lines 4 and 5 have been copied after line 15 :
... 15 This is line 16 16 This is line 7 17 This is line 8 ...
Use the command :4,6 m 20. You will get the following result:
1 This is line Linux is wonderful1 2 This is line 2 3 This is line 3 4 This is line 3 5 This is line 9 6 This is line 10 7 This is line 11 8 This is line 12 9 This is line 13 10 This is line 14 11 This is line 15 12 This is line 16 13 This is line 7 14 This is line 8 15 This is line 17 16 This is line 18 17 This is line 19 18 This is line 7 19 This is line 8 20 This is line 7 21 This is line 20 22 This is line 21 23 This is line 22 24 This is line 23 25 This is line 24 26 This is line 25Linux is wonderful 3 lines moved 20,1 All
Note that by deleting lines 4 to 6, line 20 has become line 17. The three cut lines therefore appear after line 17 and not after line 20 :
... 17 This is line 19 18 This is line 7 19 This is line 8 20 This is line 7 ...
LAB #8 -Configuring a Personal VI Interface
VI can be configured by each user by creating a file called ~/.exrc. This file must contain the option commands as they are typed on the command line but without the : character. The ~/.exrc file is read by VI when it is launched and the options set. For example, the following file will activate the line numbering and visibility of hidden characters options:
set nu set list ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ -- INSERT -- 2,9 All
To do - Save your vitext file and exit VI. Copy the above text and paste it into a new file called .exrc in your home directory.
Now open the file /home/trainee/vitext with VI:
[trainee@redhat9 ~]$ vi vitext
1 This is line Linux is wonderful1$ 2 This is line 2$ 3 This is line 3$ 4 This is line 3$ 5 This is line 9 6 This is line 10 7 This is line 11 8 This is line 12 9 This is line 13 10 This is line 14 11 This is line 15 12 This is line 16 13 This is line 7$ 14 This is line 8$ 15 This is line 17 16 This is line 18 17 This is line 19$ 18 This is line 7$ 19 This is line 8$ 20 This is line 7$ 21 This is line 20$ 22 This is line 21 23 This is line 22 24 This is line 23 25 This is line 24 26 This is line 25Linux is wonderful$ ‘vitext 26L, 442B 20,1 All
Copyright © 2024 Hugh Norris.