Using Vi Editor in Linux with Commands
Introduction to the Vi Editor in Linux
The Vi Editor (also known as the Vi Improved editor) is a powerful text editor included with most Linux operating systems. It is a modal editor which means that it operates on different modes, known as insert mode and command mode. In insert mode, the user can type in text as if using a normal word processor, while in command mode the user can enter special commands to perform various operations.
Common Commands of Vi Editor
Navigation
- h: Moves the cursor one character to the left.
- j: Moves the cursor one line down.
- k: Moves the cursor one line up.
- l: Moves the cursor one character to the right.
- 0 or ^: Moves the cursor to the beginning of the current line.
- $: Moves the cursor to the end of the current line.
Editing Text
- a: Append text after the current cursor position.
- A: Append text to the end of the current line.
- i: Insert text before the current cursor position.
- I: Insert text at the beginning of the current line.
- o: Open a new line below the current line and start inserting text.
- O: Open a new line above the current line and start inserting text.
- r: Replace a single character at the current cursor position.
- R: Replace multiple characters at the current cursor position.
- x: Delete the character at the current cursor position.
Search and Replace
- /pattern: Search forward for the specified pattern.
- ?pattern: Search backward for the specified pattern.
- :s/old/new/g: Replace all occurrences of "old" with "new" on the current line.
- :s/old/new/gc: Replace all occurrences of "old" with "new", with a prompt for each replacement.
- :%s/old/new/g: Replace all occurrences of "old" with "new" in the entire file.
- :%s/old/new/gc: Replace all occurrences of "old" with "new", with a prompt for each replacement in the entire file.
Modes of Operation
- ESC: Switch to command mode.
- i: Switch to insert mode.
- :w: Save the current file.
- :q: Quit the current session.
- :wq: Save the current file and quit the current session.
This is just a brief overview of the various commands available in Vi Editor. To learn more about this powerful editor and its various features, you can consult its manual or make use of online resources available on the Internet.