To run vi, type vi from the terminal, vi is installed on most of the Linux distros as default. You may also you the improved version called “Vim” if you are developing codes or scripts. If you don’t have vim installed just run the command to install “yum install vim-enhanced -y“.
To create a new file just write the name of the file after the command as follows:
For more functionality Vi is designed to work in three modes described below:
- Command Mode
- Edit (editing + adding) Mode
- Command Line Mode
Vi starts in Command mode, thus you cannot directly start typing or editing the lines inside the editor. You can always return to command mode by pressing ESC button anytime. To switch to Editing mode while you are in command mode press “ESC” and type “:” or press “Insert” button on your keyboard.
Below are the mostly used functions in Command Mode::
Command | Action |
i | Switches to Insert (edit) Mode |
a | Switches edit mode at the end of cursor |
A | Switches edit mode at the end of current line |
ESC | Closes edit mode and switches to Command Mode |
u | Undo |
U | Undo all |
o | Insert new line |
dd | Delete whole line |
/string | makes search for the “string” |
n | Search next item |
To switch to Command Line mode press ESC and type “:” without quotes. Below are the commands that you can use in command line mode:
Command | Action |
ZZ | Saves the current file and exits or |
wq | w for saving, q to exit |
w! | saves read-only files (force) |
q! | Quit without saving |
q | Quit |