Common commands in Vim

Source: Internet
Author: User
Tags add numbers

The

VI editor has 3 modes: Command mode, input mode, and last line mode. It is important to master these three modes: the

Command mode: The default entry is the command mode after startup, the command can be switched from this mode to the other two modes, and the command mode can be returned in any mode as long as the [ESC] key is pressed. Enter the subtitle "I" in the command mode to edit the file into the input mode of VI.

Input mode: In this mode we can edit, modify, input and other editing work, in the last line of the editor to display a "--insert--" Mark VI entered the input mode. We need to save the file when we have finished modifying the input, then we need to return to the command mode and save it in the last line mode.

Last line mode: enter ":" in command mode to enter the mode, there are many good commands in the last line mode.

4. Edit Operation
Enter input mode command
I insert command a append command o Open command C modify command
R replace command s replace command ESC exit command

Input mode action
Home cursor to beginning of line
  End cursor to end of line
Page up and PAGE down
Delect Delete the character of the cursor position

Delete operation (Command mode use)
x Delete single character at cursor
DD Delete cursor line
DW deletes all characters of the current character to the end of the word, including spaces
#x例如3x删除光标处向右的三个字符
#dd例如3dd从当前行开始向下删除三行文本

Undo Action
U command cancels the last operation and can be used multiple times to restore the original Action
U cancel all operations
Ctrl+r can revert to operations using the U command

copy operation
YY command copies the contents of the current entire line to the VI buffer
YW copy the contents of the current cursor to the end of the word to the vi buffers, equivalent to Copy a word
y$ the location of the cursor to the end of the line to the buffer area
y^ Copy the cursor to the beginning of the content to the buffer area
#yy例如: 5yy is to copy 5 rows
#yw例如: 2yw is to copy two words

If you want to copy the first M Rows to the nth row of content, you can enter M,ny in the last line mode for example: 3,5y copy the third line to the fifth line of content to the buffer.

5. Find and replace
The search and replace function of VI is mainly done in the last line mode:

Top-down Search
/To find the character channeling, where/represents the search from the cursor location, for example:/work

To the bottom of the search
。 To find the character channeling for example:/work

Replace
: S/old/new replaces the first occurrence of the old in the row with new
: S/old/new/g replaces all occurrences of old in line with new
: #,# s/old/new/g replaces the old from line # to # # with new
:% s/old/new/g replaces the old that appears in the entire article with new

If the range of replacements is large, a C command is added at the end of all commands, forcing each substitution to require the user to confirm, for example: S/OLD/NEW/C or S/OLD/NEW/GC

6 Recovering files
VI when editing a file, a temporary file is generated, which is the file. Begins and ends with a. SWP. Normal exit the file is automatically deleted, if unexpected exit such as sudden power outage, the file will not be deleted, we can choose the next edit command processing:

o read-only open without changing the contents of the file
E continue editing the file without restoring the contents of the. swp file saved
R restores the contents of the file after the last edit was not saved
Q Exit VI
D Delete the. swp file
Or use the Vi-r file name to restore unsaved content

Under the GUI:
(1) Can press I into the insertion mode
(2) Use the mouse to drag the reverse selection to paste the content, press the left mouse button to copy the selected block to the buffer
(3) Then move the cursor to the point where you want to paste, press the middle mouse button (two-button mouse can press the left and right), paste the buffer contents.

Under the plain text terminal:
(1) Select text block, use V to enter visual mode; Move cursor key selection
(2) Copy the selected block to the buffer, use Y; Copy the whole line, with YY
(3) Cut the selected block to the buffer, use D; cut the whole line with DD
(4) Paste the contents of the buffer, using p

Open the second file in the same edit window with: SP [filename]
Switch between multiple edit files, using Ctrl+w

The command preceded by a number indicating the number of repetitions, plus the letter indicating the name of the buffer used.
To get help, by using: helps [content or commands]

VI set tab to 4 and automatically convert to space 2009-10-30 15:33

: Set tabstop=4 "Force tabs to being displayed/expanded to 4 spaces (instead of default 8).
: Set softtabstop=4 "Make Vim treat <Tab> key as 4 spaces and respect hard Tabs.
: "I don ' t think this one would do and you want.
: Set Expandtab "Turn tab keypresses into spaces. Sounds like this is happening.
"You can still insert real Tabs as [ctrl]-v [Tab].
: Set Noexpandtab "Leave tab keys as Real tabs (ASCII 9 character).
: 1, $retab! "Convert all tabs to space or ASCII-9 (per" Expandtab "),
"On lines 1_to_end-of-file.
: Set shiftwidth=4 "When auto-indenting, indent by this much.
"(Use Spaces/tabs per" Expandtab ".)
: Help TabStop "Find out more about this stuff.
: Help VIMRC "Find out more about. VIMRC/_VIMRC:-)

Copy and paste basic methods in VI default classification 2009-07-30 11:16 Read 407 Comments 0 font Size: Big small We first make such a small attempt, please open the browser, copy a text from the Web page, and then open vim, with VIM, paste with "P", you will find that there is nothing to post, the reason is that, unlike Windows,vim has multiple clipboard, and the system Clipboard is completely independent, so when you copy the text in the browser, the text is saved in the system Clipboard, when you are using "P" To paste, in fact, he read the vim of the shear plate.
How is it convenient to copy the contents of the system Clipboard? Simple. Use another shortcut "Shift+insert" on it, if you are using Gvim, you can play the same role directly with the middle mouse button. is not very simple.
Ok. Let's get to the advanced level.
As we have said before, Vim has multiple shear plates, which are designed to make it easier for users to copy and paste when making more complex character processing. Please use the ": Reg" Command Now (press ESC, exit edit mode, then press: Enter Command mode and enter the Reg return), see. The results of the output on the screen. It's not very familiar, right. Is all the content you've copied before. Perhaps at this time you also found that the vim of the shear plate has so many, according to the numbers and symbols of a separate area, here to cite a number example-"" 5, see. It starts with ". And also. Did you see the clipboard with the number "+"? The content inside is not the text that you copied in the browser just now. Oh...... That's right. The system Clipboard number is "+".
Well, now that you know how to view all of the previous clipboard contents, we'll try to copy the contents of a particular shear board into the file we're editing. We know that the paste Directive of VI is "P", but if you want to paste a specific clipboard, just use P to die, then what to do. Simple. Put the number of the Clipboard on it. Now press ESC, exit edit mode, and then click +p Three characters (how to press. No way. "and + How to press." Use shift. ), all right.  You see, the text that was previously copied in the browser is pasted into the document. You already know how to copy the contents of the system Clipboard into vim. So how do you paste the characters you copied in vim somewhere else? Very simple, we just need to copy the text, copied into the "+ shear plate is not OK." ("+ Shear plate is the system shearing board, forgot.") In Vim, the copied instruction is Y (press Y in non-edit mode), then copy the text into the specified clipboard, just add the clipboard number. For example, we want to copy a piece of text from vim and then paste it into the browser's address bar: The first step is to select the text that needs to be copied (not in edit mode), then press the "+y" button on the keyboard for three characters, OK.  has been copied down, now with: Reg command to see, is not in the "+ Clipboard up." Well, so far, you have mastered the basic use of vim shear plate, of course, the function of vim shear board is also very powerful, this article can not be a word, please continue to explore it.Introduction to the Copy and paste function of multiple files in VI.

Post: 2007-6-08 22:43 | Author: seanhe | Source: | Viewed: 25 times | Go to Software Testing Forum discussion
Yesterday someone raised such questions, I specifically to edit, please share, may not write well.

In vi editing, you often encounter the need to put the file a in one or more lines, copied to the B file inside
(Many people may not know what to do), what to do.
Here are some of the methods:
1. If you have more than one machine and are networked, you can. Netterm virtual terminal, Telnet to the host to modify a file, copy, and then open the B file paste the past.
2. If a stand-alone, of course, with the UNIX-enabled editing function
Let's briefly introduce the commands in the VI Editor:
(A). YW the current word into the nameless buffer, preceded by "X", which indicates the name of the named X is stored
Buffer (x is a-Z), you can also add numbers to indicate the number of words deposited, later can be p or p life
Make out
YY to save the current row in the nameless buffer, see YW

(B). The command is as follows: A-Z is a buffer, or it can be moved out of the number 1-9 specified delete buffer
A named buffer is a good way to save several text segments for later access, movement, or rearrangement. These buffers are named with A to Z letters. In order to access one of these buffers, use double quotation marks
For example, to insert a row into the a buffer, you can type:
"Ayy
In order to put this text back in the file, type "AP


(c). m do the sign command:
The M letter marks the current line with a letter, and then the ' letter ' is used to move the cursor to the current line.
(see ' Command ')

The following: 1. 4 lines in a file to B file

The command is as follows: "ay4+
: Ex B (go to file B)
Then "AP
2. Copy multiple lines in a to file B
First sign at the beginning, Mk.
Then use "Ay" K at the end (the cursor automatically returns to the beginning, when you have put your content in the buffer)
Where a means buffer a
Then escape to your file B with an ex and paste it with the AP command.

3. If you have more than one file to copy and paste, look below:
A means buffer a, and you can still use buffer B?
Buffers have a-Z and 1-9 so much
The buffer is like a fried version under Windows, it has a number of good, you can copy it you want to, and then paste, but you use "ay time more A is, if you want to copy 462 lines with" Ay, copy 536 lines with "by

Related Article

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.