Ceci est une ancienne révision du document !
Table des matières
Version : 2024.01
Last update : 2024/10/11 10:50
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 - Commands
Command | Description |
---|---|
vi fileName | Open or create a file |
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 |
The same as :wq |
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 |
Undo last command | |
Exit without saving |
.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.
====.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].
====.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 |
l or → or Spacebar | Move cursor one character to the right |
Move cursor one character to the right | b | Move cursor one word to the left |
Moves cursor one word to the right |
Move cursor to end of current word |
Move cursor one word to the right |
Moves cursor to middle of screen |
Moves cursor to bottom of screen |
Move cursor to bottom of screen | G or :$ | Move cursor to last line of file |
G or :0 | Move cursor to first line of file | G or :$ | Move cursor to last line of file | G or :0 |
27G | Move cursor to line 27 |
Ctrl+f | Scroll forward through a screen page | Ctrl+d | Scroll forward through a screen page | Ctrl+f | Scroll forward through a screen page Ctrl+d | Scroll forward half a screen | Ctrl+b | Scroll forward half a screen | Ctrl+c | Scroll forward half a screen
Ctrl+b | Scroll back one page | Ctrl+u |
Scroll back one half-screen | Ctrl+u | Scroll forward one half-screen | Ctrl+d | Scroll forward one half-screen | Ctrl+b | Scroll back one half-screen |
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 cursor | ||||
Insert text at beginning of line | a | ||||
Insert text after cursor | A | Insert text before cursor | I | Insert text at beginning of line | I |
Insert text at end of line | O | Insert text at beginning of line | I | Insert text at beginning of line | I | Insert text at beginning of line |
Insert text after cursor | A | Insert text at end of line | O | Insert line after current line |
Insertion of a 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 up | |
Search for the next occurrence up from the last search | |
Search for the next occurrence of string in the direction of the search | |
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 |
Delete character to left of cursor | |
Delete 5 characters from current character | dw |
Delete current word | |
5dw | Delete 5 words from current character |
dd or :d | Delete current line |
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 |
[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.