vi is a screen-oriented text editor originally created for the Unix operating system. In below article we will go through vi command in unix with example . We will learn different mode of vi editor and how we can use different option while using vi command to edit files in unix.
vi command in unix with Example
Modes of VI command:
There are essentially three Modes in vi,
- Insert Mode :To insert or append in file
- Edit Mode : To edit the existing file
- Command Mode: where one is interacting with the operating system with actions like reading and writing files.
Most of the commands require just a simple letter or two, but be careful to note that vi is case sensitive, and capital letters do different things from lower case letters. It is not necessary to learn all of these commands, but eventually they will become second nature. The basic operations needed to create a file are related to each of those modes, so let’s look at them separately.
For adding text to a file ( press Esc and then below command) . The three most common ways to enter the Insert Mode are:
|Starts inserting in front of the current cursor position|
|Starts adding at the front of the line|
|Starts adding after the cursor|
|Starts adding at the end of the line|
|Starts opening a new line underneath the cursor|
|Starts opening a line above the cursor.|
|Gets out of Insert Mode|
Generally for moving the cursor and deleting stuff.
In the Edit Mode, the keys do not type letters, but do other actions such as cursor movement, deletions, copying lines, etc.
|Letter||Simple Cursor Movement|
|Moves cursor left one space||Note: the Arrow keys do work locally, but sometimes mess up over a network.|
|Moves cursor down one line|
|Moves cursor up one line|
|Moves cursor right one space|
|Moves the cursor a full word at a time to the right|
|Moves the cursor back to the left a word at a time|
|Moves the cursor to the front of a line|
|Moves the cursor to the end of a line|
|Moves the cursor forward a full page of text at a time|
|Moves the cursor backward a full page of text at a time|
|Deletes the character under the cursor|
|Deletes the line where the cursor is located (type d twice!)|
|Delete n consecutive lines ( n is an integer)|
|Replaces the character under the cursor with the next thing typed|
|Joins current line with the one below (Capital J!)|
|Undoes the last edit operation|
|Redo (Undoes the last undo operation)|
|Copies or yanks a line ( 5yy yanks 5 lines)|
|Puts the yanked text on the line below the cursor (lower case p)|
|Puts the yanked text above the current line (capital P)|
|Note: If vi is already in the input mode, text from that or another window may be highlited using the left mouse button, and copied into place by pressing the middle mouse button.|
For interacting with the operating system.
To enter the Command Mode, a colon ” : ” must precede the actual command.
|reads a file from disk into the vi editor|
|writes current file to disk|
|writes the file and quits vi|
|quits without writing (useful if you’ve messed up!)|
So in above tutorial , we have gone through the vi command in unix with example. We also see various mode of Vi command in unix.