How To Use vi Editor
vi is a screen-oriented text editor originally created for the Unix operating system. The name “vi” is derived from the shortest unambiguous abbreviation for the ex command visual, which switches the ex line editor to visual mode.
vi is included in the most popular Linux distros like Ubuntu, Linux Mint or Debian. Also, you can use it in another UNIX based systems like FreeBSD, OpenBSD or MINIX.
vi editor has the following operation modes-
1. Command mode
Administrative tasks such as saving files, executing commands, moving the cursor, cutting and pasting lines or words, and finding and replacing.
2. Text mode
This mode enables you to insert text into the file.
Writing a very small C program using vi:
- Open a terminal
- Type: vi file.c, this will create a new file
3. vi is in command mode by default, if you want to write something, you can’t do it.
4. Type “i”, it enables you to write your code.
5. Now you’re in the insert mode, but if you want to delete a character you can’t do it.
6. Type ESC to change to command mode
7. In command mode, Type “x”, it should delete the character under the cursor
8. You can insert a character to the left of the cursor Typeing “i”
9. You can insert a character to the right of the cursor Typeing “a”
10. Insert “>” in the text
11. Go back to the command mode and type :wq, then type return wq
12. Now you’re in the prompt again, see the content of your file using cat:
13. Now you should be thinking that vi is very complicated, vi isn’t a complicated editor, rather it’s different because vi can only work in one mode at a time.
14. Open your file again (vi file.c) and write the following code:
printf(“vi is a great tool to write code faster ”);
15. Save your file, and if you have gcc installed you should compile and run it.
Vi common options to open a file:
- vi file
- Creates a new file if it already does not exist, otherwise opens existing file.
- vi -R file
- Read only mode
Moving between characters
- You must be in command mode.
- You can use the keys: up, down, left and right.
- Also, you can use another key
Vi common commands (you must be in command mode):
|i||Inserts text before current cursor|
|l||Inserts text at beginning of current line|
|a||Inserts text after current cursor|
|A||Inserts text at end of current line|
|o||Creates a new line for text entry below cursor|
|O||Creates a new line for text entry above cursor|
|x||Deletes the character under the cursor|
|X||Deletes the character before the cursor|
|dd||Deletes the line the cursor is on|
|cc||Removes contents of the line, leaving you in insert mode|
|r||Replaces the character under the cursor|
Copy and Paste commands in vi Editor ( Practice! )
- Create a new file with the name “linux-distros”
- Write the following list:
- Linux Mint
- Red Hat
3. Change to the command mode (ESC)4. Move the cursor to “Ubuntu”
5. Type yy (this is the command to copy one line)
6. Type G
7. Insert a new line Typing o
8. Change to command mode (ESC)
9. Type P (this the command to paste the line)
10. Type 1G
11. Type 4yy
12. Type G
13. Type P
14. Also, you can use another copy – paste commands
- Copies the current word
- Puts the copied text after the cursor.
vi Editor Advanced commands
|<<||Join the current line with the next one. A count joins that many lines.|
|>>||Shifts the current line to the left by one shift width.|
|:nr file||Reads file and inserts it after line n.|
|~||Switch the case of the character under the cursor.|
|^G||Press CTRL and G keys at the same time to show the current filename and the status.|
|U||Restore the current line to the state it was in before the cursor entered the line.|
|u||Undo the last change to the file. Typing ‘u’ again will re-do the change.|
|J||Join the current line with the next one. A count joins that many lines.|
|:f||Displays current position in the file in % and file name, total number of file.|
|:f filename||Renames current file to filename.|
|:w filename||Write to file filename.|
|:e filename||Opens another file with filename.|
|:cd dirname||Changes current working directory to dirname.|
|:e #||Use to toggle between two opened files.|
|:n||In case you open multiple files using vi, use :n to go to next file in the series.|
|:N||In case you open multiple files using vi, use :N to go to previous file in the series.|
|:r file||Reads file and inserts it after current line|
Working with two or more files (Practice !)
- Open the linux-distros file
- In the command mode type ‘:e unix
:e unix3. Write “UNIX is a good OS” and save it. 4. Go to linux-distros file using the command ‘:e #’
5. Exit from vi typing :qIf you want to know more about vi you can visit the following links: