Use vi Editor

sohail
sohail

Table of Contents

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:

  1. Open a terminal
  2. Type: vi file.c, this will create a new file​
vi editor 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.

vi editor writing 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

vi editor for linux

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

vi editor for unix systems

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:

vi editor screenshot

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:

void main(void){
    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.

how to use vi editor
vi editor for linux operating systems

Vi common options to open a file:

  1. vi file
    1. Creates a new file if it already does not exist, otherwise opens existing file.
  2. vi -R file
    1. Read only mode

Moving between characters

  1. You must be in command mode.
  2. You can use the keys: up, down, left and right.
  3. Also, you can use another key
k Cursor up
j Cursor down
h Cursor left
l Cursor right

Vi common commands (you must be in command mode):

Command Description
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! )

  1. Create a new file with the name “linux-distros”
  2. Write the following list:
    • Ubuntu
    • Linux Mint
    • Debian
    • Slackware
    • Red Hat
vi editor for unix os

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)

using vi editor

10. Type 1G
11. Type 4yy
12. Type G
13. Type P

vi editor tutorial

14. Also, you can use another copy – paste commands

  • yw
    1. Copies the current word
  • p
    1. Puts the copied text after the cursor.

vi Editor Advanced commands

J  
<< 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 !)

  1. Open the linux-distros file
  2. In the command mode type ‘:e unix

:e unix 3. 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:

Linux TutorialsUncategorizedtutorial