Ceci est une ancienne révision du document !


Version : 2024.01

Last update : 2024/10/11 10:47

RH12402 - The VI Editor

Module content

* RH12402 - The VI Editor * Module content * 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 | | Undo changes made to current line. | Exit without saving | ====.2 - Creating a new file with VI==== Copy the 25 lines below: <file> 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 </file> Create a new file called vitext using the vi command: <code> [root@redhat9 inode]# exit logout [trainee@redhat9 /]$ cd ~ [trainee@redhat9 ~]$ vi vitext </code>

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: <code> [trainee@redhat9 ~]$ view vitext </code> You will get a result similar to this one: <code> 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 </code>

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: <code> [trainee@redhat9 ~]$ vi vitext </code> You will get a result similar to this one: <code> 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 </code>

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: <code> 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 </code> =====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 | Move cursor to first line of file | 27G | Move cursor to line 27 | Ctrl+f

Scroll forward through a screen page Ctrl+d Scroll forward through a screen page Ctrl+f

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 forward one screen 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 before cursor | Insert text after cursor | A | Insert text before cursor | I | Insert text at beginning of line | I | Insert text at beginning of line 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: <code> 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 </code>

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: <code> 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 </code> 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: <code> 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 </code> 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 | | :g/string%/s**//string1//**/g** | Search and replace //string// with //string1// | ====5.2 - Find and replace text==== Now search for the string //super// using the **/super** command followed by the <key>Enter</key> key. Then search for the next two occurrences using the command **** follow by the <key>Enter</key> key then **** follow by the <key>Enter</key> key. At this point your cursor should be on the last word of the last line of your screen. Then search upwards for the //super// string using the **?super** command follow with the <key>Enter</key> key. Your cursor should be on line 14. Now press the **n** key. Your cursor should be on the first line. Then press the **N** key. Your cursor must be on line 14. Press the **H** command. Now replace the word //super// with //wonderful// using the command **:g/super/swonderful/g%% followed by the ↵ Enter key. VI confirms the replacement: <code> 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/swonderful/g </code> <code> 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 </code> =====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 5 characters from current character | 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: <code> 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 </code> Now delete lines 4, 5 and 6 using the :4,6 d command. You will get a result similar to this one: <code> 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 </code> =====LAB #7 -Copy, Cut and Paste===== ====7.1 - Commands==== ^ Key(s) ^ Description^ | yy or Y | Copy current line | | V | Select a block | | Paste line cut or copy below current line | P | Paste line cut or copy below current line | P | Paste line cut or copy below current line | Paste cut or copy line above current line | | Paste cut or copy line below current line | P | Paste cut or copy line above 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: <code> 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 </code> 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: <code> 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 </code> Use the command :4,5 co 15. You will get the following result: <code> 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 </code> Note that lines 4 and 5 have been copied after line 15 : <code> … 15 This is line 16 16 This is line 7 17 This is line 8 … </code> Use the command :4,6 m 20. You will get the following result: <code> 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 </code> 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 : <code> … 17 This is line 19 18 This is line 7 19 This is line 8 20 This is line 7 … </code> =====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: <file> set nu set list ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ – INSERT – 2,9 All </file>

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: <code> [trainee@redhat9 ~]$ vi vitext </code> <code> 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 </code> —– Copyright © 2024 Hugh Norris.