Linux Tutorials

Use vi Editor

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:

  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
kCursor up
jCursor down
hCursor left
lCursor right

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

CommandDescription
i​Inserts text before current cursor
lInserts text at beginning of current line
aInserts text after current cursor
AInserts text at end of current line
oCreates a new line for text entry below cursor
OCreates a new line for text entry above cursor
xDeletes the character under the cursor
XDeletes the character before the cursor
ddDeletes the line the cursor is on
ccRemoves contents of the line, leaving you in insert mode
rReplaces 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 fileReads file and inserts it after line n.
~Switch the case of the character under the cursor.
^GPress CTRL and G keys at the same time to show the current filename and the status.
URestore the current line to the state it was in before the cursor entered the line.
uUndo the last change to the file. Typing ‘u’ again will re-do the change.
JJoin the current line with the next one. A count joins that many lines.
:fDisplays current position in the file in % and file name, total number of file.
:f filenameRenames current file to filename.
:w filenameWrite to file filename.
:e filenameOpens another file with filename.
:cd dirnameChanges current working directory to dirname.
:e #​Use to toggle between two opened files.
:nIn case you open multiple files using vi, use :n to go to next file in the series.
:NIn 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 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:



Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.