Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use vi Editor under UNIX

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly shows you "how to use the vi editor under UNIX", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "how to use the vi editor under UNIX" this article.

Under UNIX, vi is one of the standard text editors. Almost any UNIX system takes the vi editor as the most basic "software" and is assembled in the system by default. In PC-UNIX, even if you choose a minimally installed system, the vi editor will be installed on the system by default.

When it comes to vi, you have to mention another powerful editor under UNIX-Emacs. Many people like to compare Emacs and vi, but the author believes that Emacs and vi are not comparable. The lightweight feature of vi makes it the preferred tool for system management, while the powerful feature of Emacs makes it the preferred tool for system development. If Emacs is used for system management, its huge volume and consumption of system resources (relative to vi) will inevitably affect the efficiency of system management; however, if vi is used for software development, it is somewhat incomplete in terms of functions for development, and it is bound to affect the flexibility of development. Our goal here is to set up and manage a server, so we chose to use the vi editor to complete the server configuration.

As all the modifications related to the server settings of this site are completed through vi, if you are going to build the server through the various methods introduced by this site, please be sure to master the use of vi. Otherwise, it will be very difficult to proceed smoothly. If you use another editor (such as Emacs), you can install it during the installation process or online through the yum command.

Usage

Friends who are used to editing GUI under Microsoft Windows will definitely have "rejection" when they come into contact with vi. I can only advise you to take it easy. When you get used to vi, you will find that it is both accurate and orderly, and importantly, it is highly targeted to the object to be edited.

The picture above shows the status of vi being started after entering the "vi" command under CUI.

Or enter the path to an existing file to open an existing text file. (here is an example of logging in to the system with a root user and typing "vi / etc/X11/xorg.conf". )

The image above shows the status of opening / etc/X11/xorg.conf with vi. The cursor is at the position of the first character sliding line on the upper left.

The biggest difference between vi and other editors is that it separates editing, processing, and other operations, forming three different modes of operation, which are "command mode", "insert mode" and "ex mode" respectively.

Notepad and other editors commonly used under Windows when opening a file, the cursor is already waiting for input, that is to say, we can edit the file when we open it. For example, if we want to enter new text, or copy a piece of text, all operations are possible as long as the editor is open.

But in vi, in order to enhance the pertinence of the text to be processed, different operations such as text input, copy and so on are separated. For example, the mode of text input is called "insert mode". In insert mode, we can input, edit and so on the text in the same way as in Windows, but copy and other non-editing operations cannot be completed in insert mode. You need to switch to the corresponding mode and use the corresponding command to complete it.

Here, let's start with an explanation of the important vi patterns.

Mode of ★ vi (command mode, insert mode, ex mode)

1) Command mode:

When we just create or open an existing file through vi, the first mode to be read by default is "command mode". The characteristic of command mode is that there is no prompt banner in the lower-left corner of the editor window.

In this mode, we cannot enter new text into this file without passing the corresponding command. It is recommended that new contacts who have just come into contact with vi do not press the keyboard randomly to try to enter some text into the file when opening a file with vi, because the so-called "command" in command mode is not a command in the form of a process we enter at the command line, such as "ls-> enter" and so on. To be exact, a command in vi command mode is just a key or a combination of some keys on the computer keyboard. When we hit a key on the keyboard in command mode, the corresponding command has already been executed.

Here, I first introduce the command to move the cursor. Usually the movement of the cursor can be through the key on the right side of the keyboard, but because every time you move the cursor, the hand has to move to the right side of the keyboard significantly, so it is not efficient to use vi, so it is recommended to use the inherent commands (keys) of vi to control the movement of the cursor.

The cursor move command is as follows:

Move to the right-- > l

Move left-- > h

Move down-- > j

Move up-- > k

As shown above, for example, to move the cursor 8 characters to the right, just hit the l key 8 times on the keyboard, move down 3 lines, and hit the j key 3 times on the keyboard. In other words, these keys (commands) are used to control the movement of the cursor in vi command mode.

The second is the command to delete the text. Under Windows, most editors simply press the [Backspace] key continuously when deleting text. But because insert mode and command mode are separate in vi, you need to delete text in command mode (press the key) instead of pressing the [Backspace] key directly.

The command for text deletion is as follows:

Delete the character where the cursor is located-- > x

Delete the line where the cursor is located-- > dd (knock d twice)

As shown above, for example, to delete the three characters to the right from the cursor position, just type the x key three times on the keyboard; to delete the line where the cursor is located, simply tap dd (hit d twice in a row), and all characters and lines on the line of the cursor will be deleted.

Third, when you have finished editing a document and want to save and exit, the command is "ZZ" (uppercase, typing twice in a row).

Save and exit the document:

Save the current changes and exit-- > ZZ

In addition, vi also has some editing commands, which can be summarized as follows

Useful commands with an editorial nature:

Undo the previous operation-- > u

Move the cursor to the beginning of the line-- > 0 (number 0)

Move the cursor to the end of the line-- > $

Move the cursor to the top line of the current page-- > H

Move the cursor to the bottom line of the current page-- > L

Turn one page forward (Page Up)-- > Ctrl + f

Turn back one page (Page Down)-- > Ctrl + b

When using vi in the server configuration, the search function of vi is frequently used. In the state of the vi command line, after typing "/" (slash), the bottom of the vi appears "/" and is in the input waiting state, at this time, enter the text or text column you want to find, and then press enter to find the corresponding text or text column. When there are multiple objects, hit the "n" (lowercase) key to find the next target, and hit "N" to find the previous target.

Vi is a screen editor provided by all UNIX systems. It provides a window device through which files can be edited. Of course, people who know a little about the UNIX system will find vi super difficult to use, but vi is the most basic editor. If you learn vi well, you will be able to do it without hindrance and ease in the UNIX world, because other text processors are not standard in UNIX. If other Linux machines don't have joe or pico installed, and if you don't know how to use vi, you may not be able to do so.

The vi editor has three modes: command mode, input mode, and last line mode. It is important to master these three modes:

Command mode: after vi starts, it enters the command mode by default, from which you can switch to the other two modes by using the command, and you can return to the command mode by pressing the [Esc] key in any mode. Enter the letter I or o or an in command mode to enter the vi input mode to edit the file.

Input mode: in this mode we can edit, modify, input and other editing work, and the display of a "--INSERT--" in the last line of the editor indicates that vi has entered input mode. When we finish modifying the input and so on, we need to save the file, then we need to return to the command mode and save it in the last line mode.

Last line mode: enter this mode by typing ":" in command mode, and there are many useful commands in the last line mode.

The following describes the command operations commonly used in vi

Enter input mode command

I enter the body on the left side of the cursor [commonly used]

An enter the body to the right of the cursor [commonly used]

O lowercase character o, add a new line to the next line of the cursor

O uppercase character O, adding a new line on the previous line of the cursor

I enter the body at the beginning of the line where the cursor is located

An enter text at the end of the line where the cursor is located

Several simple ways to switch to insert mode are described above. There are also commands that allow you to delete a paragraph before entering insert mode

The text, so as to realize the replacement of the text. These commands include:

S replace the character pointed to by the cursor with the entered text [commonly used]

Ns replaces n characters to the right of the cursor with the text entered

Cw replaces the word on the right side of the cursor with the entered text [commonly used]

Ncw replaces the n words on the right side of the cursor with the text entered

Cb replaces the word on the left side of the cursor with the entered text [commonly used]

Ncb replaces the n words on the left side of the cursor with the text entered

Cd replaces the line of the cursor with the entered text [commonly used]

Ncd replaces the n lines below the cursor with the text entered

C $replaces all characters from the beginning of the cursor to the end of the line with the body entered [commonly used]

C0 replaces all characters from the beginning of this line to the cursor with the entered text [commonly used]

R replaces the character where the cursor is located, such as c3 means to replace the character at the cursor with the character 3 [commonly used]

R replaces characters until [ESC] is pressed

Press [Esc] or key combination [ctrl] + [exit input mode

Last row mode

W Save files [commonly used]

W file saves the contents of the current file to the file file

Wq saves the file and exits the vi editor [commonly used]

Wq file writes the contents of the current file to the file file, leaving the contents of the original file unchanged, and then exits the vi editor (if the contents of the current file change but no save operation is performed, the vi editor will not exit after executing this command)

R file reads the contents of the file file and inserts it after the line of the current cursor

E file closes the current file and opens a new file file (if the contents of the current file are changed but not saved, the command will not be executed) [commonly used]

F file renames the current file to file [commonly used]

F print the current file name and status, such as the number of lines of the file, the line number of the cursor, etc.

Q exit vi editor (cannot exit if there are changes to the current file) [commonly used]

Q! Force exit from the vi editor (do not save changes) [commonly used]

# move the cursor to line # [Common]

PS: some tips on using line numbers:

The absolute line number is usually specified by a numeric value; the character "." Represents the line number of the line on which the cursor is located; the character "$" represents the line number of the last line of the file, for example:

. + 5 move the cursor down 5 lines from the current line

Move the cursor to the last line of the file

1 move the cursor to the first line of the file

23w file writes line 23 of the file to the file file

3Jing 5w file writes lines 3 to 5 to the file file

1j. W file writes line 1 to the current line to the file file

., $w file writes the current line to the last line to the file file

.,. + 5w file writes 6 lines of content to the file file from the current line

1 file writes all the contents to the file file, which is equivalent to the w file command

The operation of moving the cursor

[Home] cursor to the beginning of the line

[End] cursor to the end of the line

[Page Up] turn the page up

[Page Down] turn the page down

[Delect] Delete the character at the cursor position

[Ctrl] + f move forward one page

[Ctrl] + b move back one page

The h cursor moves one character to the left [common]

J cursor moves down one character [commonly used]

The K cursor moves up one character [commonly used]

Move the cursor one character to the right [commonly used]

The number 0 moves the cursor to the end of the line.

Move the cursor to the beginning of the line [common]

G move the cursor to the end of the file [common]

Gg moves the cursor to the beginning of the file.

W move the cursor to the beginning of the next word.

E move the cursor to the suffix of the current or next word [commonly]

B move the cursor to the beginning of the current or next word [often]

^ move the cursor to the first non-white space character (tab,space) of the line [commonly used]

: # move the cursor to the # th position on the line, for example: 3,: 10 [commonly used]

) move the cursor to the end of the sentence

(move the cursor to the beginning of the sentence

Note: the sentence uses a half-corner full stop followed by a space (. ) Segmentation

} move the cursor to the end of the paragraph

{move the cursor to the beginning of the paragraph

Note: paragraphs are divided by blank lines

NG positions the cursor at the beginning of the nth line [commonly]

N + move the cursor down n lines

N-move the cursor up n lines

N $move the cursor down n lines and place it at the end of the line

H places the cursor at the top line of the current screen

M places the cursor in the middle line of the current screen

L place the cursor on the last line of the screen

~ change the case of the current character [commonly used]

> > move the current line 8 characters to the right (equivalent to [tab] key) [commonly used]

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report