This brief tutorial introduces the vi editor to new users and students who are not familiar with Linux.
If you’re a student or new user looking for a Linux system to start learning on, the easiest place to start is Ubuntu Linux OS. It’s a great Linux operating system for Linux beginners.
Ubuntu is an open source Linux operating systems that runs on desktops, laptops, server and other devices.
The vi editor is the most popular and still the favorite command line text editor among Linux administrators for file creation and editing.
This 43-year-old vi (pronounced vee-eye) editor is still an advanced, simple to use editor that most system admins run to when editing configuration files, creating list, writing scripts via the command line.
The vi editor has a symbolic link or alias to vim (vi Improved). Vim is an improvement to the original vi and much more simple to use than vi. More people are using the vim editor more than just vi.
It is very easy to invoke the vi editor.
Simply the commands below to create a new file or edit an existing file when the name filename.txt
vi filename.txt
The vi (aka, vim) has two modes: Command and Insert.
When you first open the file with the vi command, it puts in you in a command mode. This mode allows you to use the keyboard to navigate, delete, copy and paste and do other tasks.
You can not enter text in the command mode.
To leave the command mode and go into the insert mode, you will have to press the i key on your keyboard.
In the Insert mode, you can enter text, use the Enter key to go to a new line, use the arrow keys to navigate text and other tasks. When you’re done, editing text, simple press the Esc key to go back to the command mode.
To save a file after editing, you must first be in the command mode. From the Insert mode, press the Esc key to go to the command mode, then press these keys :wq to save the content of the file and exit.
:w ====> Write
:q ===== Quit
You can also use the ZZ key to write or save your content and quit vi editor.
If you made a mistake and editing file and which to abandon all your changes and leave the file as is, simple run the commands below:
Esc key to go back to the command mode, then press :q!
Below is a table of the vi command options:
vi switches | Purpose |
---|---|
vi filename.txt | Create or edit existing file |
i | Switches to Insert mode from the command mode |
Esc | Switches to the command mode from the Insert mode |
:w | Write or save your changes while in the Insert mode and continue editing |
:wq or ZZ | Save your changes and exit or quit vi editor |
:q! | Quit vi and abandon all changes to the file |
yy | Copy line of the file while in the command mode |
p | Paste a line below the current line |
o | Open new line below current line |
O | Open new line above current line |
dd | Delete the entire line |
G | Go to the last line of the file |
gg | Go to the first line of the file |
:num | Display the number of line’s line number |
h | Move to the left character |
k | Move up one line. |
j | Move down one line |
l | Move right one character |
XG | Go to X in line |
As you can see in the table above, you do a lot with vi editor. If you learn these options, you should be comfortable with using vi as a text editor on the command line terminal.
That’s it!