Nội dung
In this tutorial, AZDIGI will show you how to use vi on Linux to edit files.
I. What is Vi?
Vi was the first full-screen text editor written for Unix. While it is designed to be very light and simple, it can be a bit daunting to use for those who only use interfaced text editors, such as NotePad++ or gedit.
II. Why you should read through the guide to using vi on Linux
There are at least 2 reasons for you to learn how to usevi
.
vi
available on all Linux operating systems.vi
consumes almost no system resources and allows you to perform almost all tasks without lifting your finger from the keyboard.
Besides, vi
has a very rich set of built-in instructions, which you can invoke with the command: vi –help.
vi --help
III. How to use vi on Linux
1. Start vi
To start vi
to edit a file you just need to use the vi filename
command. For example, I will usevi
to edit the anaconda-ks.cfg
file.
vi anaconda-ks.cfg
Next, to enter edit mode you just need to pressi
on the keyboard. When entering this mode, you will see -- INSERT --
at the bottom of the interface using vi
.
2. Commands to use vi
The following table shows a list of commonly used vi commands: Commands when using vi with files can be executed by adding an exclamation point to the command (Example: “:q!
” Perform exit without saving the file).
Command | Command Description |
h or left arrow ⇦ | Move left one character. |
j or down arrow ⇩ | Move down one line. |
k or up arrow ⇧ | Move up one line. |
l (lowercase L) or right arrow ⇨ | Move right one character. |
H | Go to the top of the screen or to the top of the page. |
L | Go to the bottom of the screen or the bottom of the page. |
G | Go to the bottom of the file. |
w | Move to the next word. |
b | Move back to the front word. |
0 (zero) | Move to the beginning of the standing line. |
^ | Moves to the first character in the current line. |
$ | Move to the end of the standing line. |
Ctrl + b | Move back to one screen or one page. |
Ctrl + f | Move to a screen or a page. |
i | Enter data editing mode at the standing position. |
I (capital i) | Enter data editing mode at the beginning of the standing line. |
J (capital letter j) | Append the current line to the next line. |
a | Append after the current position |
o (lowercase O) | Creates a blank line after the current line. |
O (capital o) | Create a blank line before the current line. |
r | Replace the character with a new character in the current position. |
R | Overwrite in current position. |
x | Delete the character at the current position. |
X | Delete the character immediately before (left) the current position. |
dd | Cut (possibly to paste later) the entire current line. |
D | Cut from the current cursor position to the end of the line (this command is equivalent to d$). |
yX | Issue the command to move X, copy (drag) the appropriate number of characters, words or lines from the current cursor position. |
yy or Y | Copy the entire current line. |
p | Paste after (next line) the current cursor position. |
P | Paste before (the previous line) the current cursor position. |
. | Repeat the last command. |
u | Undo the last command. |
U | Undo the last command at the last line. This will work as long as the cursor stays on the line. |
n | Find the next match in a search. |
N | Find previous matches in a search. |
:n | Next file; when multiple files are specified for editing, this command moves on to the next file. |
:e file | Load the file to the location of the current file. |
:r files | Insert the contents of the file after (next line) the current cursor position. |
:q | Exit and don’t save the changes. |
:w files | Write the current buffer to the file. To add to an existing file, use: w >> file. |
:wq | Write the contents of the current file and exit. Equivalent to x! and ZZ |
:r! command | Execute the command and insert the output after (next line) the current cursor position. |
3. Search and Replace
vi
has the ability to move the cursor to a certain position based on the search results. It can also do text replacement and has a confirmation step from the user.
- Search in one line: the
f
command search for a line and move the cursor to where the search results appear.
For example, the fo command will move the cursor to the next instance of the letter o in the current line. Note that neither the f character nor the character you are looking for will appear anywhere on your screen, but the character in the result will be in the cursor after you press Enter.
- Searching the entire contents of a file: Use the command
/
followed/
by the word or phrase you want to search for. The next matching results will be displayed as you continue to press n, or review previous results with N. This is the result of typing /Setup in command mode.
- Replace data:
vi
use a command to perform substitution operations on a range of lines or an entire file. To change from “Root” to “admin” for the entire file, we have to enter the following command:
:%s/Root/admin/g
If you want to confirm before replacing data then add thec
option at the end of the command:
:%s/Root/admin/gc
Options when asked for alternate confirmation:
- y: do replace (yes)
- n: skip this occurrence and move on to the next (also known as no replacement)
- a: perform replacement in all found results.
- q or Esc: do not replace and exit the command instead.
- l (lowercase L): perform the replacement on the first result only.
- Ctrl-e, Ctrl-y: Scroll down and scroll up respectively to see suggested alternative contexts.
III. Summary
Hopefully, this tutorial will help you better understand it and easily manage your servers.
Wishing you success!
If you need assistance, you can contact support in the ways below:
- Hotline 247: 028 888 24768 (Ext 0)
- Ticket/Email: You use the email to register for the service and send it directly to: support@azdigi.com .