This brief tutorial introduces the vi editor to new users and students unfamiliar with Linux.
If you’re a student or new user looking for a Linux system to start learning on, the most accessible place to start is Ubuntu Linux OS. It’s a great Linux operating system for Linux beginners.
Ubuntu is an open-source Linux operating system that runs on desktops, laptops, servers, 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 use when editing configuration files, creating lists, and 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 straightforward to use than vi. More people are using the Vim editor than just vi.
It is straightforward to invoke the vi editor.
Simply use 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 Command and Insert modes.
When you open the file with the vi command, it puts you in 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 enter the insert mode, 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, and use the arrow keys to navigate the text and other tasks. After editing the text, press the Esc key to return 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 file’s content and exit.
:w ====> Write
:q ===== Quit
You can also use the ZZ key to write or save your content and quit the vi editor.
If you made a mistake and edited the file and want to abandon all your changes and leave the file as is, simply run the commands below:
Esc key to return 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 an 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 the vi editor |
:q! | Quit vi and abandon all changes to the file |
yy | Copy the line of the file while in the command mode |
p | Paste a line below the current line |
o | The open new line below the current line |
O | Open a new line above the 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 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 the line |
As you can see in the table above, you do a lot with the vi editor. If you learn these options, you should be comfortable using vi as a text editor on the command line terminal.
That’s it!
Leave a Reply