Vi Editor, vi Editor

Source: Internet
Author: User

Vi Editor, vi Editor
Vi editor Introduction

Vi Editor, usually called vi, is a text editing program widely used in various UNIX and Linux systems. It has powerful functions, but it has many commands and is not easy to grasp. It can execute output, delete, search, replace, block operations, and many other text operations, in addition, you can customize it as needed, which is not available in other editing programs. Vi is not window-based, so this multi-purpose editing program can be used to edit a variety of files on any type of terminal.

Address:Http://www.cnblogs.com/archimedes/p/linux-Vi.html, Reprinted, please specify the source address.

Three Modes of vi Editor

1. Command mode

In this mode, you can enter commands to execute many functions. Controls the movement of the screen cursor, the deletion of characters, words, or rows, the movement of copying a segment and entering the Insert mode, or to the last line mode

2. editing mode

When vi is running, it is usually in command mode. You can enter the following command to exit the command mode and enter the input mode: I (I), A (a), O (o)

3. Last line mode

Save the file or exit vi. You can also set the editing environment, such as searching strings and listing row numbers ...... . However, we generally simplify vi into two modes when using it, and calculate the last line mode into the command line mode.

Vi Entry and Exit

Go to vi:

Command "vi file name"

Run the "vi" command to specify the file name when exiting vi.

Option "+ n", indicating that the cursor is on line n of the file after entering vi

The "+" option indicates that you want to move the cursor to the last line of the file after entering vi.

Save the file:

1. In command mode, two uppercase letters are used for connection. <Z>

2. In the last row mode:

: W vi saves the current edited file, but does not exit vi, but continues waiting for the user to enter the command

: W <newfile>

: W! <Newfile> Save the content of the current file to the specified newfile. If newfile already exists, it overwrites the original content.

In the last row mode, there are four methods to exit vi and return to shell:

: Q system exits vi and returns to shell. When you use this command, if the edited file is not saved, vi will provide a prompt at the last line of the window.

: Q! Vi abandons the modification and directly returns it to shell.

: Wq first saves the file, and then exits vi and returns to shell.

: X the command has the same function as the ZZ command in command mode.

How to enter the insert mode

A (append) adds data after the cursor

A. add data at the end of the row

I (insert) add data before the cursor

I add data from the beginning of the row

O (open) adds a new row under this row for data input.

O add a row above this row for data input

How to exit the insert mode

ESC end insertion Mode

How to enter the last line mode first Press ESC :/

?

VI Environment Settings

Set nu: Add row number display

Set nonu: Cancel row number display

Set autoindent: auto indent

Set noautoindent: cancel auto indent

Search

You can search for a field in vi to move the cursor to this field.

/Search for the string after the cursor

? String to search for the string before the cursor

N. Continue searching for the next same string.

N continues to search for the next same string

Undo)

That is, the content before executing the previous command is restored.

U returns the result before the last command is restored.

U restore all changes to the cursor row

You can edit multiple files by using the following method:

# Vi file1 file2 ..

After editing the first file, you can use ": w" to archive the buffer and then use ": n" to load the next file.

At the same time when vi multiple files, the CTRL-SHIFT-6 back to the previous file, in the vi file and the last vi file switch. You can also use: e # To switch

String search and replacement

The s (substitute) command can be used to search for a range of rows and columns.

The g (global) command can search for information in the entire editing buffer zone.

: 1, % s/old/new/g change all "old" in the file to "new"

: 10th s/old/new/Change "old" from 20th rows to "new"

: % S/old/new/g change all "old" in the editing buffer to "new"

: 1, % s/^/string1 Insert "string1" at the beginning of the line from the first row to the last row of the file"

: % S/$/some string/g Add "some string" at the end of each row of the entire file"

: 1, % s/word1/word2/gc searches for the word1 string from the first row to the last row, and replaces the string with word2. Confirm one by one

Vi editing operation commands

In vi, "word" has two meanings:

Broadly speaking, a word is any content between two spaces.

In a narrow sense, words refer to English words, punctuation marks, and non-letter characters.

Sentences are defined as periods (.) and question marks (?). And exclamation point "!" Followed by at least one space or a linefeed character sequence.

Segment is defined as a segment that starts and ends with a blank row.

Command to move the cursor in the current line

H. move the cursor to the left.

L move the cursor to the right

J. move the cursor down one cell

K move the cursor up to a grid

 

0 move the cursor to the first character of the current row

$ Move the cursor to the last character in the current row

Command to move the cursor between rows

Ctrl + f roll back one page

Ctrl + d roll back half page

Ctrl + B roll forward one page

Ctrl + u roll forward half page

Ctrl + e scroll down a line

Ctrl + y roll up a line

 

NG move the cursor to the beginning of the row specified by the row number

Command to move the cursor between text blocks

The first character from the cursor position to the next sentence.

(The first character of the sentence from the cursor position

{The last character from the cursor position to the paragraph

} The first character from the cursor position to the paragraph

Move on screen

H command: This command moves the cursor to the first line of the screen. If the number n is added before the H command, the cursor is moved to the first line of the n line of the screen.

M command: This command moves the cursor to the beginning of the line in the middle of the screen display file.

L command: This command moves the cursor to the beginning of the line on the bottom line of the displayed file.

 

G command: This command moves the cursor to the last column of the file

Command mode:

X dh

Word and line deletion commands

Dw db dd d $ d0 <n> dd

Text block deletion command:

D) d (d} d {

Delete relative screen:

DH dM dL

Delete operation

X: Delete the text of the cursor.

Nx: delete n characters after the cursor

X: delete a character before the cursor

NX: delete n characters before the cursor

Dd: Delete the row where the cursor is located.

Ndd: delete n rows down where the cursor is located

Copy

Enter yy or nyy in command mode to copy n consecutive lines from the current line or from the current line. n is a specific integer.

Move the cursor to the location to be copied and press p to paste

Simple use of the vi Editor

Insert text I key

X: Delete the text of the cursor.

Return command mode ESC

Disk Storage: w

Exit Without saving the disk: q

Disk logout: wq

Force Exit: q!

Force save disk to exit: wq!

Other common commands of vi

Delete a line of text dd

Copy n lines of text yny

Paste the copied content p

Search for text abc/abc in the file

Move the cursor to the first line of the text: 1

Move the cursor to the last line of text: $ or G

Save disk and exit ZZ

 
How to Use VI Editor

First enter the directory of this file, then use vi to open vim assignment.1, then press I, o, or a to edit and Complete ESC exit mode, then press: Enter last line mode, enter wq, save, and exit

How to access the VI editor of LINUX?

VI is a notepad equivalent to the windows operating system.
For example, if you want to create a Hello. c file, perform the following operations:
Vi Hello. c (Press enter and start editing. To exit Press ESC, enter the intermediate mode, and press Colon: followed by the command)
: Wq (save and Exit)
: Q! (Exit and do not save)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.