Introduction
In this lab, you will learn how to use the powerful vi text editor on the Linux operating system. The lab covers the basic navigation and editing commands in vi, as well as more advanced operations. You will start by understanding the fundamentals of the vi editor, such as opening the editor, moving the cursor, entering insert mode, and saving or quitting the editor. Then, you will explore more advanced text editing techniques, including searching, replacing, and performing various operations on the text. By the end of this lab, you will have a solid understanding of how to effectively use the vi editor for efficient text processing and editing in the Linux environment.
Understand the Basics of vi Editor
In this step, you will learn the basic navigation and editing commands in the vi text editor. Vi is a powerful and widely-used text editor in the Linux environment, and understanding its fundamentals is crucial for efficient text processing.
First, let's start by opening the vi editor. In the terminal, run the following command:
vi
This will open the vi editor in the default mode, which is the command mode.
Now, let's explore some basic navigation and editing commands in vi:
Moving the Cursor:
- Use the arrow keys (up, down, left, right) to move the cursor.
- Press
hto move the cursor left,jto move down,kto move up, andlto move right.
Entering Insert Mode:
- Press
ito enter the insert mode, which allows you to start typing and editing text. - While in insert mode, you can type, delete, and modify the text.
- To exit the insert mode and return to the command mode, press the
Esckey.
- Press
Saving and Quitting:
- To save the changes and quit the vi editor, type
:wqand pressEnter. - If you want to quit without saving, type
:q!and pressEnter.
- To save the changes and quit the vi editor, type
Example output:
## Opening vi editor
$ vi
## Navigating in vi
## Press 'h', 'j', 'k', 'l' to move the cursor
## Entering insert mode
## Press 'i' to start typing
## Saving and quitting
## Type ':wq' and press Enter to save and quit
## Type ':q!' and press Enter to quit without saving
Navigate and Edit Text in vi
In this step, you will learn how to navigate and edit text within the vi editor.
First, let's open the vi editor by running the following command in the terminal:
vi
Now, let's explore some commands for navigating and editing text in vi:
Moving the Cursor:
- Use the arrow keys (up, down, left, right) to move the cursor.
- Press
hto move the cursor left,jto move down,kto move up, andlto move right. - Press
wto move the cursor to the beginning of the next word, andbto move the cursor to the beginning of the previous word. - Press
0to move the cursor to the beginning of the current line, and$to move the cursor to the end of the current line.
Editing Text:
- Press
ito enter the insert mode and start typing. - Press
xto delete the character under the cursor. - Press
ddto delete the entire line. - Press
yyto copy the current line. - Press
pto paste the copied text.
- Press
Example output:
## Open vi editor
$ vi
## Navigate using arrow keys, 'h', 'j', 'k', 'l'
## Move to next/previous word using 'w', 'b'
## Move to start/end of line using '0', '$'
## Enter insert mode and start typing
## Delete character using 'x'
## Delete line using 'dd'
## Copy line using 'yy'
## Paste using 'p'
Perform Advanced Operations in vi
In this final step, you will learn some advanced operations in the vi editor, which will help you become more efficient in text processing and editing.
First, let's open the vi editor by running the following command in the terminal:
vi
Now, let's explore some advanced commands in vi:
Searching and Replacing:
- Press
/to enter the search mode, then type the word you want to search for and pressEnter. - Press
nto navigate to the next occurrence of the search term, andNto navigate to the previous occurrence. - To replace a word, type
:%s/old_word/new_word/gand pressEnter. This will replace all occurrences of "old_word" with "new_word" in the entire file.
- Press
Working with Multiple Files:
- Press
:e filename.txtto open a new file in the vi editor. - Press
:bnto switch to the next file, and:bpto switch to the previous file. - Press
:lsto list all the open files in the vi editor.
- Press
Executing Shell Commands:
- Press
:!commandto execute a shell command without leaving the vi editor. - For example,
:!ls -lwill list the contents of the current directory.
- Press
Example output:
## Open vi editor
$ vi
## Search for a word using '/'
## Navigate to next/previous occurrence using 'n', 'N'
## Replace a word using ':%s/old_word/new_word/g'
## Open a new file using ':e filename.txt'
## Switch between files using ':bn', ':bp'
## List open files using ':ls'
## Execute shell commands using '!command'
## Example: ':!ls -l'
Summary
In this lab, you will learn the basic navigation and editing commands in the vi text editor, as well as how to perform advanced operations. First, you will understand the basics of the vi editor, including how to open it, move the cursor, enter insert mode, and save and quit. Then, you will learn how to navigate and edit text within the vi editor, such as moving the cursor, inserting and deleting text, and searching and replacing. Finally, you will explore advanced operations in vi, including working with multiple files, using macros, and customizing the editor.



