Home Linux ServerLinux Fundementals Instructions for using vi on Linux

Instructions for using vi on Linux

by Thạch Phạm
Published: Last Updated on

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 .

  1. vi available on all Linux operating systems.
  2. 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.

AZDIGI Tutorial
vi --help
List of options to use with vi.

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.

AZDIGI Tutorial
vi anaconda-ks.cfg
Interface to view file content with vi.

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 .

File editing mode with 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).

CommandCommand 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.
HGo to the top of the screen or to the top of the page.
LGo to the bottom of the screen or the bottom of the page.
GGo to the bottom of the file.
wMove to the next word.
bMove 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 + bMove back to one screen or one page.
Ctrl + fMove to a screen or a page.
iEnter 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.
aAppend 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.
rReplace the character with a new character in the current position.
ROverwrite in current position.
xDelete the character at the current position.
XDelete the character immediately before (left) the current position.
ddCut (possibly to paste later) the entire current line.
DCut from the current cursor position to the end of the line (this command is equivalent to d$).
yXIssue the command to move X, copy (drag) the appropriate number of characters, words or lines from the current cursor position.
yy or YCopy the entire current line.
pPaste after (next line) the current cursor position.
PPaste before (the previous line) the current cursor position.
.Repeat the last command.
uUndo the last command.
UUndo the last command at the last line.
This will work as long as the cursor stays on the line.
nFind the next match in a search.
NFind previous matches in a search.
:nNext file; when multiple files are specified for editing, this command moves on to the next file.
:e fileLoad the file to the location of the current file.
:r filesInsert the contents of the file after (next line) the current cursor position.
:qExit and don’t save the changes.
:w filesWrite 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! commandExecute 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:
AZDIGI Tutorial
Find Root and replace it as admin with vi .

If you want to confirm before replacing data then add thec option at the end of the command:

AZDIGI Tutorial
Find Root and replace it as admin with vi but requires confirmation before replacement.

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:

Đánh giá

Tham gia nhóm hỗ trợ Server - Hosting

Tham gia nhóm Hỗ trợ Server - Hosting & WordPress để cùng nhau hỏi đáp và hỗ trợ các vấn đề về WordPress, tối ưu máy chủ/server.

Tham gia ngay

Bài viết cùng chuyên mục

AZDIGI – Không chỉ là đơn vị hàng đầu trong lĩnh vực Web Hosting và Máy chủ, chúng tôi mong muốn mang lại những kiến thức bổ ích nhất và luôn cập nhật thường xuyên cho cộng đồng người đam mê thiết kế website, công nghệ,…

Vui lòng không sao chép nội dung nếu chưa xin phép. Designed and Developed by PenciDesign