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

Vi editor usin

2025-04-06 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

I. Overview of Unix Editor

The editor is one of the important tools to use the computer, and the editor is an indispensable part in all kinds of operating systems. Unix and its similar family of ix operating systems provide a series of ex editors, including ex, edit,ed and vi. Ex,edit,ed is a line editor that is rarely used now, and Unix provides them to meet the needs of a variety of users, especially some end users. Fortunately, Unix provides a full-screen Vi editor, which makes our work a lot easier. Many DOS users complain that Vi editors are not as useful as editors under DOS, such as edit, because Vi takes into account the needs of various users and does not use some general editing keys (their definitions are different on different terminals, and they don't even have these keys on some terminals). Instead, you use state switching, but it's just a matter of habit, and once you're adept at using vi, you'll find it easy to use.

Although Vi uses the method of state switching, computers have a variety of hardware and operating systems, and some computers do not have specific function keys on their keyboards! So there are some functions that can't be used? This problem is the same with Unix systems, almost every major computer manufacturer has its own Unix system, and the operation method of vi will be a little different. Here we use the PC keyboard to explain the operation of vi, but also refer to the corresponding information in the specific environment, which is worth paying attention to.

II. Introduction to Vi

(1) entering vi

Type vi < File name > at the prompt characters (such as $, #), and vi can automatically help you load the file you want to edit or open a new file (if the file does not exist or the file name is missing). After entering the vi, a wave symbol appears on the left side of the screen, and the symbol at the beginning of the column means that the column is currently empty.

(2) two modes

As mentioned above, there are two modes of vi: instruction mode and input mode. Keystrokes entered in instruction mode will be processed as instructions: for example, entering aMagnevi is considered to insert characters at the current position. In input mode, vi treats the typed key as if it were an inserted character. To switch from the input mode to the input mode, you only need to type the corresponding input command (such as aline A). To switch from the input mode to the instruction mode, you need to type the ESC key in the input mode. If you do not know what mode you are in, you can press [ESC] several times. If the system beeps, it indicates that it is already in the instruction mode.

Pay: an instruction to enter input mode in instruction mode:

New (append)

A: add data from behind the cursor, and the data behind the cursor moves backward with the new data.

A: start adding data at the end of the column where the cursor is located.

Insert (insert)

I: start inserting data in front of the cursor, and the data behind the cursor moves backward with the new data.

I: start inserting data in front of the first non-blank character in the column where the cursor is located.

Start (open)

O: add a column under the column where the cursor is located and enter input mode.

O: add a column above the column where the cursor is located and enter input mode.

(3) withdraw from vi

In instruction mode, type: Q < Q < Q > < / < / I > < / I > < / w > < / em > > < / em > < / em >. Among them: wq and: X are saved to exit, while: Q is to exit directly, if there are new changes to the file, vi will prompt you to save the file and the: Q command will fail, then you can use the: W command to save the file and then use: Q exit, or use: wq or: X command to exit, if you do not want to save the changed file, you need to use: Q! Command, which exits vi without saving the file.

(4) basic editors

With the general keyboard function keys, such as arrow keys, [Insert], [Delete], etc., you should now be able to use vi to edit files. Of course, vi also provides many other features to make text processing more convenient.

What is an editor? It is generally considered to be the addition, modification and deletion of text, even including the moving and copying of text blocks, and so on. First, here is how to delete and modify vi. (note: in the original concept of vi, input and editing are two different things. The editor is operated in instruction mode, first using the instruction to move the cursor to locate the place to be edited, and then instructing to do the editing. )

Commands for deleting and modifying files:

X: delete the character where the cursor is located.

Dd: delete the column where the cursor is located.

R: modify the character of the cursor, followed by the character to be corrected.

R: when you enter the replace state, the new text will overwrite the original text until you press [ESC] to return to instruction mode.

S: delete the character where the cursor is located and enter input mode.

S: delete the column where the cursor is located and enter input mode.

In fact, there is no such trouble on PC at all! Both input and editing can be done in input mode. For example, to delete characters, just press [Delete]. On the other hand, the insertion state and the replacement state can be switched directly with [Insert], so there is no need to use any instruction mode of editing instructions. But as mentioned earlier, these instructions are available on almost every terminal, not just on PC. The basic instructions for moving the cursor in instruction mode are h, j, k, l. I think you should be able to guess by now that you just need to use the PC arrow keys directly, both in instruction mode and input mode. How easy it is. Of course, the PC keyboard also has its shortcomings. There is a good instruction u to restore the deleted text, while the U instruction can restore all changes to the column in which the cursor is located. This is the same function as the [Undo] button on some computers.

Annex: vi detailed instruction list

Command to enter vi

Vi filename: open or create a new file and place the cursor at the beginning of the first line

Vi + n filename: open the file and place the cursor at the beginning of line n

Vi + filename: open the file and place the cursor at the beginning of the last line

Vi + / pattern filename: open the file and place the cursor at the first string that matches pattern

Vi-r filename: system crash occurred the last time you were editing with vi, restore filename

Vi filename....filename: open multiple files and edit them in turn

Move cursor class command

H: move the cursor one character to the left

L: move the cursor one character to the right

Space: move the cursor one character to the right

Backspace: move the cursor one character to the left

K or Ctrl+p: move the cursor up one line

J or Ctrl+n: move the cursor down one line

Enter: move the cursor down one line

W or W: move the cursor right one word to the prefix

B or B: move the cursor left one word to the prefix

E or E: move the cursor right one word to the suffix

): move the cursor to the end of the sentence

(: move the cursor to the beginning of the sentence

}: move the cursor to the beginning of the paragraph

{: move the cursor to the end of the paragraph

NG: move the cursor to the beginning of line n

Nmarker: move the cursor down n lines

Nmury: move the cursor up n lines

Cursor: move the cursor to the end of the nth line

H: move the cursor to the top line of the screen

M: move the cursor to the middle line of the screen

L: move the cursor to the last line of the current screen

0: move the cursor to the beginning of the current line / / is the number 0, not the letter o

$: move the cursor to the end of the current line

Screen tumble command

Ctrl+u: turn half the screen to the first part of the file

Ctrl+d: flip half the screen to the end of the file

Ctrl+f: flip a screen to the end of the file

Ctrl+b: flip through the first screen of the file

Nz: scroll the nth line to the top of the screen, or the current line to the top of the screen if n is not specified.

Insert text class command

I: in front of the cursor

I: at the beginning of the current line

A: after the cursor

A: at the end of the current line

O: create a new line under the current line

O: open a new line above the current line

R: replace the current character

R: replace the current character and subsequent characters until you press the ESC key

S: replaces the specified number of characters with the entered text, starting at the current cursor position

S: delete the specified number of lines and replace them with the entered text

Ncw or nCW: modifies a specified number of words

NCC: modifies a specified number of rows

Delete command

Ndw or ndW: delete the word NMY at the beginning and after the cursor

Do: delete to the beginning of the line

Delete: delete to the end of the line

Ndd: delete the current line and the subsequent nmur1 line

X or X: delete a character, x after the cursor, and X before the cursor

Ctrl+u: deletes text entered in input mode

Search and replace command

/ pattern: search for pattern from the beginning of the cursor to the end of the file

Pattern: search for pattern from the beginning of the cursor to the beginning of the file

N: repeat the previous search command in the same direction

N: repeat the last search command in the opposite direction

: s/p1/p2/g: replace all p1 with p2 in the current line

: N1 Magazine, n2sqqp1, p2Unix, g: replace all p1 with p2 in lines N1 to N2.

: g/p1/s//p2/g: replace all p1 in the file with p2

Option settin

All: lists all option settings

Term: setting terminal type

Ignorance: ignore case in search

List: displays the tab stop (ctrl+i) and the end of line flag ($)

Number: display line number

Report: displays the number of modifications made by line-oriented commands

Terse: displays a short warning message

Warn: displays no write information if the current file is not saved when transferring to another file

Nomagic: allows special characters not preceded by "\" to be used in search mode

Nowrapscan: forbids vi to start from the other end when the search reaches both ends of the file

Mesg: allows vi to display information written to their terminals by other users using write

Last line command

: N1 co N2 n3: copy the content between lines N1 and N2 under line n3

: N1 meme N2 m n3: move the content between lines N1 and N2 to the next line n3

: N1 and N2 d: delete the content between N1 lines and N2 lines

W: save the current file

: e filename: open the file filename for editing

X: save the current file and exit

: Q: exit vi

: Q quit: do not save the file and exit vi

:! command: execute the shell command command

: N1 recording command: take the contents of lines N1 to N2 in the file as the input of command and execute them. If you do not specify N2, it means that the entire file content is input as the input of command.

: ringing command: puts the output of the command command to the current line

Register operation

Nyy: saves the contents of the current line and its next n lines to a register, where? is a letter and n is a number

"nyw: saves the current line and its next n words to a register, where? is a letter and n is a number

"nyl: saves the current line and its next n characters to a register, where? is a letter and n is a number

"? P: take out the contents of the register and place it at the cursor position. here? it can be a letter or a number.

Ndd: delete the current line and its total n lines of text, and put the deleted contents in the No. 1 delete register.

Set nu: displays all line numbers.

Set nonu: line numbers are not displayed

Case is not taken into account when set ic lookup

Case is considered when set noic lookup

The vi implementation selects the text block to press v, while vim is ctrl+v

Y is copy

P is paste

Delete the line where the cursor is located and all previous lines: dgg or D1G

Delete the line of the cursor and all lines after it: dG

The G cursor moves to the end of the file

Move the nG cursor to the nth line of the file

Move the cursor to line 1 of the file

: 1 move the cursor to line 1 of the file

: n move the cursor to the nth line of the file

Move the cursor to the end of the file

/ string move the cursor forward to the beginning of string

Move the string cursor back to the beginning of the string

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